简体   繁体   English

依赖注入网络核心控制台应用程序设置

[英]Dependency injection net core console application setup

I am trying to use dependency injection for a.Net Core Console application using the built in DI.我正在尝试使用内置 DI 对 .Net Core Console 应用程序使用依赖注入。

I have 2 following Methods:我有以下两种方法:

    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();
        }
    }

I can get this to work in the main method by using this:我可以通过使用以下方法使其在主要方法中工作:

private static IServiceProvider _serviceProvider;
private static IClientDataRepository _clientDataRepository;



static void Main(string[] args)
{

    RegisterServices();

    _clientDataRepository = _serviceProvider.GetService<IClientDataRepository>();

However I need to inject the repository to the service and the service to main but I can t use the following in the service class:但是我需要将存储库注入服务并将服务注入主服务,但我不能在服务 class 中使用以下内容:

_clientDataRepository = _serviceProvider.GetService<IClientDataRepository>();

Here is service:这里是服务:

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

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

If I use如果我使用

_clientDataRepository = _serviceProvider.GetService<IClientDataRepository>();

will give an error会报错

Just resolve the service and the service provider will inject the repository into the service when building the object graph of the requested object只需解析服务,服务提供者将在构建请求的 object 的 object 图时将存储库注入服务

Based on the provided ClientDataService you would also need to make sure that all dependencies are registered with the service collection.根据提供的ClientDataService ,您还需要确保所有依赖项都已注册到服务集合中。

As it is current shown, ClientDataService also depends on ILogger which does not appear to be registered with the service collection.如当前所示, ClientDataService还依赖于ILogger ,它似乎没有向服务集合注册。

services.AddLogging();

The following example uses the originally provided code and refactors to run the main using dependency injection.以下示例使用最初提供的代码和重构来使用依赖注入运行 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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 .NET Core 控制台应用程序中通过依赖注入访问配置 - Access configuration through dependency injection in .NET Core console application 如何在.net Core 2.2控制台应用程序中实现依赖项注入 - How to implement dependency injection in .net core 2.2 console application 如何在 .Net core 控制台应用程序中使用依赖注入 - How to use Dependency Injection in .Net core Console Application 如何在.Net核心控制台应用程序中实现依赖注入 - How to implement Dependency Injection in .Net core Console Application 控制台应用程序中的.Net核心依赖项注入 - .Net Core Dependency Injection in Console App 了解控制台应用程序中的.net Core依赖注入 - Understanding .net Core Dependency Injection in a console app 在 .NET 核心控制台应用程序中为 EF Core DbContext 服务错误建立依赖注入 - Established dependency injection in .NET Core Console Application for EF Core DbContext Service Error 在.NET Core Service应用程序中使用依赖注入 - Using Dependency Injection in .NET Core Service application .NET Core 2.2 Web应用程序。 DbContext是通过依赖注入设置的,可以在运行时更改它吗? - .NET Core 2.2 Web Application. DbContext was setup by Dependency Injection Can it be changed at Runtime? .NET Core 中的依赖注入 - Dependency injection in .NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM