我有一个网站,其中包含SignalR hub,在这个hub中,我需要连接到另一个同样托管在网站上的SignalR hub。
public FirstHub : Hub, IDisconnect
{
public void FirstMethod()
///Connect to second hub in the second website.
HubConnection hubConnection = new HubConnection("http://localhost:1234");
IHubProxy firstHub = hubConnection.CreateProxy("SecondWebSite.SecondHub");
firstHub.On("secondCallBack", i =>
{
Caller.showMessage("Ok, Website 2 has return message");
});
hubConnection.Start().Wait();
firstHub.Invoke("SecondMethod").ContinueWith(task =>
{
Caller.showMessage(task.Exception.Message);
}, TaskContinuationOptions.OnlyOnFaulted);
}
在第二个网站中,我有这个枢纽:
public class SecondHub : Hub, IDisconnect
{
public void SecondMethod()
{
Caller.callBack("Yes, I got it");
}
}
在第一个网站上,我有这段 JavaScript 代码。
var firstHub = $.connection.firstHub;
$.connection.hub.start({ transport: 'auto' }, function () {
alert('connected');
});
$('#TestButton').click(function () {
firstHub.firstMethod();
firstHub.showMessage = function (msg) {
alert(msg);
};
现在当我打开页面时,我看到信息(已连接),但是当我按下测试按钮时,我什么也没得到!而且火狐控制台没有任何错误。但有时,我不知道为什么,当我进入代码并开始调试时,浏览器显示消息(好的,网站2返回了消息)。
我无法弄清发生了什么!!这个想法有可能吗?我错过了什么吗?
我在输出窗口中得到了这些行:
A first chance exception of type 'System.NullReferenceException' occurred in System.Web.dll
Failed to process message: System.NullReferenceException: Object reference not set to an instance of an object.
at System.Web.HttpApplication.ThreadContext.Enter(Boolean setImpersonationContext)
at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)
at System.Web.AspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state)
at System.Web.AspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state)
at System.Web.AspNetSynchronizationContext.Post(SendOrPostCallback callback, Object state)
at SignalR.Client.Connection.OnReceived(String message) in D:\TestProjects\SignalR\SignalR.Client\Connection.cs:line 188
at SignalR.Client.Transports.HttpBasedTransport.OnMessage(Connection connection, String response) in D:\TestProjects\SignalR\SignalR.Client\Transports\HttpBasedTransport.cs:line 139