使用 ASP.NET RavenDB 连接的核心配置设置
Using ASP.NET Core configuration settings for RavenDB connections
如何使用 ASP.NET 核心中的 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;
}
}
最简单的方法是读取各个值并直接在 DocumentStore
上设置属性。在 3.x 中,RavenDB 不支持从 AppSettings.json
文件
中读取配置
一种不同的方法可能是使用连接字符串而不是单独的配置,因此您可以根据 asp net core 平台中预期的当前环境名称解析连接字符串,并一直向下传递连接字符串第一次初始化 DocumentStore。
private static IDocumentStore InitRavenDb(string connectionString)
{
var optsBuilder = ConnectionStringParser<RavenConnectionStringOptions>.FromConnectionString(connectionString);
optsBuilder.Parse();
var opts = optsBuilder.ConnectionStringOptions;
return new DocumentStore
{
Url = opts.Url,
ApiKey = opts.ApiKey,
DefaultDatabase = opts.DefaultDatabase,
}.Initialize(true);
}
var connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddSingleton(_ => InitRavenDb(connectionString));
如何使用 ASP.NET 核心中的 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;
}
}
最简单的方法是读取各个值并直接在 DocumentStore
上设置属性。在 3.x 中,RavenDB 不支持从 AppSettings.json
文件
一种不同的方法可能是使用连接字符串而不是单独的配置,因此您可以根据 asp net core 平台中预期的当前环境名称解析连接字符串,并一直向下传递连接字符串第一次初始化 DocumentStore。
private static IDocumentStore InitRavenDb(string connectionString)
{
var optsBuilder = ConnectionStringParser<RavenConnectionStringOptions>.FromConnectionString(connectionString);
optsBuilder.Parse();
var opts = optsBuilder.ConnectionStringOptions;
return new DocumentStore
{
Url = opts.Url,
ApiKey = opts.ApiKey,
DefaultDatabase = opts.DefaultDatabase,
}.Initialize(true);
}
var connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddSingleton(_ => InitRavenDb(connectionString));