将 IDocumentStore 设置为 RavenDB .net 中的静态变量
setting IDocumentStore as a static variable in RavenDB .net
我有一个 class 继承了 RavenTestDriver。
我需要第一次初始化 IDocumentStore。因为出于某种原因,我只需要这个 class 的一个对象。
所以这是我的代码:
public sealed class mystatic : RavenTestDriver
{
public static IDocumentStore store;
// here something like this store= GetDocumentStore()
public static IHostBuilder host = easy.api.Program.CreateHostBuilder(new string[0])
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder.UseTestServer();
}).ConfigureAppConfiguration(config =>
{
//config.AddJsonFile("appsettings.json", optional: true);
})
.ConfigureServices(services =>
{
services.AddScoped<ICurrentUserService, InitRequest>();
services.AddScoped<ICacheStorage>(provider =>
{
return new Mock<ICacheStorage>().Object;
});
services.AddTransient<IAsyncDocumentSession>((c) =>
{
return store.OpenAsyncSession();
});
});
public static IHost cli = host.Start();
}
我的问题是如何初始化存储变量??
或者如何在静态 class?
中使用 GetDocumentStore() 初始化商店
public class DocumentStoreHolder : RavenTestDriver
{
private IDocumentStore _store;
public IDocumentStore Store => _store;
public DocumentStoreHolder()
{
_store = GetDocumentStore();
}
}
public sealed class mystatic
{
public static readonly IDocumentStore _store;
static mystatic()
{
var storeHolder = new DocumentStoreHolder();
_store = storeHolder.Store;
}
}
我有一个 class 继承了 RavenTestDriver。 我需要第一次初始化 IDocumentStore。因为出于某种原因,我只需要这个 class 的一个对象。 所以这是我的代码:
public sealed class mystatic : RavenTestDriver
{
public static IDocumentStore store;
// here something like this store= GetDocumentStore()
public static IHostBuilder host = easy.api.Program.CreateHostBuilder(new string[0])
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder.UseTestServer();
}).ConfigureAppConfiguration(config =>
{
//config.AddJsonFile("appsettings.json", optional: true);
})
.ConfigureServices(services =>
{
services.AddScoped<ICurrentUserService, InitRequest>();
services.AddScoped<ICacheStorage>(provider =>
{
return new Mock<ICacheStorage>().Object;
});
services.AddTransient<IAsyncDocumentSession>((c) =>
{
return store.OpenAsyncSession();
});
});
public static IHost cli = host.Start();
}
我的问题是如何初始化存储变量?? 或者如何在静态 class?
中使用 GetDocumentStore() 初始化商店 public class DocumentStoreHolder : RavenTestDriver
{
private IDocumentStore _store;
public IDocumentStore Store => _store;
public DocumentStoreHolder()
{
_store = GetDocumentStore();
}
}
public sealed class mystatic
{
public static readonly IDocumentStore _store;
static mystatic()
{
var storeHolder = new DocumentStoreHolder();
_store = storeHolder.Store;
}
}