首页 > 解决方案 > 依赖注入网络核心控制台应用程序设置

问题描述

我正在尝试使用内置 DI 对 .Net Core Console 应用程序使用依赖注入。

我有以下两种方法:

    private static void RegisterServices()
    {
        var collection = new ServiceCollection();

        //repositories
        collection.AddScoped<IAccountDataRepository, AccountDataRepository>();
        collection.AddScoped<IClientDataRepository, ClientDataRepository>();
        collection.AddScoped<IAddressDataRepository, AddressDataRepository>();
        collection.AddScoped<IClientAccountDataRepository, ClientAccountDataRepository>();
        //services
        collection.AddScoped<IAccountDataService, AccountDataService>();
        collection.AddScoped<IClientDataService, ClientDataService>();
        collection.AddScoped<IAddressDataService, AddressDataService>();
        collection.AddScoped<IClientAccountDataService, ClientAccountDataService>();

        _serviceProvider = collection.BuildServiceProvider();

    }

    private static void DisposeServices()
    {
        if (_serviceProvider == null)
        {
            return;
        }
        if (_serviceProvider is IDisposable)
        {
            ((IDisposable)_serviceProvider).Dispose();
        }
    }

我可以通过使用以下方法使其在主要方法中工作:

private static IServiceProvider _serviceProvider;
private static IClientDataRepository _clientDataRepository;



static void Main(string[] args)
{

    RegisterServices();

    _clientDataRepository = _serviceProvider.GetService<IClientDataRepository>();

但是我需要将存储库注入服务并将服务注入主服务,但我不能在服务类中使用以下内容:

_clientDataRepository = _serviceProvider.GetService<IClientDataRepository>();

这里是服务:

public class ClientDataService : IClientDataService
{
    private readonly ILogger _logger;
    private readonly IClientDataRepository _clientDataRepository;

    public ClientDataService(ILogger logger, IClientDataRepository clientDataRepository)
    {
        _logger = logger;
        _clientDataRepository = clientDataRepository;
    }

如果我使用

_clientDataRepository = _serviceProvider.GetService<IClientDataRepository>();

会报错

标签: c#dependency-injection.net-coreconsole-application

解决方案


只需解析服务,服务提供者将在构建请求对象的对象图时将存储库注入服务

根据提供的内容,ClientDataService您还需要确保所有依赖项都已在服务集合中注册。

如当前所示,ClientDataService还取决于ILogger哪个似乎未向服务集合注册。

services.AddLogging();

以下示例使用最初提供的代码和重构来使用依赖注入运行 main。

public class Program
    private readoonly IClientDataService service;

    public Program(IClientDataService service) {
        this.service = service;
    }

    public void SomeMethod() {
        //...
    }

    //entry
    public static void Main(string[] args) {
        IServiceProvider serviceProvider = RegisterServices();

        Program program = serviceProvider.GetService<Program>();

        program.SomeMethod();

        DisposeServices(serviceProvider);
    }

    //Support
    private static IServiceProvider RegisterServices() {
        var services = new ServiceCollection();

        //repositories
        services.AddScoped<IAccountDataRepository, AccountDataRepository>();
        services.AddScoped<IClientDataRepository, ClientDataRepository>();
        services.AddScoped<IAddressDataRepository, AddressDataRepository>();
        services.AddScoped<IClientAccountDataRepository, ClientAccountDataRepository>();
        //services
        services.AddScoped<IAccountDataService, AccountDataService>();
        services.AddScoped<IClientDataService, ClientDataService>();
        services.AddScoped<IAddressDataService, AddressDataService>();
        services.AddScoped<IClientAccountDataService, ClientAccountDataService>();
        services.AddLogging(); //<-- LOGGING
        //main
        services.AddScoped<Program>(); //<-- NOTE THIS

        return services.BuildServiceProvider();
    }

    private static void DisposeServices(IServiceProvider serviceProvider) {
        if (serviceProvider == null) {
            return;
        }
        if (serviceProvider is IDisposable sp) {
            sp.Dispose();
        }
    }
}

推荐阅读