SignalR 不在本地 IIS 上工作,但在 IIS Express 上工作

SignalR not working on local IIS but working with IIS Express

我正在尝试编写一个小型 SignalR 项目。当项目在 IIS Express 下设置为 运行 时,一切正常。

但是当我在 Visual Studio 项目属性中切换到 "Local IIS" 并尝试 运行 时,我的页面加载但未连接到 SignalR 服务器。

我已经检查过 Fiddler,我发现虽然所有脚本都相对于路径正确加载,但 SignalR 调用是对网站的根目录进行的。所以在提琴手中我看到这样的东西:

3   304 HTTP    localhost   /TestApp/Scripts/jquery-1.10.2.min.js   0           microsoftedgecp:11004           
4   304 HTTP    localhost   /TestApp/Scripts/jquery.signalR-2.1.2.min.js    0           microsoftedgecp:11004           
6   404 HTTP    localhost   /signalr/negotiate?clientProtocol=1.4&connectionData=%5B%7B%22name%22%3A%22testapphub%22%7D%5D&_=1440092162315  4,958   private text/html; charset=utf-8    microsoftedgecp:11004           

如您所见,SignalR 称其在 /signalr 上进行协商,而项目 运行ning 在 /TestApp/

如何让它查看正确的位置(相对)?

编辑

这是我的 OwinStartup.cs:

public void Configuration(IAppBuilder app)
{
    app.MapSignalR();
}

我在 global.asax 中没有定义 - 我应该吗?

试试这个:

public void Configuration(IAppBuilder app)
    {
        var hubConfiguration = new HubConfiguration();
        hubConfiguration.EnableDetailedErrors = true;
        hubConfiguration.EnableJavaScriptProxies = true;

        app.MapSignalR("/TestApp", hubConfiguration);
    }

现在,这应该告诉您的应用程序将 SignalR 进程映射到您的 /TestApp 物理位置。根据我的经验,使用直接的 app.MapSignalR(); 大约有 30% 的时间有效,其余的,我必须使用像上面这样的显式位置声明。

我发现了一些让它起作用的东西。它与 OwinStartup 中的映射无关(经过测试我发现它正在被调用但在调试期间没有停止)。

深入了解 SignalR 的 js 源文件后,我发现这个函数初始化集线器连接:

function hubConnection(url, options) {
    /// <summary>Creates a new hub connection.</summary>
    /// <param name="url" type="String">[Optional] The hub route url, defaults to "/signalr".</param>
    /// <param name="options" type="Object">[Optional] Settings to use when creating the hubConnection.</param>
    var settings = {
        qs: null,
        logging: false,
        useDefaultPath: true
    };

    $.extend(settings, options);

    if (!url || settings.useDefaultPath) {
        url = (url || "") + "/signalr";
    }
    return new hubConnection.fn.init(url, settings);
}

如您所见,它需要一个 url 参数。因此,当我使用以下代码初始化集线器时:

var connection = $.hubConnection('/TestApp/');

现在可以了。现在我只是写了一个简单的服务器端来检查天气它应该初始化这个参数(如果它是 运行 在子目录中)并将值注入 HTML 页面。

developer82 的答案对我来说不太适用,但我没有足够的评论要点,所以我将其作为单独的答案提交。我需要像这样指定我的连接:

var connection = $.hubConnection('/TestApp/signalr');

其中 TestApp 是虚拟目录名称。请注意,我明确必须将 signalr 添加到最后。

我 运行 在切换到本地 IIS 时遇到同样的问题。 通过 'Manage NuGet Packages' 升级到 signalR-2.2.0 后,我确实阅读了这个 readme.txt:

Please see http://go.microsoft.com/fwlink/?LinkId=272764 for more information on using SignalR.

Upgrading from 1.x to 2.0 Please see http://go.microsoft.com/fwlink/?LinkId=320578 for more information on how to upgrade your SignalR 1.x application to 2.0.

Mapping the Hubs connection To enable SignalR in your application, create a class called Startup with the following:

using Microsoft.Owin;
using Owin; 
using MyWebApplication;

namespace MyWebApplication {
  public class Startup
  {
      public void Configuration(IAppBuilder app)
      {
          app.MapSignalR();
      }
  }   
} 

Getting Started See http://www.asp.net/signalr/overview/getting-started for more information on how to get started.

Why does ~/signalr/hubs return 404 or Why do I get a JavaScript error: 'myhub is undefined'? This issue is generally due to a missing or invalid script reference to the auto-generated Hub JavaScript proxy at '~/signalr/hubs'. Please make sure that the Hub route is registered before any other routes in your application.

In ASP.NET MVC 4 you can do the following:

  <script src="~/signalr/hubs"></script>

If you're writing an ASP.NET MVC 3 application, make sure that you are using Url.Content for your script references:

<script src="@Url.Content("~/signalr/hubs")"></script>

If you're writing a regular ASP.NET application use ResolveClientUrl for your script references or register them via the ScriptManager using a app root relative path (starting with a '~/'):

<script src='<%: ResolveClientUrl("~/signalr/hubs") %>'></script>

If the above still doesn't work, you may have an issue with routing and extensionless URLs. To fix this, ensure you have the latest patches installed for IIS and ASP.NET.


由于我正在处理常规 ASP.NET 应用程序,因此我必须更改此设置:

<script src="/Scripts/jquery-1.10.2.min.js" ></script>
<script src="/Scripts/jquery.signalR-2.1.2.js"></script>
<script src="/signalr/hubs"></script>

对此:

<script src='<%: ResolveClientUrl("~/Scripts/jquery-1.10.2.min.js") %'></script>
<script src='<%: ResolveClientUrl("~/Scripts/jquery.signalR-2.2.0.js") %>'></script>
<script src='<%: ResolveClientUrl("~/signalr/hubs") %>'></script>

之前我添加了一个 class 文件 "Startup.SignalR.cs":

using Owin;
using Microsoft.AspNet.SignalR;

namespace WebApplication1
{
    public partial class Startup
    {
        public void ConfigureSignalR(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
}

我的 Startup.cs 看起来像这样:

using Microsoft.Owin;
using Owin;

[assembly: OwinStartupAttribute(typeof(WebApplication1.Startup))]
namespace WebApplication1
{
    public partial class Startup {
        public void Configuration(IAppBuilder app) {
            //System.Diagnostics.Debugger.Break();

            ConfigureSignalR(app);
            ConfigureAuth(app);            
        }
    }
}

现在在项目/属性中我可以在 IIS Express 和本地 ISS 之间切换(用于调试/生产)。