c# - 迁移到服务器后 SignalR 不工作

标签 c# asp.net-mvc iis signalr

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

但是,自从移动到运行 IIS 7 的测试服务器后,signalR 不再有效。 webservice和website在同一台服务器上,website在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>

最佳答案

这有点摸不着头脑,但可能是因为它希望 SignalR 默认监听端口 80,而您正在端口 8088 上运行站点。或者 SignalR 仍在监听端口80 而您的站点在 8088 上?

也许设置连接 URL 可以解决问题?

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

关于c# - 迁移到服务器后 SignalR 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34205177/

相关文章:

c# - 使用 C# 通过网络发送文件的最佳方式是什么?

asp.net - ASP.NET 最大请求大小的临时覆盖

ajax - 在 ajax 请求中下载文件而不会遇到长查询字符串

c# - 我在 IIS 中的站点中遇到错误,未找到 Microsoft.EnterpriseManagement.OperationsManager.Apm.DataCollecting.Producers.Mvc.4.0

当另一个 HTML 页面向其提交表单时,HTML 页面抛出 405 错误

c# - 请告诉我 c#.net 中 "default"关键字的用途

c# - 当前安装不支持该项目类型

c# - ASP.NET MVC C# Razor SQL 1 of 2 几乎相同的更新查询不起作用

windows - 在 Windows/NTFS 上,可以将符号链接(symbolic link)移动到另一台计算机吗?

c# - 解析日志文件并获取条目数据