Question

The main problem about Access-Control-Allow-Origin I think. But when I configure the Web API project as defined in the given documentation, it still not working in chrome and firefox but working in IE well (it is about IE thinks localhost is not cross domain, AFAIK). I tried different ways to make it work but no result.

I put the example project to github repository. Project is very simple. There are two applications working on cross domains. It is very simple chat application like in signalr examples.

You must change the value of api host in client javascript file: https://github.com/yusufuzun/WebApiSignalR/blob/master/ChatApp/Scripts/app/chat.js#L2

When you open the Chat page in mvc project, there will be two requests to api application

1- Regular ajax request (which is working fine)

2- Signalr negotiate request (cancelled)

And also I don't think browser disables the CORS because of if it disables there would not be an hit to server. So I think it is about browser but not about browser disables (something else).

Details are in repository Readme: https://github.com/yusufuzun/WebApiSignalR/blob/master/README.md Fiddler Results: https://github.com/yusufuzun/WebApiSignalR/blob/master/FiddlerResults

The bad part about it also is server returning 500 with this error: System.InvalidOperationException: 'chat' Hub could not be resolved.

Which hub name is chat also. https://github.com/yusufuzun/WebApiSignalR/blob/master/ChatApi/Hubs/ChatHub.cs#L10

You can enable CORS for Web Api in project with different ways for test purposes. Each one is giving different errors all about XMLHttpRequest Access-Control-Allow-Origin.

I commented them, so you can uncomment and make test for each one:

https://github.com/yusufuzun/WebApiSignalR/blob/master/ChatApi/Global.asax.cs#L24 https://github.com/yusufuzun/WebApiSignalR/blob/master/ChatApi/App_Start/WebApiConfig.cs#L14 https://github.com/yusufuzun/WebApiSignalR/blob/master/ChatApi/App_Start/WebApiConfig.cs#L16 https://github.com/yusufuzun/WebApiSignalR/blob/master/ChatApi/Controllers/ChatController.cs#L17

So what is going on here?

Was it helpful?

Solution

After I talked with David Fowler in JabbR, he mentioned the thing about using CORS with SignalR. My signalr startup code was wrong. So after changing the startup code like in his advice it worked well.

He also mentioned SignalR and Web API are working with different CORS definitions. So enabling or disabling one doesn't affect other.

Here is the new startup code:

app.Map("/signalr", map =>
{
    map.UseCors(CorsOptions.AllowAll);
    map.RunSignalR(new HubConfiguration()
    {
        EnableDetailedErrors = true,
        EnableJavaScriptProxies = true
    });
});

The old one:

app.MapSignalR(new HubConfiguration()
{
    EnableDetailedErrors = true,
    EnableJavaScriptProxies = true
}).UseCors(CorsOptions.AllowAll);

Hope it helps to somebody out there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top