C# 为RavenDB连接使用ASP.NET核心配置设置
如何在ASP.NET core中使用appsettings.json为不同环境提供到RavenDB的连接设置?RavenDB文档解释了如何使用app.config和web.config执行此操作,但我找不到appsettings.json的任何内容 将C# 为RavenDB连接使用ASP.NET核心配置设置,c#,asp.net-core,ravendb,C#,Asp.net Core,Ravendb,如何在ASP.NET core中使用appsettings.json为不同环境提供到RavenDB的连接设置?RavenDB文档解释了如何使用app.config和web.config执行此操作,但我找不到appsettings.json的任何内容 将IOptions注入DocumentStoreHolder是正确的方法(这是什么样子的),还是有更好的选择 以下是迄今为止的代码: appsettings.json { "Logging": { "IncludeScopes": fal
IOptions
注入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"));
}
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;
}
}
public void配置服务(IServiceCollection服务)
{
...
services.AddSingleton(配置);
services.Configure(Configuration.GetSection(“Raven”));
}
DocumentStoreHolder.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<IConfiguration>(Configuration);
services.Configure<RavenSettings>(Configuration.GetSection("Raven"));
}
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;
}
}
公共类DocumentStoreHolder
{
private static Lazy docStore=new Lazy(CreateStore);
公共静态IDocumentStore
{
获取{return docStore.Value;}
}
私有静态IDocumentStore CreateStore()
{
IDocumentStore=新文档库()
{
Url=”http://localhost:8080",
DefaultDatabase=“MyDatabase”
}.Initialize();
退货店;
}
}
最简单的方法是读取单个值,并直接在文档库上设置属性。在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));
私有静态IDocumentStore InitRavenDb(字符串连接字符串)
{
var optsBuilder=ConnectionStringParser.FromConnectionString(connectionString);
Parse();
var opts=optsBuilder.ConnectionStringOptions;
返回新文档存储
{
Url=opts.Url,
ApiKey=opts.ApiKey,
DefaultDatabase=opts.DefaultDatabase,
}.初始化(true);
}
var connectionString=Configuration.GetConnectionString(“DefaultConnection”);
AddSingleton(=>InitRavenDb(connectionString));