SignalR 应用程序在 IIS 下无法工作。

5
我正在尝试在Visual Studio 2012中构建SignalR应用程序。我的问题是,在Visual Studio调试下(在Windows 7上使用Visual Studio 2012),它运行良好,但当我尝试在Windows Server 2012上的IIS 8上部署该应用程序时,该应用程序除了显示index.html页面外,什么也不做。
我决定尝试缩小问题范围,确定问题是在我的代码还是SignalR中。我编译了SignalR教程,网址为http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr,在Visual Studio下运行正常,但在IIS下只会显示静态页面,无法正常工作。
我已经尝试了一些方法,这里列出了Signalr/Hub not loading in IIS 7 but working correctly in Visual Studio,但它们似乎都没有起作用。

你在控制台上看到任何错误吗?打开日志记录($.connection.hub.logging = true)。 - N. Taylor Mullen
你的聊天页面叫做 index.html 吗?或者你的聊天页面叫做其他名字,而 index.html 只是新网站的默认 IIS 页面吗? - Andy Brown
有没有完整源代码的最终解决方案? - Kiquenet
2个回答

26
假设您实际上想查看index.html(即您的问题不是“空的index.html显示而不是我的聊天页面chat.html”),那么当您在聊天页面输入内容时,似乎其他连接到聊天的浏览器窗口中并没有显示出来。
我建议尝试一些基本测试。我假设:
  • ASP.NET已安装并配置在服务器上(如果没有,请参阅脚注)
  • SignalR库已部署到服务器上(Microsoft.AspNet.SignalR.Core.dllMicrosoft.AspNet.SignalR.Owin.dllMicrosoft.AspNet.SignalR.SystemWeb.dll和其他库)。
我会尝试以下测试:
  1. 安装Fiddler,或使用浏览器开发工具中的网络选项卡(在浏览器中按F12)。
  2. 浏览到yourdomainnamehere.com/index.html(如果您已经在那里,请重新加载)
  3. 网络跟踪应该显示以下状态为200(或可能为304):
    1. 您的页面index.html
    2. 下载jQuery、jQuery.signalR的javascript文件
  4. 它还应该显示以下关键连接状态为200:
    1. signalr/hubs
    2. signalr/negotiate(带查询字符串)
  5. 它应该显示与以下持续连接:
    1. signalr/connect(带查询字符串)
因此:
  • If you don't see 3.2 above, then you know the javascript files are not being served, so you need to find out why (are they there on the server/is the path correct in your html).
  • If you see that, but not 4.1 above, then there is a problem with the ASP.NET routing. Check that you have this as the first line in your Application_Start:

    protected void Application_Start(object sender, EventArgs e)
    {
        // Register the default hubs route: ~/signalr/hubs
        RouteTable.Routes.MapHubs();
    }
    
  • If you see that, but not 4.2 or 5.1 then there is a problem with your javascript that is preventing the $.connection.hub.start() code being called.

好的,现在怎么办?

现在您需要在signalr上打开客户端日志记录。在您的index.html聊天页面中,当您看到$.connection.hub.start().done(function () {这一行时,请添加以下内容,以使代码读取:

    $.connection.hub.logging = true;
    $.connection.hub.start().done(function () {` 

再次打开浏览器的开发工具,切换到控制台选项卡。现在加载页面并发送聊天消息。查看您是否收到任何错误消息。成功打开页面和发送聊天消息应该会生成以下日志:

LOG: [12:34:56 UTC+0100] SignalR: Negotiating with '/signalr/negotiate'. 
LOG: [12:34:56 UTC+0100] SignalR: This browser doesn't support SSE. 
LOG: [12:34:56 UTC+0100] SignalR: Binding to iframe's readystatechange event. 
LOG: [12:34:56 UTC+0100] SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332 and a connection lost timeout of 20000 
LOG: [12:34:56 UTC+0100] SignalR: Triggering client hub event 'broadcastMessage' on hub 'ChatHub'. 

注:如何测试服务器上是否已安装和配置ASP.NET

使用您的signalr端点网站,创建一个新页面:

  • 将其作为aspx网页表单(或mvc表单)并命名为TestAspNet.aspx
  • 在TestAspNet.aspx中添加标签控件:<asp:Label runat="server" ID="lblTest"></asp:Label>
  • 在代码后台中,将以下代码添加到Page_Load中:this.lblTest.Text = DateTime.Now.ToLongTimeString();

现在将其部署到您的Web服务器,并导航到http://yourdomainnamehere.com/TestAspNet.aspx。如果此操作向您显示当前服务器时间,则说明 ASP.NET 已安装在服务器上。否则,有两个选项:

如果您认为可能有另一个站点在使用主机头,则可以在 Win Server 2012 上使用 powershell 轻松检查此问题:

Import-Module WebAdministration
Get-WebBinding |? bindingInformation -match .*mydomainname.com.* | ft protocol, bindingInformation, ItemXPath -AutoSize

网站名称和ID显示在结果列下的ItemXPath


我也遇到了这个问题,我测试了你建议的步骤。它们都没问题,但是当我发送一条消息时,SignalR日志的最后一行“SignalR: Triggering client hub event…”没有显示出来。我不知道哪里出了问题! - Amir Oveisi
我解决了这个问题。那是因为我在服务器端逻辑上犯了错误。 - Amir Oveisi
1
@AmirOveisi,你能详细说明一下你服务器端逻辑的错误是什么吗?我遇到了类似的问题,尽管检查了上面的答案中的所有内容,但仍然无法找出原因。 - Bruno
@Bruno:就我所记得的,那是我在选择适当的客户端通过“Hub”更新它们时犯的错误。 - Amir Oveisi
谢谢,这是一个很棒的清单。在我的情况下,出现了一些问题。首先是由于不存在 signalR min 文件而导致 JS 文件无法提供服务,其次是需要在应用程序清单文件中添加信号连接所需的条目。 - TechnicalSmile

1

我遇到了同样的问题。通过将应用程序池设置为ASP.NET v4.0(v4.0)来解决它。

我还必须使用Chrome而不是IE才能使演示应用程序正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接