SignalR CORS error on request for Hub method

I am just trying to configure SignalR to run owin and I want to connect via a JavaScript client. Should be easy? I was able to quickly set up the SignalR server and connect to the server. However, when the called callback is called on the client, I try to make a call to the dispatch method of the hubs and I get the following error in the console:

XMLHttpRequest cannot load http: // localhost: 8199 / signalr / signalr / send? Transport = serverSentEvents & clien ... AxBg6iaFwlK50s2DnurE & connectionData =% 5B% 7B% 22name% 22% 3A% 22testhub% 22% 7D% 5D. The requested resource does not have an "Access-Control-Allow-Origin" header. Origin ' http: // localhost ' is therefore not allowed. The response was HTTP status code 500.

Here is my server-side code:

    public static IAppBuilder UseService(this IAppBuilder app)
    {
        log4net.Config.XmlConfigurator.Configure();
        app.UseService(NinjectConfig.CreateKernel);
        app.UseLogging();
        app.UseSignalr();
        return app;
    }

      

    internal static IAppBuilder UseSignalr(this IAppBuilder app)
    {
        app.Map("/signalr", map =>
            {
               map.UseCors(CorsOptions.AllowAll);

                var hubConfig = new HubConfiguration {EnableDetailedErrors = true};
                map.RunSignalR(hubConfig);
            }
        );
        return app;
    }

      

Here is the client side:

    var connection = $.hubConnection('http://localhost:8199/signalr');
    var testHub = connection.createHubProxy('testHub');

    testHub.on('addMessage', function(name){
        console.log(name);
    });

    connection.start().done(function () {
            testHub.invoke('send', 'testName');
        }).fail(function (e) {
            console.log('Failed to connect');
        });
        connection.error(function(error){
        console.log(error);
    });

      

Why does a CORS request work for a startup call rather than a subsequent call to a hub method?

Update: In the network tab, I see there is a call: http: // localhost8199 / signalr / negotiate - this call works and CORS headers will return a response as expected

Then: http: // localhost: 8199 / signalr / connect is called and this call also has the correct CORS headers and works.

Then the call comes up: http: // localhost: 8199 / signalr / start Here the same good things happen.

Finally, there is a call: http: // localhost: 8199 / signalr / send and this is a called call and there are null CORS headers in the error message.

Update:

I also tried to configure my client like this:

    $.connection.hub.url = 'http://localhost:8199/signalr';
        var proxy = $.connection.testHub;
        proxy.client.addMessage = function(name){
            console.log(name);
    };

    $.connection.hub.start()
    .done(function () {
        console.log($.connection.hub.id);
        proxy.server.sendMessage()
            .done(function(val){
                console.log(val);
            });
     })
    .fail(function (e) {
        console.log('Failed to connect');
    });

      

I have little hope for this to work as I feel the problem is with the server. But just wanted to clarify what I've tried here.

+3


source to share


2 answers


Take a look at this: http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#crossdomain

See the note where they point out that depending on the browsers you are using, you may need to fix different things.

SignalR 1.0.1 Cross Domain Request (CORS) with Chrome



SignalR IE9 Invalid Domain Request Not Working

Try to include JSONP method in your startup method:

    internal static IAppBuilder UseSignalr(this IAppBuilder app)
    {
        app.Map("/signalr", map =>
        {
           map.UseCors(CorsOptions.AllowAll);

            var hubConfig = new HubConfiguration {
               EnableDetailedErrors = true,
               EnableJSONP = true
            };
            map.RunSignalR(hubConfig);
        };
        return app;
    }

      

+1


source


It turned out that the error I was observing in the browser was red herring. There was no problem configuring CORS on the server side. I ended up pushing the url in error from the console in chrome:

XMLHttpRequest cannot load http: // localhost: 8199 / signalr / signalr / send? Transport = serverSentEvents & clien ... AxBg6iaFwlK50s2DnurE & connectionData =% 5B% 7B% 22name% 22% 3A% 22testhub% 22% 7D% 5D. The requested resource does not have an "Access-Control-Allow-Origin" header. Origin ' http: // localhost ' is therefore not allowed. The response was HTTP status code 500.

This led me to the yellow screen of death of .Net telling me that the s parameter name cannot be null and there was a null reference exception.



It ended up as a problem with the dependency transformer. We are using the ninject core in this project to resolve dependencies. I didn't know that SignalR would require me to inform my dependent resolver about this if my hub class had a surfaceless constructor, but it needed this information. Below is what I did:

       app.Map("/signalr", map =>
        {
            var resolver = new NinjectSignalRDependencyResolver(kernel);
            GlobalHost.DependencyResolver = resolver;
            map.UseCors(CorsOptions.AllowAll);

            var hubConfig = new HubConfiguration { EnableDetailedErrors = true, EnableJSONP = true };
            map.RunSignalR(hubConfig);
        }

      

So the lesson to be learned is clicking on a hyperlink and there might be a root error deeper.

+1


source







All Articles