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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章