自迁移到服务器后,SignalR无法正常工作

时间:2015-12-10 15:01:55

标签: c# asp.net-mvc iis signalr

我构建了一个处理通知请求的Web服务和一个使用SignalR接收推送通知的网站。当使用Visual Studio和VS用于运行项目的任何Web服务器在我的盒子上运行web服务和网站时,这一切都运行良好。

但是,由于转移到运行IIS 7的测试服务器,signalR不再有效。 webservice和网站都在同一台服务器上,网站在端口8088上,webservice在端口8089上。

这是我得到的错误

10-12-2015 14:56:26,864 [UK\!kerslaj1][35] ERROR Centrica.CE.SEFlex.Common.Logging.ConsoleLogger - There was an error opening the connection 'http://localhost:8088/'
Microsoft.AspNet.SignalR.Client.HttpClientException: StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Connection: close
  Date: Thu, 10 Dec 2015 14:56:26 GMT
  Server: Microsoft-HTTPAPI/2.0
  Content-Length: 334
  Content-Type: text/html; charset=us-ascii
}
   at Microsoft.AspNet.SignalR.Client.Http.DefaultHttpClient.<>c__DisplayClass2.<Get>b__1(HttpResponseMessage responseMessage)
   at Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners`2.<>c__DisplayClass42.<RunTask>b__41(Task`1 t)
10-12-2015 14:56:26,866 [UK\!kerslaj1][41] DEBUG Centrica.CE.SEFlex.Common.Logging.ConsoleLogger - Connection started
10-12-2015 14:56:26,866 [UK\!kerslaj1][41] DEBUG Centrica.CE.SEFlex.Common.Logging.ConsoleLogger - User: kerslaj1
10-12-2015 14:56:26,866 [UK\!kerslaj1][41] DEBUG Centrica.CE.SEFlex.Common.Logging.ConsoleLogger - Added username: UK\!kerslaj1
10-12-2015 14:56:26,867 [UK\!kerslaj1][41] ERROR Centrica.CE.SEFlex.Common.Logging.ConsoleLogger - There was an error notifying using the connection http://localhost:8088/
System.InvalidOperationException: Data cannot be sent because the connection is in the disconnected state. Call start before sending any data.
   at Microsoft.AspNet.SignalR.Client.Connection.Send(String data)
   at Microsoft.AspNet.SignalR.Client.Hubs.HubProxy.Invoke[TResult,TProgress](String method, Action`1 onProgress, Object[] args)
   at Microsoft.AspNet.SignalR.Client.Hubs.HubProxy.Invoke[T](String method, Object[] args)
   at Centrica.CE.SEFlex.Common.Notification.SignalRHandler.Notify(EventNotification notification, IEnumerable`1 subscribers, String hubConnection) in c:\Development\TFS\Atlas\SE\DEV\SEFLEX\Build\Common\Centrica.CE.SEFlex.Common\Notification\SignalRHandler.cs:line 66
10-12-2015 14:56:26,868 [UK\!kerslaj1][41] DEBUG SEFlex - Subscriber notified

在我的网站上,SignalR配置为

 public class OwinStartup
    {
        public void Configuration(IAppBuilder app)
        {
            var container = new UnityContainer();
            container.RegisterType<NotificationHub, NotificationHub>(new ContainerControlledLifetimeManager());
            GlobalHost.DependencyResolver.Register(typeof(IHubActivator), () => new UnityHubActivator(container));

            var idProvider = new PrincipalUserIdProvider();
            GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => idProvider);

            // Any connection or hub wire up and configuration should go here            
            app.MapSignalR();
        }
    }

我的布局html页面

@Scripts.Render("~/bundles/signalr")
    <script src="/signalr/hubs"></script>
        <script>

        var notifyProxy = $.connection.notification, // the generated client-side hub proxy
            $notificationTable = $('#NotificationTable'),
            $notificationTableBody = $notificationTable.find('tbody'),
            rowTemplate = '<tr data-symbol="{EventNotificationId}"><td style="text-align:left;width:115px;vertical-align:text-top;"><nobr>{EventDate} : </nobr></td><td><span class="{Class}"><small class="text-uppercase">{ClassLabel}</small></span></td><td style="text-align:left;">{Description}</td></tr>';

        function formatNotification(notification) {
            return $.extend(notification, {
                EventDate: notification.EventTime.substr(0, 10).concat(' ').concat(notification.EventTime.substr(11, 8)),
                Description: notification.FriendlyText,
                Class: notification.Class                
            });
        }

        function init() {
            notifyProxy.server.getCurrentNotifications().done(function (notifications) {
                $notificationTableBody.empty();
                $.each(notifications, function () {
                    var notification = formatNotification(this);
                    $notificationTableBody.prepend(rowTemplate.supplant(notification));
                });
            });
        }

        // Add a client-side hub method that the server will call
        notifyProxy.client.addNotification = function (notification) {
            $notificationTableBody.prepend(rowTemplate.supplant(formatNotification(notification)));
        };

        // Start the connection
        $.connection.hub.start().done(init);

    </script>

1 个答案:

答案 0 :(得分:0)

这在黑暗中有点刺痛,但可能是它希望SignalR默认在端口80上侦听,并且你在端口8088上运行你的站点。或者SignalR仍在侦听端口80,您的网站是8088?

也许设置连接URL可能会有效?

$.connection.hub.url = "http://[HOST URL HERE]:8080/signalr";