简体   繁体   中英

Register multiple services in .net core 2.0

I have defined two services.

public class FirstService : IFirstService
{
}
public class SecondService : ISecondService
{
}

Each of the corresponding service interfaces implements main service interface IService.

public interface IFirstService : IService
{
}
public interface ISecondService : IService
{
}
public interface IService
{
}

To register and use each of the services injecting via DI I have to register each of them like this.

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<IFirstService, FirstService>();
    services.AddScoped<ISecondService, SecondService>();
}

Is there any way to register all services in another way using IService. I want to avoid typing each defined service.

You can use Autofac library https://autofac.org/ and reflection to register all services. Here you have Autofac's documentation for that: https://autofaccn.readthedocs.io/en/latest/register/scanning.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM