Bob Manz
Bob Manz

Reputation: 106

SignalR 2.0 - 404 in IIS Virtual Directory

I'm having an issue when deploying a very basic MVC5 app running SignalR 2.0.2. Everything works great in my local development environment when I'm running it with IIS Express. When I deploy to IIS, my js receives a 404 error attempting to connect to SignalR.

More specifically, I'm deploying to an application/virtual directory that is running under my Default Web Site. When I publish directly to Default Web Site, everything works successfully so IIS is not the issue.

GET http://myServer/signalr/negotiate?connectionData=%5B%5D&clientProtocol=1.3&_=1395517687175 404 (Not Found)

I'm assuming the 404 is caused by the missing application name. ie: myServer/MyApp/signalr/negotiate...

I've searched a number of posts and SignalR documentation with no luck regarding IIS and Applications/Virtual Directories and SignalR. Below is snippets of code in my app.

Thanks!

JS:

var connection = $.hubConnection();
var proxy = connection.createHubProxy('TestHub');

connection.start()
.done(function () {
console.log('Now connected, connection ID=' + connection.id + ' using transport=' +   connection.transport.name);
                })
                .fail(function () { console.log('Could not connect'); });

Startup.cs:

 app.MapSignalR();

Update By changing the following JS code I was able to 'fix' the issue. The question is, how proper is this?

//var connection = $.hubConnection();
var connection = $.hubConnection("/MyApp/signalr", { useDefaultPath: false });

Upvotes: 4

Views: 3475

Answers (2)

do0g
do0g

Reputation: 311

A solution which will work in dev, and in IIS hosted as application, virtual directory or root is to configure the hub using the page url as its base. This will mean you won't need to hard code the value and negates configuration change for development and deployed scenarios.

var connection = $.hubConnection(document.location.origin + document.location.pathname);

Upvotes: 1

halter73
halter73

Reputation: 15234

Your fix seems reasonable.

{ useDefaultPath: false } simply tells SignalR not to append "/signalr" to the url, so you could also create your connection object like this: var connection = $.hubConnection("/MyApp");

Alternatively, if you want to use JS hub proxies generated at /MyApp/signalr/hubs, you can could connect like this:

var proxy = $.connection.testHub;

// Make sure you always wire up client methods before calling start
proxy.client.myClientMethod = function () { /* ... */ };

$.connection.hub.start()
    .done(function () { /* ... */ })
    .fail(function () { /* ... */ });

http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-javascript-client#genproxy

Upvotes: 1

Related Questions