問題:
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