How could I make my RavenDB application execute properly when UseEmbeddedHttpServer is set to true using 2-tier architecture?

前端 未结 2 1489
终归单人心
终归单人心 2021-01-31 14:08

I used RavenDB-Embedded 2.0.2230 in my application interacted with ASP .Net Web API in different assemblies.

When I set UseEmbeddedHttpServer = tr

2条回答
  •  面向向阳花
    2021-01-31 14:40

    The only way I could reproduce the experience you describe is by intentionally creating a port conflict. By default, RavenDB's web server hosts on port 8080, so if you are not changing raven's port, then you must be hosting your WebApi application on port 8080. If this is not the case, please let me know in comments, but I will assume that it is so.

    All you need to do to change the port Raven uses is to modify the port value before calling Initialize method.

    Add this RavenConfig.cs file to your App_Startup folder:

    using Raven.Client;
    using Raven.Client.Embedded;
    
    namespace 
    {
        public static class RavenConfig
        {
            public static IDocumentStore DocumentStore { get; private set; }
    
            public static void Register()
            {
                var store = new EmbeddableDocumentStore
                            {
                                UseEmbeddedHttpServer = true,
    
                                DataDirectory = @"~\App_Data\RavenDatabase", 
                                // or from connection string if you wish
                            };
    
                // set whatever port you want raven to use
                store.Configuration.Port = 8079;
    
                store.Initialize();
                this.DocumentStore = store;
            }
    
            public static void Cleanup()
            {
                if (DocumentStore == null)
                    return;
    
                DocumentStore.Dispose();
                DocumentStore = null;
            }
        }
    }
    

    Then in your Global.asax.cs file, do the following:

    protected void Application_Start()
    {
        // with your other startup registrations
        RavenConfig.Register();
    }
    
    protected void Application_End()
    {
        // for a clean shutdown
        RavenConfig.Cleanup();
    }
    

提交回复
热议问题