【问题标题】:how to configure dependencies in asp.net core如何在 asp.net core 中配置依赖项
【发布时间】:2020-01-27 17:31:28
【问题描述】:

我有一个 ASP.Net Core 2.1 应用程序。我需要注册并注入一些AWS 的依赖项。

目前,实现如下所示:

public abstract class BaseService
{
    protected readonly IConfiguration _configuration;
    protected readonly RegionEndpoint _region;
    protected readonly IAmazonDynamoDB _dynamoClient;
    protected  IPocoDynamo _pocoDynamo;

    public BaseService(IConfiguration configuration)
    {
        _configuration = configuration;
        var awsSettings = configuration.GetSection("AWS");
        _dynamoClient = SetDynamoClient(awsSettings);
        _pocoDynamo = SetPocoDynamoClient();
    }

    protected IAmazonDynamoDB SetDynamoClient(IConfigurationSection configuration)
    {
        AWSCredentials credentials = new BasicAWSCredentials(configuration["AccessKey"], configuration["AccessSecret"]);
        return new AmazonDynamoDBClient(credentials, _region);
    }

    protected IPocoDynamo SetPocoDynamoClient()
    {
        return new PocoDynamo(_dynamoClient);
    }
}

在进行单元测试时,不能因此模拟 AWS 服务。

我想在Startup.csConfigureServices()中注册所有这些依赖项

这是我正在尝试的:

public void ConfigureServices(IServiceCollection services)
{
    AWSCredentials credentials = new BasicAWSCredentials(configuration["AccessKey"], configuration["AccessSecret"]);

    services.AddTransient(IAmazonDynamoDB, (a) =>
         {
             return new AmazonDynamoDBClient(credentials, RegionEndpoint.GetBySystemName(""))
         });
    // here I need to pass the IAmazonDynamoDB to below IOC
    // services.AddSingleton<IPocoDynamo,new PocoDynamo()> ();

    return services;
}

但这会引发错误

错误 CS0119 'IAmazonDynamoDB' 是一种类型,在给定的上下文中无效

这里如何根据需要配置依赖?

谢谢!

【问题讨论】:

    标签: c# dependency-injection inversion-of-control ioc-container asp.net-core-2.1


    【解决方案1】:

    使用工厂委托调用注册的服务

    public void ConfigureServices(IServiceCollection services) {
        AWSCredentials credentials = new BasicAWSCredentials(configuration["AccessKey"], configuration["AccessSecret"]);
        services.AddTransient<IAmazonDynamoDB>(sp => 
            new AmazonDynamoDBClient(credentials, RegionEndpoint.GetBySystemName(""))
        );
    
        //here pass the IAmazonDynamoDB to below IOC
        services.AddSingleton<IPocoDynamo>(serviceProvider => {
            var pocoDynamo = new PocoDynamo(serviceProvider.GetRequieredService<IAmazonDynamoDB>());
            pocoDynamo.SomeMethod();
            return pocoDynamo;
        });
    }
    

    目标类不再需要依赖IConfiguration,因为可以通过构造函数注入显式注入依赖项。

    public abstract class BaseService {
        protected readonly IAmazonDynamoDB dynamoClient;
        protected readonly IPocoDynamo pocoDynamo;
    
        public BaseService(IAmazonDynamoDB dynamoClient, IPocoDynamo pocoDynamo) {        
            this.dynamoClient = dynamoClient;
            this.pocoDynamo = pocoDynamo;
        }
    }
    

    【讨论】:

    • 太好了.. 谢谢!!
    • 这里还需要一件事,我需要在DI之后使用IPocoDynamo的实例
    • services.AddSingleton&lt;IPocoDynamoAsync&gt;(di =&gt; new PocoDynamo(di.GetRequiredService&lt;IAmazonDynamoDB&gt;())); var pocoDynamo = //instance of IPocoDynamoAsync pocoDynamo.SomeMethod();
    • 谢谢!很抱歉,这听起来可能很荒谬,但您能否在这篇文章中查看并提出建议stackoverflow.com/questions/58138603/…
    猜你喜欢
    • 2018-02-11
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    相关资源
    最近更新 更多