[assembly: OwinStartup(typeof (Startup))] namespace MyApp.Service { public class Startup { public void Configuration(IAppBuilder app) { app.Map("/signalr",map => { //worry about locking it down to specific origin later map.UseCors(CorsOptions.AllowAll); map.RunSignalR(new HubConfiguration()); }); //now start the WebAPI app GlobalConfiguration.Configure(WebApiConfig.Register); } } }@H_502_3@WebApiConfig.cs还包含自己的CORS声明.
namespace MyApp.Service { public static class WebApiConfig { public static void Register(HttpConfiguration config) { //controller invocations will come from the MVC project which is deployed to a //different domain,so must enable cross origin resource sharing config.EnableCors(); // Web API routes config.MapHttpAttributeRoutes(); //Snip other controller dependency initialisation } } }@H_502_3@我已经定义了一个没有服务器端API的简单集线器类(它只允许服务器推送到客户端,而不是客户端调用).
namespace MyApp.Service.Hubs { [HubName("testresult")] public class TestResultHub : Hub { } }@H_502_3@由于我要跨域并且集线器没有公开任何服务器端API,所以我不打算使用生成的JS代理.
设置信号器集线器连接的JS的相关位是:(记住这是从MVC应用程序提供的,它没有任何信号器支持(当然除了jquery-signalr- {version} .js))
function TestScenarioHandler(signalrHubUrl) { var self = this; //Snip irrelevant bits (mostly Knockout initialisation) self.signalrConnectionId = ko.observable(); var hubConnection = $.hubConnection(signalrHubUrl,{ useDefaultPath: false }); var hubProxy = hubConnection.createHubProxy("testresult"); hubProxy.on("progress",function(value) { console.log("Hooray! Got a new value from the server: " + value); }); hubConnection.start() .done(function() { self.signalrConnectionId(hubConnection.id); console.log("Connected to signalr hub with connection id " + hubConnection.id); }) .fail(function() { console.log("Failed to connect to signalr hub at " + hubConnection.url); }); }@H_502_3@像这样跨越起源,Firefox网络流量显示(我已经确认Chrome显示同样的事情)GET to
?HTTP://****service.azurewebsites.net/signalr/negotiate clientProtocol = 1.5&安培; connectionData = [{ “名称”: “的TestResult”}]&安培; _ = 1424419288550
请注意,该名称与我的集线器类上的HubName属性的值匹配.
此GET返回HTTP 200,响应为我提供了一个JSON有效负载,其中包含ConnectionId,ConnectionToken和一堆其他字段,表明一切正常. HTTP响应还将Access-Control-Allow-Origin:标头设置为GET源自的域.一切都看起来不错,除了交通停止的地方.
但JS控制台打印“无法连接到信号器集线器http://****service.azurewebsites.net/signalr”
为了验证我没有做任何太愚蠢的事情,我已经为MVC应用程序添加了信号器支持和基本集线器(因此不需要交叉源),并相应地更改了$.hubConnection()和hubConnection.createProxy()调用.当我这样做时,浏览器流量显示相同/信号器/协商?… GET(显然不再交叉原点),但是然后GET到/ signalr / connect?…和/ signalr / start?… .JS控制台还会打印成功消息.
总而言之;
>在服务层启用CORS,信号器/协商GET返回200,看起来是有效的连接ID,以及预期的Access-Control-Allow-Origin:标头.这向我建议服务器端CORS支持正常运行,但信号器连接不成功.
>当我重新配置所以信号器连接不是交叉原点时,一切都按预期工作.WTF我错过了还是做错了?!也许HttpConfiguration.EnableCors()和IAppBuilder.UseCors(CorsOption)之间存在一些冲突?
解决方法
private static readonly Lazy<CorsOptions> SignalrCorsOptions = new Lazy<CorsOptions>(() => { return new CorsOptions { PolicyProvider = new CorsPolicyProvider { PolicyResolver = context => { var policy = new CorsPolicy(); policy.AllowAnyOrigin = true; policy.AllowAnyMethod = true; policy.AllowAnyHeader = true; policy.SupportsCredentials = false; return Task.FromResult(policy); } } }; }); public void Configuration(IAppBuilder app) { app.Map("/signalr",map => { map.UseCors(SignalrCorsOptions.Value); map.RunSignalR(new HubConfiguration()); }); //now start the WebAPI app GlobalConfiguration.Configure(WebApiConfig.Register); }@H_502_3@将SupportCredentials设置为true会导致在响应中使用实际原点(非*)和access-control-allow-credentials:true重写Access-Control-Allow-Origin标头.
现在它有效.