相关疑难解决方法(0)

使用ASP.NET Core DI解析实例

如何使用ASP.NET Core MVC内置依赖注入框架手动解析类型?

设置容器很容易:

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

    services.AddTransient<ISomeService, SomeConcreteService>();
}
Run Code Online (Sandbox Code Playgroud)

但是如何在ISomeService不进行注射的情况下解决?例如,我想这样做:

ISomeService service = services.Resolve<ISomeService>();
Run Code Online (Sandbox Code Playgroud)

没有这样的方法IServiceCollection.

c# dependency-injection asp.net-core-mvc asp.net-core

245
推荐指数
6
解决办法
17万
查看次数

如何在ASP.NET Core中解析ConfigureServices中的实例

是否可以IOptions<AppSettings>ConfigureServicesStartup中的方法解析实例?通常,您可以使用IServiceProvider初始化实例,但在注册服务时此阶段没有实例.

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<AppSettings>(
        configuration.GetConfigurationSection(nameof(AppSettings)));

    // How can I resolve IOptions<AppSettings> here?
}
Run Code Online (Sandbox Code Playgroud)

c# inversion-of-control asp.net-core-mvc asp.net-core

67
推荐指数
3
解决办法
3万
查看次数