Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用.NET Core配置带有AWS Lambda函数的DI容器?_C#_Amazon Web Services_.net Core_Aws Lambda - Fatal编程技术网

C# 如何使用.NET Core配置带有AWS Lambda函数的DI容器?

C# 如何使用.NET Core配置带有AWS Lambda函数的DI容器?,c#,amazon-web-services,.net-core,aws-lambda,C#,Amazon Web Services,.net Core,Aws Lambda,我想将DI容器连接到AWS lambda函数。如果有一个基类体系结构来促进AWS Lambda中的可靠原则,那就太好了。显然,没有startup.cs类或其他.NET核心初始化工具可用 这种方法将允许服务于更大的lambda函数的可测试部分 public class Function : Startup { private IFooService _fooService; public Function(IFooService fooService) {

我想将DI容器连接到AWS lambda函数。如果有一个基类体系结构来促进AWS Lambda中的可靠原则,那就太好了。显然,没有startup.cs类或其他.NET核心初始化工具可用

这种方法将允许服务于更大的lambda函数的可测试部分

public class Function : Startup
{
    private IFooService _fooService;
    
    public Function(IFooService fooService)
    {
        _fooService = fooService;   
    }

    public SkillResponse FunctionHandler(SkillRequest input, ILambdaContext context)
    {
       FooBar fooBar = _fooService.GetFooBar();
    }
}

我一直在使用AutoFac执行此操作,每次函数调用都会创建一个新的作用域:

public class Functions
{
    public static Lazy<ILifetimeScope> LifetimeScope { get; set; } = new Lazy<ILifetimeScope>(CreateContainer);

    private static ILifetimeScope CreateContainer()
    {
        var containerBuilder = new ContainerBuilder();
        containerBuilder.RegisterType<ServerAbc>()
            .AsImplementedInterfaces();

        return containerBuilder.Build();
    }

    /// <summary>
    /// A Lambda function
    /// </summary>
    public async Task Handle(ILambdaContext context)
    {
        using (var innerScope = LifetimeScope.Value.BeginLifetimeScope())
        {
            var service = innerScope.Resolve<IServerAbc>();

            await service.GoDoWork()
                .ConfigureAwait(false);
        }
    }
}
公共类函数
{
公共静态惰性生命周期范围{get;set;}=new Lazy(CreateContainer);
私有静态ILifetimeScope CreateContainer()
{
var containerBuilder=新的containerBuilder();
containerBuilder.RegisterType()
.a实现接口();
返回containerBuilder.Build();
}
/// 
///Lambda函数
/// 
公共异步任务句柄(ILambdaContext上下文)
{
使用(var innerScope=LifetimeScope.Value.BeginLifetimeScope())
{
var service=innerScope.Resolve();
等待服务
.配置等待(错误);
}
}
}
publicstaticlazy
也是这样,我可以在测试中模拟它

[Fact]
public async Task ShouldMostLikelyWork()
{
    var lifetimeScope = new Mock<ILifetimeScope>();
    lifetimeScope.Setup(x => x.Resolve<IServerAbc>()).Returns(new MockService());

    Functions.LifetimeScope = new Lazy<ILifetimeScope>(() => lifetimeScope.Object);
    var functions = new Functions();

    await functions.Handle(Mock.Of<ILambdaContext>())
        .ConfigureAwait(false);
}
[事实]
公共异步任务ShouldMostLikelyWork()
{
var lifetimeScope=new Mock();
Setup(x=>x.Resolve()).Returns(newmockservice());
Functions.LifetimeScope=newlazy(()=>LifetimeScope.Object);
var函数=新函数();
wait functions.Handle(Mock.Of())
.配置等待(错误);
}

我一直在用AutoFac执行此操作,每次函数调用都会创建一个新的作用域:

public class Functions
{
    public static Lazy<ILifetimeScope> LifetimeScope { get; set; } = new Lazy<ILifetimeScope>(CreateContainer);

    private static ILifetimeScope CreateContainer()
    {
        var containerBuilder = new ContainerBuilder();
        containerBuilder.RegisterType<ServerAbc>()
            .AsImplementedInterfaces();

        return containerBuilder.Build();
    }

    /// <summary>
    /// A Lambda function
    /// </summary>
    public async Task Handle(ILambdaContext context)
    {
        using (var innerScope = LifetimeScope.Value.BeginLifetimeScope())
        {
            var service = innerScope.Resolve<IServerAbc>();

            await service.GoDoWork()
                .ConfigureAwait(false);
        }
    }
}
公共类函数
{
公共静态惰性生命周期范围{get;set;}=new Lazy(CreateContainer);
私有静态ILifetimeScope CreateContainer()
{
var containerBuilder=新的containerBuilder();
containerBuilder.RegisterType()
.a实现接口();
返回containerBuilder.Build();
}
/// 
///Lambda函数
/// 
公共异步任务句柄(ILambdaContext上下文)
{
使用(var innerScope=LifetimeScope.Value.BeginLifetimeScope())
{
var service=innerScope.Resolve();
等待服务
.配置等待(错误);
}
}
}
publicstaticlazy
也是这样,我可以在测试中模拟它

[Fact]
public async Task ShouldMostLikelyWork()
{
    var lifetimeScope = new Mock<ILifetimeScope>();
    lifetimeScope.Setup(x => x.Resolve<IServerAbc>()).Returns(new MockService());

    Functions.LifetimeScope = new Lazy<ILifetimeScope>(() => lifetimeScope.Object);
    var functions = new Functions();

    await functions.Handle(Mock.Of<ILambdaContext>())
        .ConfigureAwait(false);
}
[事实]
公共异步任务ShouldMostLikelyWork()
{
var lifetimeScope=new Mock();
Setup(x=>x.Resolve()).Returns(newmockservice());
Functions.LifetimeScope=newlazy(()=>LifetimeScope.Object);
var函数=新函数();
wait functions.Handle(Mock.Of())
.配置等待(错误);
}