Use SignalR hub to connect to another SignalR hub [closed]
Question
I have a website that has SignalR hub, in this hub I need to connect to another SignalR hub which is also hosted in a website.
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);
}
in the second website I have this hub :
public class SecondHub : Hub, IDisconnect
{
public void SecondMethod()
{
Caller.callBack("Yes, I got it");
}
}
in the first website I have this javascript code
var firstHub = $.connection.firstHub;
$.connection.hub.start({ transport: 'auto' }, function () {
alert('connected');
});
$('#TestButton').click(function () {
firstHub.firstMethod();
firstHub.showMessage = function (msg) {
alert(msg);
};
now when I open the page i see the message (connected), but when I press the test button, I get nothing !! and there is no error in the fire-bug console. but sometimes, I don't know how is this happen, when I step into the code and start debugging, the browse shows the message (Ok, Website 2 has return message).
I couldn't figure out what is happening !!
is this idea possible ?? am I missing something ???
I got these lines in the output window :
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
Solution
@dfowler
Ok, as I said in the comments, it was a bug in SignalR and it is solved now.
and also my problem is solved.
Thanks David ,
Thanks everybody, you're awesome.
here are links to relevant topics.
SignalR .NET Client: Cannot Start Connection From Within Web Application App Domain