【发布时间】:2017-03-20 12:20:20
【问题描述】:
如何在 ASP.NET Core 中使用 appsettings.json 为不同环境提供到 RavenDB 的连接设置? RavenDB 文档解释了如何使用 app.config 和 web.config 执行此操作,但我找不到 appsettings.json 的任何内容。
将IOptions<RavenSettings> 注入 DocumentStoreHolder 是正确的方法吗(这会是什么样子),还是有更好的选择?
到目前为止的代码如下:
appsettings.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"Raven": {
"Url": "http://localhost:8080",
"DefaultDatabase": "MyDatabase"
}
}
startup.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<IConfiguration>(Configuration);
services.Configure<RavenSettings>(Configuration.GetSection("Raven"));
}
DocumentStoreHolder.cs
public class DocumentStoreHolder
{
private static Lazy<IDocumentStore> docStore = new Lazy<IDocumentStore>(CreateStore);
public static IDocumentStore Store
{
get { return docStore.Value; }
}
private static IDocumentStore CreateStore()
{
IDocumentStore store = new DocumentStore()
{
Url = "http://localhost:8080",
DefaultDatabase = "MyDatabase"
}.Initialize();
return store;
}
}
【问题讨论】:
标签: c# asp.net-core ravendb