.net 访问Lambda入口点中的ILambdaContext以实现. net无服务器API

vql8enpb  于 2022-12-05  发布在  .NET
关注(0)|答案(3)|浏览(119)

尝试获取ILambdaContext对象-示例和用例如下。我使用的是dotnet 6

public class LambdaEntryPoint : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction

    {
        internal static ILambdaContext Context;

        public override async Task<APIGatewayProxyResponse> FunctionHandlerAsync(APIGatewayProxyRequest request, ILambdaContext lambdaContext)
        {
            Context = lambdaContext;
            return await base.FunctionHandlerAsync(request, lambdaContext);
        }

        protected override void Init(IWebHostBuilder builder)
        {
            var variables = JsonConvert.SerializeObject(Context);
            //var variables = JsonConvert.Serliaze
            throw new Exception($"{variables}");
            var environment = "Beta";// arr[arr.Length - 1];

            //builder.UseStartup<Startup>();
            builder.ConfigureAppConfiguration((c, b) =>
            {
                b.AddJsonFile("appsettings.json");
                b.AddSystemsManager((source) =>
                {
                    var awsOptions = new AWSOptions();
                    awsOptions.Region = RegionEndpoint.EUWest1;
                    source.Path = $"/common";
                    source.AwsOptions = awsOptions;
                    source.ReloadAfter = TimeSpan.FromMinutes(5);
                });
                b.AddSystemsManager((source) =>
                {
                    var awsOptions = new AWSOptions();
                    awsOptions.Region = RegionEndpoint.EUWest1;
                    source.Path = $"/{environment}";
                    source.AwsOptions = awsOptions;
                    source.ReloadAfter = TimeSpan.FromMinutes(5);
                });
            }).UseStartup<Startup>();
        }
    }

我已经使用了here中的一个例子来尝试覆盖FunctionHandlerAsync入口点,但是Lambda上下文为空。我还尝试了许多其他路径,所有这些都失败了。
我的目标是从lambda上下文中获取别名作为环境配置。我已经阅读了大部分的互联网,但我仍然无法得到正确的。

beq87vna

beq87vna1#

我刚刚测试了这个,你的例子工作得很好。
这是一个.Net 6“AWS无服务器应用程序”,使用“ASP.NET核心Web应用程序”。
函数处理程序覆盖工作,我可以访问上下文。

vyswwuz2

vyswwuz22#

您可以利用:

ConcurrentDictionary<Guid, ILambdaContext>

您将在lambda处理程序 Package 器中添加到它,并在作用域结束时删除它。

// MyLambdaWrapper.cs
using Amazon.Lambda.Core;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Concurrent;

namespace MyNamespace;

public class MyLambdaWrapper
{
    private readonly ConcurrentDictionary<Guid, ILambdaContext> _contexts = new();

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddScoped(_ => new ScopeContextIdentifier(Guid.NewGuid()));
        services.AddScoped<ILambdaContext>(serviceProvider =>
        {
            var scopeContextIdentifier =
                serviceProvider.GetRequiredService<ScopeContextIdentifier>();
            var contexts =
                serviceProvider
                    .GetRequiredService<ConcurrentDictionary<Guid, ILambdaContext>>();
            var injected = contexts[scopeContextIdentifier.Guid];
            return injected;
        });
        services
            .AddSingleton<ConcurrentDictionary<Guid, ILambdaContext>>(_contexts);
    }

    public async Task Run(IServiceProvider serviceProvider, App app, Type handlerType)
    {
        Func<Stream, ILambdaContext, Task<Stream?>> func =
        async (inputStream, lambdaContext) =>
        {
            // ...
            using var scope = serviceProvider.CreateScope();
            using var _ =
                new ScopeContextManager(scope, lambdaContext, _contexts);
            // ...
        };
        // ...
    }
}

和管理器:

// ScopeContextManager.cs
using Amazon.Lambda.Core;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Concurrent;

namespace MyNamespace;

internal sealed record ScopeContextIdentifier(Guid Guid);

internal sealed class ScopeContextManager : IDisposable
{
    private readonly ConcurrentDictionary<Guid, ILambdaContext> _contexts;
    private readonly ScopeContextIdentifier _internalScopeContext;

    public ScopeContextManager(IServiceScope serviceScope, ILambdaContext lambdaContext, ConcurrentDictionary<Guid, ILambdaContext> ctx)
    {
        _internalScopeContext = serviceScope.ServiceProvider.GetRequiredService<ScopeContextIdentifier>();
        _contexts = ctx;
        _contexts.TryAdd(_internalScopeContext.Guid, lambdaContext);
    }

    public void Dispose()
    {
        _contexts.Remove(_internalScopeContext.Guid, out _);
    }
}
deyfvvtc

deyfvvtc3#

关闭此项目,目前无法取得内容

相关问题