Signalr example app from official documentation does not work on IIS

痞子三分冷 提交于 2019-12-12 03:47:03

问题


I've spent too much time trying to figure this out, and I'm starting to exhaust the threads here on SO that are dealing with this very issue. Feel free to link to other threads about this issue, but I've probably already consulted those with no success.

I'm following Microsoft's Signalr example app from this url: https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-getting-started-with-signalr

I'm running IIS 8.5 on Windows Server 2012 R2, SignalR 2.2.1, and .Net 4.5.2 and when I run the application from Visual Studio, this simple chat application runs as expected. As soon as I throw it up on IIS, it breaks (404 for generated proxy). Below is the code I'm working with and the error output.

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();
        }
    }
}

Browser Console Error:

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

My application started as an empty web application as instructed by the demo app I linked at the top of this post, so the script reference issues associated with MVC style applications should not apply here.

I've tried many solutions relevant to Signalr 2. Since I'm on IIS 8.5, it shouldn't be having an issue with extensionless urls. I also have a few suggested installed features on my IIS including WebSocket protocol, though I was told that wasn't 100% necessary. I've tried restarting the w3svc, recycling the application pool, and clearing the ASP.NET temp files cache as well.

Am I just messing this up?

UPDATE

With @klabranche's extensive help, I decided not to use the very basic sample tutorial that I linked to at the top of this question.

Instead, he suggested I try this tutorial here: https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-getting-started-with-signalr-and-mvc

Following this tutorial was enough to get the application running on IIS. I had to target .net 4.5 instead of 4.5.2 though.


回答1:


The 404 means your file is not being found by the web server. You moved from localhost to a server where the relativity of the link changed. In ASP.Net relative paths are represented by the tilde character (~/).

It looks to me like your script references are not relative and since you moved it to a server the location is not as expected.

Add ~/ to all your script paths.

instead of <script src="signalr/hubs"></script> try <script src="~/signalr/hubs"></script>

The docs use ~/ but unfortunately the sample does not. It would work localhost with IIS Express as you have expressed.

You can also generate the proxy file. Read "How to create a physical file for the SignalR generated proxy section on how to do that. You don't have to do this to get SignalR to work though but wanted to throw it out there as another option.



来源:https://stackoverflow.com/questions/42237137/signalr-example-app-from-official-documentation-does-not-work-on-iis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!