我有一个拥有SignalR中心的网站,在这个中心我需要连接到另一个也在网站上托管的SignalR中心。
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);
};
现在当我打开页面时,我看到了消息(已连接),但是当我按下测试按钮时, 我一无所获! 并且fire-bug控制台中没有错误。 但有时,我不知道这是怎么发生的,当我进入代码并开始调试时,浏览显示消息(好的,网站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
答案 0 :(得分:3)
@dfowler 好吧,正如我在评论中所说,这是SignalR中的一个错误,现在已经解决了。 而且我的问题也解决了 谢谢大卫, 谢谢大家,你真棒 这里是相关主题的链接。
SignalR .NET Client: Cannot Start Connection From Within Web Application App Domain