了解控制台应用程序中的.net Core依赖注入

时间:2019-05-04 14:29:53

标签: c# asp.net-core dependency-injection

控制台应用程序不像Web应用程序那样将启动文件与配置服务一起使用,我正在努力理解依赖注入的关键概念。

(请注意下面的示例无法编译)

以下是我认为它应该如何工作的一个基本示例(请务必指出任何非常规或错误的内容):

        static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddUserSecrets<Settings>()
                .Build();

            var services = new ServiceCollection()
                .AddLogging(b => b
                    .AddConsole())
                .AddDbContext<UnderstandingDIContext>(options =>
                    options.UseSqlite(builder.GetConnectionString("DefaultConnection")))
                .BuildServiceProvider();

            var logger = services.GetService<ILoggerFactory>()
                .CreateLogger<Program>();

            logger.LogInformation("Starting Application");

            var worker = new Worker();

            logger.LogInformation("Closing Application");
        }

但是如何在“工人”类中使用这些服务?

        public Worker(ILogger logger, IConfiguration configuration)
        {
            logger.LogInformation("Inside Worker Class");
            var settings = new Settings()
            {
                Secret1 = configuration["Settings:Secret1"],
                Secret2 = configuration["Settings:Secret2"]
            };
            logger.LogInformation($"Secret 1 is '{settings.Secret1}'");
            logger.LogInformation($"Secret 2 is '{settings.Secret2}'");

            using (var context = new UnderstandingDIContext())
            {
                context.Add(new UnderstandingDIModel()
                {
                    Message = "Adding a message to the database."
                });
            }
        }

UnderstandingDIContext

    public class UnderstandingDIContext : DbContext
    {
        public UnderstandingDIContext(DbContextOptions<UnderstandingDIContext> options)
            : base(options)
        { }

        public DbSet<UnderstandingDIModel> UnderstandingDITable { get; set; }
    }

此代码的问题如下:

Worker()希望传递ILogger和IConfiguration参数,但我认为依赖注入应该可以解决?

我无法运行“ dotnet ef迁移添加初始”,因为我没有正确传递连接字符串(错误:“无法创建类型为“ UnderstandingDIContext”的对象。”)

“使用(var context = new理解DIContext())”不会编译,因为我误解了DbContext位。

我搜索了很多内容,其中有很多针对Web应用程序的示例,而对于Console应用程序则很少。我只是完全误解了依赖注入的整个概念吗?

1 个答案:

答案 0 :(得分:1)

使用构造函数注入时,仅当通过 依赖注入本身实际创建要创建的对象时,才能解决依赖关系。因此,使依赖注入在您的Worker中起作用的关键是实际上也要通过依赖注入容器来解析Worker

这实际上很简单:

var services = new ServiceCollection()
    .AddLogging(b => b.AddConsole())
    .AddDbContext<UnderstandingDIContext>(options =>
        options.UseSqlite(builder.GetConnectionString("DefaultConnection")));

// register `Worker` in the service collection
services.AddTransient<Worker>();

// build the service provider
var serviceProvider = services.BuildServiceProvider();

// resolve a `Worker` from the service provider
var worker = serviceProvider.GetService<Worker>();

var logger = serviceProvider.GetService<ILogger<Program>>();
logger.LogInformation("Starting Application");

worker.Run();

logger.LogInformation("Closing Application");

此外,由于您使用的数据库上下文默认情况下已注册为 scoped 依赖项,因此我建议您也创建一个服务范围-或更改数据库的生存期您注册时的上下文。

var serviceProvider = services.BuildServiceProvider();

using (var scope = serviceProvider.CreateScope())
{
    var worker = serviceProvider.GetService<Worker>();
    worker.Run();
}

请注意,我还在您的工作人员上做了一个显式方法Run,以免构造函数中没有逻辑。

public class Worker
{
    private readonly ILogger<Worker> _logger = logger;
    private readonly IConfiguration _configuration = configuration;
    private readonly UnderstandingDIContext _dbContext = dbContext;

    public Worker(ILogger<Worker> logger, IConfiguration configuration, UnderstandingDIContext dbContext)
    {
        _logger = logger;
        _configuration = configuration;
        _dbContext = dbContext;
    }

    public void Run()
    {
        _logger.LogInformation("Inside Worker Class");
        var settings = new Settings()
        {
            Secret1 = configuration["Settings:Secret1"],
            Secret2 = configuration["Settings:Secret2"]
        };

        _logger.LogInformation($"Secret 1 is '{settings.Secret1}'");
        _logger.LogInformation($"Secret 2 is '{settings.Secret2}'");

        _dbContext.Add(new UnderstandingDIModel()
        {
            Message = "Adding a message to the database."
        });
        _dbContext.SaveChanges();
    }
}