【发布时间】:2017-03-30 10:43:21
【问题描述】:
我目前在为多个环境设置带有 dotnet 核心的 RavenDB 时遇到问题。
在 StartUp 类中,我将 Raven 配置为 Singleton,并使用 IOptions 模式将设置 Raven 绑定到 RavenSettings 对象。
public virtual void ConfigureServices(IServiceCollection services)
{
Services.AddMvc()
//Add functionality to inject IOptions<T>
services.AddOptions();
// App Settings
services.Configure<RavenSettings>(Configuration.GetSection("Raven"));
//services.Configure<RavenSettings>(settings => Configuration.GetSection("Raven").Bind(settings));
// .NET core built in IOC
services.AddSingleton(DocumentStoreHolder.Store);
services.AddSingleton<IConfiguration>(Configuration);
}
这是我的默认应用设置。
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"Raven": {
"Url": "x",
"DefaultDatabase": "x"
}
}
这是我尝试将设置从 appsettings 绑定到 ...
的类public class RavenSettings
{
public string Url { get; set; }
public string DefaultDatabase { get; set; }
}
下面的类在生成 Raven 文档存储时遵循 Raven 文档。因为我使用的是单例,所以我没有点击构造函数来注入设置。任何人都可以建议解决此问题的方法吗?
public sealed class DocumentStoreHolder
{
private static RavenSettings _ravenSettings;
public DocumentStoreHolder(IOptions<RavenSettings> ravenSettings)
{
_ravenSettings = ravenSettings.Value;
}
public static IDocumentStore Store => DocStore.Value;
private static readonly Lazy<IDocumentStore> DocStore = new Lazy<IDocumentStore>(CreateStore);
private static IDocumentStore CreateStore()
{
var store = new DocumentStore
{
Url = _ravenSettings.Url,
DefaultDatabase = _ravenSettings.DefaultDatabase
}.Initialize();
return store;
}
}
【问题讨论】:
标签: dependency-injection .net-core ravendb