Reputation: 61
I have an ASP.NET Web Api project in which SignalR has been implemented. In Stratup class I use CorsOptions.AllowAll. In WebApiConfig class I also use EnableCorsAttribute("", "", "*"). I published this Web Api project in a server location (http://192.168.9.6:3030). But while trying to connect using $.hubConnection("http://192.168.9.6:3030/signalr") from a javascript client I get this: "Error during negotiation request."
Instead of using mentioned Url if I use http://localhost:8080 from my PC I can Connect to SignalR.
Here is StartUp Class
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.Map("/signalr", map =>
{
app.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration();
map.RunSignalR(hubConfiguration);
});
}
}
Hub Class
public class SetupHub : Hub
{
public void Send(string name, string message)
{
var msg = String.Format("{0}: {1} : {2}", name, message, DateTime.Now);
Clients.All.addMessage(msg);
}
}
WebApiConfig Class
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
var cors = new EnableCorsAttribute("*", "*", "*");
// config.EnableCors();
config.EnableCors(cors);
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Global ASAX
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
JavaScript Client
var connection = $.hubConnection("http://192.168.9.6:3030/signalr");
var setupHubProxy = connection.createHubProxy('SetupHub');
setupHubProxy.on('addMessage', function (msg) {
alert(msg);
});
connection.start()
.done(function () {
console.log('connected');
})
.fail(function (a) {
console.log('not connected' + a);
});
Any Help regarding this will be Highly Appreciated.
Upvotes: 1
Views: 5800
Reputation: 61
Error is in this line:
app.UseCors(CorsOptions.AllowAll);
It will be
map.UseCors(CorsOptions.AllowAll);
Upvotes: 0
Reputation: 712
I have it in the web.config
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
maybe this works for you too.
Upvotes: 0