c# - 当使用 2 层架构将 UseEmbeddedHttpServer 设置为 true 时,如何使我的 RavenDB 应用程序正确执行?

标签 c# web-applications asp.net-mvc-4 asp.net-web-api ravendb

我在我的应用程序中使用了 RavenDB-Embedded 2.0.2230 在不同的程序集中与 ASP .Net Web API 交互。

当我在文档存储上设置 UseEmbeddedHttpServer = true 时,我第一次向 RavenDB 发送请求时,它会正确执行,但是当我第二次尝试时,我的应用程序会显示 Raven Studio。

当我删除 UseEmbeddedServer 设置时,我的应用程序运行没有任何问题。

我的 RavenDB 在数据层配置了以下代码:

this.documentStore = new EmbeddableDocumentStore
{
    ConnectionStringName = "RavenDB",
    UseEmbeddedHttpServer = true
}.Initialize();

Web.config 的实现在服务层中有这些设置:

<connectionStrings>
    <add name="RavenDB" connectionString="DataDir=~\App_Data\RavenDatabase" />
</connectionStrings>

有没有我错过的设置?

是否需要应用任何设置才能将 Raven Studio 指向不同的端口?

最佳答案

我可以重现您描述的体验的唯一方法是故意制造端口冲突。默认情况下,RavenDB 的 Web 服务器托管在端口 8080 上,因此如果您不更改 raven 的端口,那么您必须在端口 8080 上托管您的 WebApi 应用程序。如果不是这种情况,请在评论中告诉我,但我会假设确实如此。

要更改 Raven 使用的端口,您需要做的就是在调用 Initialize 方法之前修改端口值。

将此 RavenConfig.cs 文件添加到您的 App_Startup 文件夹:

using Raven.Client;
using Raven.Client.Embedded;

namespace <YourNamespace>
{
    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;
        }
    }
}

然后在您的 Global.asax.cs 文件中,执行以下操作:

protected void Application_Start()
{
    // with your other startup registrations
    RavenConfig.Register();
}

protected void Application_End()
{
    // for a clean shutdown
    RavenConfig.Cleanup();
}

关于c# - 当使用 2 层架构将 UseEmbeddedHttpServer 设置为 true 时,如何使我的 RavenDB 应用程序正确执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13147077/

相关文章:

C# 创建对象 obj = new T()?

C#,写入二进制数据

c# - 程序集使用版本 X,其版本高于引用的程序集错误

java - eclipse中如何自动部署maven项目到tomcat

iphone - 在 iPhone WebApp 文本框中更改 "return key"

asp.net-mvc - 客户端列表的 MVC 自定义验证

asp.net-mvc-4 - 如何获取Jquery DataTable中隐藏字段的值

c# - Installer工程多次打包文件

c# - 如何将同一个应用程序部署到多个服务器

javascript - 每 5 秒从数据库获取数据并通过 AJAX 将其发送到 View