Resolving instances with ASP.NET Core DI from within ConfigureServices

问题:

How do I manually resolve a type using the ASP.NET Core MVC built-in dependency injection framework?如何使用 ASP.NET Core MVC 内置依赖注入框架手动解析类型?

Setting up the container is easy enough:设置容器很容易:

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

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

But how can I resolve ISomeService without performing injection?但是如何在不执行注入的情况下解决ISomeService For example, I want to do this:例如,我想这样做:

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

There are no such methods in IServiceCollection . IServiceCollection中没有这样的方法。


解决方案:

参考一: https://en.stackoom.com/question/2CCF4
参考二: https://stackoom.com/question/2CCF4
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章