使用 RavenDB 设置 dotnet 核心
Setting up dotnet core with RavenDB
我目前在为多个环境设置带有 dotnet 核心的 RavenDB 时遇到问题。
在 StartUp class 我已经将 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 中的设置绑定到 ...
的 class
public class RavenSettings
{
public string Url { get; set; }
public string DefaultDatabase { get; set; }
}
下面的class按照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;
}
}
ASP.NET Core DI 已经使用延迟加载 singleton:
Singleton lifetime services are created the first time they are requested (or when ConfigureServices is run if you specify an instance there) and then every subsequent request will use the same instance.
此外:
If your application requires singleton behavior, allowing the services container to manage the service's lifetime is recommended instead of implementing the singleton design pattern and managing your object's lifetime in the class yourself.
因此,您可以这样做:
services.AddSingleton<DocumentStoreHolder>();
通过从 DocumentStoreHolder
class
中删除您自己的单例实现
public sealed class DocumentStoreHolder
{
public DocumentStoreHolder(IOptions<RavenSettings> ravenSettings)
{
var _ravenSettings = ravenSettings.Value;
Store = new DocumentStore
{
Url = _ravenSettings.Url,
DefaultDatabase = _ravenSettings.DefaultDatabase
}.Initialize();
}
public IDocumentStore Store {get; private set;}
}
基于 Set 的回答,我能够通过创建 IDocumentStoreHolder 接口并在 IOC 中注册来解决它。
DocumentStoreHolder.cs
public class DocumentStoreHolder : IDocumentStoreHolder
{
public DocumentStoreHolder(IOptions<RavenSettings> ravenSettings)
{
var settings = ravenSettings.Value;
Store = new DocumentStore
{
Url = settings.Url,
DefaultDatabase = settings.DefaultDatabase
}.Initialize();
}
public IDocumentStore Store { get; }
}
我DocumentStoreHolder.cs
public interface IDocumentStoreHolder
{
IDocumentStore Store { get; }
}
Startup.cs(配置服务)
services.AddSingleton<IDocumentStoreHolder, DocumentStoreHolder>();
SomeClass.cs
private readonly IDocumentStore _store;
public SomeClass(IDocumentStoreHolder documentStoreHolder)
{
_store = documentStoreHolder.Store;
}
我目前在为多个环境设置带有 dotnet 核心的 RavenDB 时遇到问题。
在 StartUp class 我已经将 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 中的设置绑定到 ...
的 classpublic class RavenSettings
{
public string Url { get; set; }
public string DefaultDatabase { get; set; }
}
下面的class按照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;
}
}
ASP.NET Core DI 已经使用延迟加载 singleton:
Singleton lifetime services are created the first time they are requested (or when ConfigureServices is run if you specify an instance there) and then every subsequent request will use the same instance.
此外:
If your application requires singleton behavior, allowing the services container to manage the service's lifetime is recommended instead of implementing the singleton design pattern and managing your object's lifetime in the class yourself.
因此,您可以这样做:
services.AddSingleton<DocumentStoreHolder>();
通过从 DocumentStoreHolder
class
public sealed class DocumentStoreHolder
{
public DocumentStoreHolder(IOptions<RavenSettings> ravenSettings)
{
var _ravenSettings = ravenSettings.Value;
Store = new DocumentStore
{
Url = _ravenSettings.Url,
DefaultDatabase = _ravenSettings.DefaultDatabase
}.Initialize();
}
public IDocumentStore Store {get; private set;}
}
基于 Set 的回答,我能够通过创建 IDocumentStoreHolder 接口并在 IOC 中注册来解决它。
DocumentStoreHolder.cs
public class DocumentStoreHolder : IDocumentStoreHolder
{
public DocumentStoreHolder(IOptions<RavenSettings> ravenSettings)
{
var settings = ravenSettings.Value;
Store = new DocumentStore
{
Url = settings.Url,
DefaultDatabase = settings.DefaultDatabase
}.Initialize();
}
public IDocumentStore Store { get; }
}
我DocumentStoreHolder.cs
public interface IDocumentStoreHolder
{
IDocumentStore Store { get; }
}
Startup.cs(配置服务)
services.AddSingleton<IDocumentStoreHolder, DocumentStoreHolder>();
SomeClass.cs
private readonly IDocumentStore _store;
public SomeClass(IDocumentStoreHolder documentStoreHolder)
{
_store = documentStoreHolder.Store;
}