Search code examples
asp.net-mvcasp.net-mvc-4cross-domainsignalrsignalr-hub

Signalr 1.0.1 Cross domain 404 error


I have problem with Signalr cross domain usage. I have three different projects(applications) inside one solutions and use signalr to enable chat functionality among them. I have chat project that is separated for other three apps.

This is code from it:

HUB

[HubName("ChatHub")]
public class ChatHub : Hub
 {
    public void Send(PorukaViewModel message)
     {
        ..do some code

        Clients.All.addMessage(
           ... // returns feedback to clients
        );
     }
 }

GlobalASAX

protected void Application_Start()
        {
            RouteTable.Routes.MapHubs(new HubConfiguration() { EnableCrossDomain = true });
        }

And this is code from my clients apps,

Controller

string chatUrl = System.Configuration.ConfigurationManager.AppSettings["ChatUrl"] + "/signalr/hubs";

var connection = new HubConnection(chatUrl, useDefaultUrl: false);
IHubProxy myHub = connection.CreateHubProxy("ChatHub");

connection.Start().ContinueWith(task =>
{
  if (task.IsFaulted)
  {
    //... I log error and stop connection
    connection.Stop();
  }

  message = "some message";
  myHub.Invoke("Send", message).Wait();
    connection.Stop();
});

This all working fine on my localhost, but when I deploy it on IIS I have this error on connection.Start():

System.Net.WebException: The remote server returned an error: (404) Not Found. at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at Microsoft.AspNet.SignalR.Client.Http.HttpHelper.<>c__DisplayClass2.b__0(IAsyncResult ar) at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization)

I browsed all question and answers on stackoverflow but can't find any that would help me.

What am I doing wrong?

*NOTE

With jQuery I modified hubs.js and changed this code:

var signalrUrl = $("#chatUrl").val() + '/signalr';
...
signalR.hub = $.hubConnection(signalrUrl, { useDefaultPath: false });

and use this in my communication Views, this is working fine both on localhost and IIS.

Maybe problem is in this line?

signalR.hub = $.hubConnection(signalrUrl, { useDefaultPath: false });

In original /signalr/hubs it like this:

signalR.hub = $.hubConnection("/signalr", { useDefaultPath: false });

How can I do that from controller?


Solution

  • This code is incorrect:

    string chatUrl = System.Configuration.ConfigurationManager.AppSettings["ChatUrl"] + "/signalr/hubs";

    /SignalR/Hubs points to a Javascript proxy.

    /signalr is the connection end point so the code should be:

    string chatUrl = System.Configuration.ConfigurationManager.AppSettings["ChatUrl"];

    Since the .NET client automatically appends the default /signalr URL.

    More on the documentation here https://github.com/SignalR/SignalR/wiki/SignalR-Client-Hubs#hubconnection-api