我如何手动解析一个类型使用ASP。NET核心MVC内置依赖注入框架?

设置容器非常简单:

public void ConfigureServices(IServiceCollection services)
{
    // ...

    services.AddTransient<ISomeService, SomeConcreteService>();
}

但是如何在不执行注入的情况下解析ISomeService呢?例如,我想这样做:

ISomeService service = services.Resolve<ISomeService>();

在IServiceCollection中没有这样的方法。


当前回答

如果您只是为了将一个依赖项传递给正在注册的另一个依赖项的构造函数而解析它,那么您可以这样做。

假设您有一个接受字符串和ISomeService的服务。

public class AnotherService : IAnotherService
{
    public AnotherService(ISomeService someService, string serviceUrl)
    {
        ...
    }
}

当你在Startup.cs中注册这个时,你需要这样做:

services.AddScoped<IAnotherService>(ctx => 
      new AnotherService(ctx.GetService<ISomeService>(), "https://someservice.com/")
);

其他回答

如果您只是为了将一个依赖项传递给正在注册的另一个依赖项的构造函数而解析它,那么您可以这样做。

假设您有一个接受字符串和ISomeService的服务。

public class AnotherService : IAnotherService
{
    public AnotherService(ISomeService someService, string serviceUrl)
    {
        ...
    }
}

当你在Startup.cs中注册这个时,你需要这样做:

services.AddScoped<IAnotherService>(ctx => 
      new AnotherService(ctx.GetService<ISomeService>(), "https://someservice.com/")
);

您可以使用IApplicationBuilder实例以这种方式注入依赖项

public void Configure(IApplicationBuilder app)
{
    //---------- Your code

    using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
    {
        var resultLogic = serviceScope.ServiceProvider.GetService<IResultLogic>();
        resultLogic.YourMethod();
    }

    //---------- Your code
}

手动解析实例需要使用IServiceProvider接口:

在启动时解决依赖关系。ConfigureServices

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IMyService, MyService>();

    var serviceProvider = services.BuildServiceProvider();
    var service = serviceProvider.GetService<IMyService>();
}

解决启动中的依赖关系。配置

public void Configure(
    IApplicationBuilder application,
    IServiceProvider serviceProvider)
{
    // By type.
    var service1 = (MyService)serviceProvider.GetService(typeof(MyService));

    // Using extension method.
    var service2 = serviceProvider.GetService<MyService>();

    // ...
}

解决启动中的依赖关系。在ASP中配置。NET Core 3

public void Configure(
    IApplicationBuilder application,
    IWebHostEnvironment webHostEnvironment)
{
    application.ApplicationServices.GetService<MyService>();
}

使用运行时注入的服务

有些类型可以作为方法参数注入:

public class Startup
{
    public Startup(
        IHostingEnvironment hostingEnvironment,
        ILoggerFactory loggerFactory)
    {
    }

    public void ConfigureServices(
        IServiceCollection services)
    {
    }

    public void Configure(
        IApplicationBuilder application,
        IHostingEnvironment hostingEnvironment,
        IServiceProvider serviceProvider,
        ILoggerFactory loggerfactory,
        IApplicationLifetime applicationLifetime)
    {
    }
}

解决控制器动作中的依赖关系

[HttpGet("/some-action")]
public string SomeAction([FromServices] IMyService myService) => "Hello";
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    services.AddDbContext<ConfigurationRepository>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("SqlConnectionString")));

    services.AddScoped<IConfigurationBL, ConfigurationBL>();
    services.AddScoped<IConfigurationRepository, ConfigurationRepository>();
}

您可以通过这种方式在诸如AuthorizeAttribute之类的属性中注入依赖项

var someservice = (ISomeService)context.HttpContext.RequestServices.GetService(typeof(ISomeService));