【问题标题】:Signalr example app from official documentation does not work on IIS来自官方文档的 Signalr 示例应用程序不适用于 IIS
【发布时间】:2017-02-14 22:06:35
【问题描述】:

我花了太多时间试图解决这个问题,而且我开始用尽 SO 上正在处理这个问题的线程。随意链接到有关此问题的其他线程,但我可能已经咨询过那些没有成功的人。

我正在通过以下网址关注 Microsoft 的 Signalr 示例应用程序:https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-getting-started-with-signalr

我在 Windows Server 2012 R2、SignalR 2.2.1 和 .Net 4.5.2 上运行 IIS 8.5,当我从 Visual Studio 运行应用程序时,这个简单的聊天应用程序按预期运行。一旦我把它扔到 IIS 上,它就会中断(生成的代理为 404)。下面是我正在使用的代码和错误输出。

index.html:

<!DOCTYPE html>
<html>
<head>
    <title>SignalR Simple Chat</title>
    <style type="text/css">
        .container {
            background-color: #99CCFF;
            border: thick solid #808080;
            padding: 20px;
            margin: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <input type="text" id="message" />
        <input type="button" id="sendmessage" value="Send" />
        <input type="hidden" id="displayname" />
        <ul id="discussion"></ul>
    </div>
    <!--Script references. -->
    <!--Reference the jQuery library. -->
    <script src="Scripts/jquery-3.1.1.min.js"></script>
    <!--Reference the SignalR library. -->
    <script src="Scripts/jquery.signalR-2.2.1.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="signalr/hubs"></script>
    <!--Add script to update the page and send messages.-->
    <script type="text/javascript">
        $(function () {
            // Declare a proxy to reference the hub.
            var chat = $.connection.chatHub;
            // Create a function that the hub can call to broadcast messages.
            chat.client.broadcastMessage = function (name, message) {
                // Html encode display name and message.
                var encodedName = $('<div />').text(name).html();
                var encodedMsg = $('<div />').text(message).html();
                // Add the message to the page.
                $('#discussion').append('<li><strong>' + encodedName
                    + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
            };
            // Get the user name and store it to prepend to messages.
            $('#displayname').val(prompt('Enter your name:', ''));
            // Set initial focus to message input box.
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub.
                    chat.server.send($('#displayname').val(), $('#message').val());
                    // Clear text box and reset focus for next comment.
                    $('#message').val('').focus();
                });
            });
        });
    </script>
</body>
</html>

ChatHub.cs:

using System;
using System.Web;
using Microsoft.AspNet.SignalR;

namespace SOChatApp
{
    public class ChatHub : Hub
    {
        public void Send(string name, string message)
        {
            // Call the broadcastMessage method to update clients.
            Clients.All.broadcastMessage(name, message);
        }
    }
}

Startup.cs:

using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
        }
    }
}

浏览器控制台错误:

Failed to load resource: the server responded with a status of 404 (Not Found) 指向http://sub.domain.com/MyApp/signalr/hubs

按照我在本文顶部链接的演示应用程序的说明,我的应用程序作为一个空的 Web 应用程序启动,因此与 MVC 样式应用程序相关的脚本引用问题不应适用于此。

我已经尝试了许多与 Signalr 2 相关的解决方案。由于我使用的是 IIS 8.5,因此无扩展名 url 应该没有问题。我的 IIS 上还有一些建议的安装功能,包括 WebSocket 协议,尽管有人告诉我这不是 100% 必要的。我试过重启 w3svc,回收应用程序池,以及清除 ASP.NET 临时文件缓存。

我是不是搞砸了?

更新

在@klabranche 的广泛帮助下,我决定不使用我在此问题顶部链接到的非常基本的示例教程。

相反,他建议我在这里尝试本教程:https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-getting-started-with-signalr-and-mvc

遵循本教程足以让应用程序在 IIS 上运行。 我不得不以 .net 4.5 而不是 4.5.2 为目标。

【问题讨论】:

标签: c# signalr asp.net-4.5 iis-8.5 signalr-2


【解决方案1】:

404 表示网络服务器未找到您的文件。您从 localhost 移动到链接的相对性发生变化的服务器。在 ASP.Net 中,相对路径由波浪号 (~/) 表示。

在我看来,您的脚本引用不是相对的,并且由于您将其移动到服务器,因此位置与预期不符。

将 ~/ 添加到所有脚本路径。

代替&lt;script src="signalr/hubs"&gt;&lt;/script&gt;试试script src="~/signalr/hubs"></script>

docs 使用 ~/ 但不幸的是示例没有。正如您所表达的,它可以与 IIS Express 一起使用 localhost。

您也可以generate the proxy file。阅读“如何为 SignalR 生成的代理创建物理文件部分,了解如何执行此操作。您不必这样做才能让 SignalR 工作,但想将其作为另一个选项。

【讨论】:

  • 我过去曾尝试过,但我只是仔细检查了一遍。这会导致针对 http://sub.domain/MyApp/~/signalr/hubs 的 404 - 我注意到将 ~/ 添加到脚本路径为一些运行 MVC 样式应用程序的人解决了问题,但我正在运行的这个示例应用程序不是 MVC。
  • ~/ 通常是 ASP.Net。适用于 WebForms 或 MVC。如果您实际上得到了 404 到 sub.domain/myapp/~/signalr/hubs,那么它并没有将 ~/ 解析为相对路径,而是将其用作实际路径。这对我说,您的应用程序没有在服务器上作为 ASP.Net 应用程序运行。您是否正确设置了 IIS 应用程序设置? IISExpress 和 Visual Studio 会自动为您执行此操作....
  • 它没有解析到相对路径,因为我实际上没有使用 .aspx 网络表单?所有客户端工作都在一个原始 html 文件 index.html
  • 但是集线器是由在运行时通过反射在编译时创建的 C# dll 生成的。如果您的应用程序未在 IIS 中设置为 .NET 应用程序,则不会创建集线器,也不会识别和正确处理 ~/ 语法。正如您在评论中所说,IIS 从字面上看是在寻找 ~ 的文件夹。此外,您的 c# hub 代码将永远不会被执行。不使用任何 webform 或 mvc 语法也没关系,也就是 - 只是原始 html。它仍然是一个 .Net 应用程序。
  • 例如,您可以生成您的代理,正如我在回答中提到的那样,并将您的链接更改为像普通的 javascript 文件一样指向它。但是,您会发现您的服务器端 C# 代码将永远不会执行,因为在我看来,此 IIS 文件夹/应用程序尚未设置为作为 .Net 应用程序运行。
猜你喜欢
  • 2022-06-15
  • 1970-01-01
  • 2015-11-10
  • 1970-01-01
  • 2012-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-16
相关资源
最近更新 更多