【发布时间】: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>: ' + 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