Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 官方文档中的Signalr示例应用程序在IIS上不起作用_C#_Signalr_Asp.net 4.5_Iis 8.5_Signalr 2 - Fatal编程技术网

C# 官方文档中的Signalr示例应用程序在IIS上不起作用

C# 官方文档中的Signalr示例应用程序在IIS上不起作用,c#,signalr,asp.net-4.5,iis-8.5,signalr-2,C#,Signalr,Asp.net 4.5,Iis 8.5,Signalr 2,我花了太多时间试图弄明白这一点,我开始用尽这里的线程,以便处理这个问题。请随意链接到关于这个问题的其他线程,但我可能已经咨询过那些没有成功的线程 我从以下url关注Microsoft的Signalr示例应用程序: 我正在Windows Server 2012 R2、SignalR 2.2.1和.Net 4.5.2上运行IIS 8.5,当我从Visual Studio运行该应用程序时,这个简单的聊天应用程序按预期运行。一旦我在IIS上抛出它,它就会崩溃(404表示生成的代理)。下面是我正在使用的代

我花了太多时间试图弄明白这一点,我开始用尽这里的线程,以便处理这个问题。请随意链接到关于这个问题的其他线程,但我可能已经咨询过那些没有成功的线程

我从以下url关注Microsoft的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>
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);
        }
    }
}
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();
        }
    }
}
Startup.cs:

<!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>
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);
        }
    }
}
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();
        }
    }
}
浏览器控制台错误:

<!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>
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);
        }
    }
}
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();
        }
    }
}
加载资源失败:服务器响应状态为404(未找到)
指向
http://sub.domain.com/MyApp/signalr/hubs

我的应用程序一开始是一个空的web应用程序,正如我在本文顶部链接的演示应用程序所指示的,因此与MVC风格的应用程序相关的脚本引用问题不应该在这里应用

我尝试过许多与信号机2相关的解决方案。因为我使用的是IIS 8.5,所以它不应该有无扩展URL的问题。我还建议在我的IIS上安装一些功能,包括WebSocket协议,尽管有人告诉我这不是100%必要的。我尝试重新启动w3svc,回收应用程序池,并清除ASP.NET临时文件缓存

我是不是把事情搞砸了

更新

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

相反,他建议我在这里尝试本教程:


遵循本教程足以使应用程序在IIS上运行。不过,我的目标是.net 4.5,而不是4.5.2。

404表示web服务器找不到您的文件。您已从localhost移动到链接相对性已更改的服务器。在ASP.Net中,相对路径由波浪号字符(
~/
)表示

在我看来,您的脚本引用不是相对的,因为您将其移动到了服务器上,所以位置不符合预期

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

不要使用
试试

使用~/但不幸的是,样本没有。正如您所表达的那样,它可以在本地主机上使用IIS Express


你也可以。阅读“如何为SignalR生成的代理创建物理文件”部分介绍了如何创建物理文件。你不必这样做就能让信号器工作,但你想把它作为另一种选择扔掉。

试试这里,我过去也试过,我只是反复检查了一下。这会导致404对
http://sub.domain/MyApp/~/signalr/hubs
——我注意到将~/添加到脚本路径解决了一些运行MVC风格应用程序的人的问题,但我运行的这个示例应用程序不是MVC。~/通常是ASP.Net。适用于WebForms或MVC。如果你真的得到了一个404 to,那么它不是将~/解析为相对路径,而是将它用作实际路径。这说明您的应用程序在服务器上没有作为ASP.Net应用程序运行。您是否正确设置了IIS应用程序设置?IISExpress&visualstudio会自动为您执行此操作……它不是因为我实际上没有使用.aspx web表单而解析为相对路径吗?所有客户端工作都在一个原始html文件index.htmlb中,但中心是由C#dll生成的,该dll是在运行时通过反射在编译时创建的。如果您的应用程序未在IIS中设置为.NET应用程序,则不会创建集线器,~/语法将无法识别和正确处理。在你的评论中,IIS实际上是在寻找一个~。此外,您的c#hub代码将永远不会执行。不使用任何webform或mvc语法(也称为原始html)并不重要。它仍然是一个.Net应用程序。例如,您可以像我在回答中提到的那样生成代理,并将链接更改为指向它,就像普通javascript文件一样。但是,您会发现您的服务器端C#代码将永远不会执行,因为我觉得此IIS文件夹/应用程序尚未设置为作为.Net应用程序运行。