k.c*_*.c. 4 c# dependency-injection .net-core
我们正在.Net 2.0 Core 中开发一个类库。我们直接使用依赖注入。我们有一些服务想要从容器中解析为“Scoped”。
我们如何在代码中开始(和结束)这样的范围。如果能够在我们一些更复杂的测试场景中做到这一点,那就太好了。
注意:我们不使用“ASP.Net Core”
using Microsoft.Extensions.DependencyInjection; // it's an extension method
using (var scope = serviceProvider.CreateScope())
{
// in your new Scope, use the scope's ServiceProvider
var service = scope.ServiceProvider.GetService<SomeService>();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
989 次 |
| 最近记录: |