SignalR not working on production server

2019-05-22 16:06发布

问题:

I am tring to utilize a simple SignalR sample and from a reason I get the 404 code from the following -

<script src="<%= ResolveUrl("~/signalr/hubs") %>" type="text/javascript"></script>

I checked the SignalR documentation and changed my web.config according to what is suggests there still I get that 404 status code.

my code follows -

Web.Config:

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </assemblies>
    </compilation>
    <httpRuntime/>
  </system.web>
  <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <modules runAllManagedModulesForAllRequests="true">
        </modules>
    </system.webServer>
</configuration>

Default.aspx:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.signalR-0.5.3.min.js" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/signalr/hubs") %>" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {

            //var connection = new Connection( "127.0.0.1");

            // Proxy created on the fly
            var chat = $.connection.chatt;

            // Declare a function on the chat hub so the server can invoke it
            chat.addMessage = function (message) {
                $('#messages').append('<li>' + message + '</li>');
            };



            $("#broadcast").click(function () {
                // Call the chat method on the server
                chat.send($('#msg').val());
            });

            // Start the connection
            $.connection.hub.start();
        });
    </script>
</head>

<input type="text" id="msg" />
<input type="button" id="broadcast" value="broadcast" />

<ul id="messages">
</ul>
</html>

Please assist.

回答1:

I had this same problem, the solution is putting this in your Global.asax file:

protected void Application_Start(object sender, EventArgs e)
{
    RouteTable.Routes.MapHubs();
}

Unfortunately this isn't documented anywhere, even though it's an essential part of SignalR configuration...



标签: iis-7 signalr