问题:
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