SignalR,可以调用服务器端方法,但客户端方法不能,只能在生产环境中调用

时间:2015-12-16 11:17:03

标签: asp.net c#-4.0 signalr signalr-hub signalr.client

我已成功为.net网站上的简报发送功能开发了一个中心。 集线器调用服务器过程,然后在发送例程期间,我发送客户端方法来报告进度状态,最后发送另一个客户端方法来报告例程的结束。

我的开发环境是使用IIS 10,VS2013,.NET 4.5 SignalR 2.2.0的win10。 我可以在我的开发中使用websocket并且工作正常,但我的生产服务器是win 2008r2(IIS 7.5),所以我必须使用serverSentEvents,也可以在dev上工作。

这是我的客户代码:

$.connection.hub.logging = true;
hubConn = $.connection.newsletterHub;
hubConn.client.addProgress = function (perc, label) {
    UpdateProgress(perc, label)
}
hubConn.client.raiseError = function (message) {
    alert(message);
}
hubConn.client.finishSent = function (D) {
    Sent(D);
}
hubConn.client.notify = function (msg) {
    console.log(msg);
}
$.connection.hub.start({ transport: ['serverSentEvents'] }).done(function () {
    ishubdone = true;
});

我的Hub课程:

public class NewsletterHub : Hub, IRequiresSessionState
{
    public void DoSendReal()
    {
        // other stuff
        foreach (string mre in tosend_Manual)
        {
            // other stuff
            Clients.Caller.addProgress(((decimal)cur / (decimal)tc), string.Format("{0}Emails", cur));
        }
        // other stuff
        Clients.Caller.finishSent(R.ToString());
    }
}

我的Owin Startup:

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
using Microsoft.AspNet.SignalR;

[assembly: OwinStartup(typeof(Shopper.OwinStartup))]

public static partial class Shopper
{
    public class OwinStartup
    {
        public void Configuration(IAppBuilder app)
        {
            var hubConfiguration = new HubConfiguration();
            hubConfiguration.EnableDetailedErrors = true;
            app.MapSignalR();
        }
    }
}

这是chrome log in开发环境,所有工作正常,服务器调用,客户端方法触发:

[13:01:18 GMT+0100] SignalR: Client subscribed to hub 'newsletterhub'.
[13:01:18 GMT+0100] SignalR: Negotiating with '/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22newsletterhub%22%7D%5D'.
[13:01:18 GMT+0100] SignalR: serverSentEvents transport starting.
[13:01:18 GMT+0100] SignalR: Attempting to connect to SSE endpoint 'http://barzo.topten/signalr/connect?transport=serverSentEvents&clientProtoc…VOMDrrj&connectionData=%5B%7B%22name%22%3A%22newsletterhub%22%7D%5D&tid=10'.
[13:01:18 GMT+0100] SignalR: EventSource connected.
[13:01:18 GMT+0100] SignalR: serverSentEvents transport connected. Initiating start request.
[13:01:18 GMT+0100] SignalR: The start request succeeded. Transitioning to the connected state.
[13:01:18 GMT+0100] SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332, keep alive timeout of 20000 and disconnecting timeout of 30000
[13:01:26 GMT+0100] SignalR: Invoking newsletterhub.DoSend
[13:01:30 GMT+0100] SignalR: Triggering client hub event 'addProgress' on hub 'NewsletterHub'.
[13:01:30 GMT+0100] SignalR: Triggering client hub event 'finishSent' on hub 'NewsletterHub'.
[13:01:30 GMT+0100] SignalR: Invoked newsletterhub.DoSend

在生产环境中,只进行服务器调用,程序正常(发送电子邮件),但不触发客户端方法:

[12:58:38 GMT+0100] SignalR: Client subscribed to hub 'newsletterhub'.
[12:58:38 GMT+0100] SignalR: Negotiating with '/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22newsletterhub%22%7D%5D'.
[12:58:38 GMT+0100] SignalR: serverSentEvents transport starting.
[12:58:38 GMT+0100] SignalR: Attempting to connect to SSE endpoint 'http://www.topten-italia.com/signalr/connect?transport=serverSentEvents&cli…v5PQ4%3D&connectionData=%5B%7B%22name%22%3A%22newsletterhub%22%7D%5D&tid=9'.
[12:58:38 GMT+0100] SignalR: EventSource connected.
[12:58:38 GMT+0100] SignalR: serverSentEvents transport connected. Initiating start request.
[12:58:39 GMT+0100] SignalR: The start request succeeded. Transitioning to the connected state.
[12:58:39 GMT+0100] SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332, keep alive timeout of 20000 and disconnecting timeout of 30000
[13:00:14 GMT+0100] SignalR: Invoking newsletterhub.DoSend
[13:00:17 GMT+0100] SignalR: Invoked newsletterhub.DoSend

我的代码是预编译的,在开发环境中既有源代码又有编译工作,在生产中编译代码不起作用。

我还尝试使用Clients.Client(Context.ConnectionId)代替Clients.Caller,但行为相同。

我已阅读StackOverflow已明确链接的常见问题

如何验证未触发客户端方法的原因?

祝你好运

1 个答案:

答案 0 :(得分:1)

我找到了解决方案!我的生产环境有一个配置为在webgarden(多进程)中工作的应用程序池,因此SignalR doesent正常工作。在这个article中,详细的过程使sql server backbone scaleout config能够与SignalR和webgarden一起工作。这stackoverflow引导我找到解决方案。