【发布时间】:2017-02-02 21:43:13
【问题描述】:
我是 ASP.NET Core 的新手,需要一些指导。我试图找出一个简单的场景,在 ASP.NET Core 中使用应用程序设置,同时还使用 Simple Injector。
我首先按照 Rick Strahl 的 here 解释设置了我的强类型配置设置。这很好用。
public class MySettings
{
public string ApplicationName { get; set; }
}
appsetting.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"MySettings": {
"ApplicationName": "Test Service"
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
// Add Options
services.AddOptions();
// Add our Config object so it can be injected
services.Configure<MySettings>(Configuration.GetSection("MySettings"));
services.AddSingleton(GetTestBroker(container));
//Simple Injector
//services.AddSingleton<IControllerActivator>(new SimpleInjectorControllerActivator(container));
//services.AddSingleton<IViewComponentActivator>(new SimpleInjectorViewComponentActivator(container));
}
在我们的其他项目中,我们正在使用 Simple Injector for DI。添加 Simple Injector 包并按照说明对其进行配置后,我发现我的 IOptions 配置中断。
我想知道的是,在 ASP.NET Core 中实现配置并同时使用另一个 DI 库(如 Simple Injector)的最佳实践是什么?
【问题讨论】:
-
这里描述了使用 IOptions 的最佳实践:github.com/simpleinjector/SimpleInjector/issues/…
-
我去看看。
-
@Steven 看过那篇文章后,似乎完全放弃 IOptions 是共识。然后只需将您的 POCO 注册为您的 DI 容器中的单例。这就是你的意思吗?
-
绝对没有任何理由让任何应用程序组件依赖
IOption<T>。您绝对应该将配置对象直接注入到您的类中。
标签: c# asp.net-core simple-injector