Websocket连接在.net 4.6中失败

时间:2016-06-22 13:54:40

标签: websocket signalr .net-4.6

我有一个适用于4.5的信号灯 升级到4.6后,它在Win 2008 R2上运行正常,但在Windows 2012上无效。

错误是:

System.InvalidOperationException: WebSockets is unsupported in the current application configuration. To enable this, set the following configuration switch in Web.config:
system.web
httpRuntime targetFramework="4.5" 
system.web
For more information, see http://go.microsoft.com/fwlink/?LinkId=252465.
at  
System.Web.Util.SynchronizationContextUtil.ValidateMode(SynchronizationContextMode currentMode, SynchronizationContextMode requiredMode, String specificErrorMessage)

at System.Web.HttpContext.AcceptWebSocketRequest(Func2 userFunc, AspNetWebSocketOptions options) at Microsoft.AspNet.SignalR.Transports.WebSocketTransport.AcceptWebSocketRequest(Func2 callback)
at Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequestPostGroupRead(HostContext context, String groupsToken)
at Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethodT1,T2,T3,TResult
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Owin.Mapping.MapMiddleware.d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at 

    Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar)
    at         System.Web.HttpApplication.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

如果我将httpRuntime targetFramework =“4.5”添加到web.config,则该应用程序根本不会运行。

2 个答案:

答案 0 :(得分:5)

添加

 <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>

让web.config解决了问题

答案 1 :(得分:0)

假设您在IIS中托管服务器,则需要在IIS中启用websockets。以下是详细信息:http://www.iis.net/learn/get-started/whats-new-in-iis-8/iis-80-websocket-protocol-support