将idocumentStore设置为ravendb .net中的静态变量

发布于 2025-01-20 14:02:25 字数 1167 浏览 2 评论 0原文

我有一个继承 RavenTestDriver 的类。 我第一次需要初始化 IDocumentStore。因为出于某种原因我只需要这个类的一个对象。 所以这是我的代码:

   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();
    }

我的问题是如何初始化商店变量? 或者如何在静态类中使用 GetDocumentStore() 初始化存储?

I have a class that inherits RavenTestDriver.
I need to initialize the IDocumentStore just for the first time . Because for some reason I need just one object of this class .
So here is my code :

   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();
    }

My question is how can I initialize the store variable ??
Or How can I initialize the store with GetDocumentStore() in static class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停顿的约定 2025-01-27 14:02:25
    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;

        }
}
    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;

        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文