我正在从 .Net 4.5.2 框架上运行的 Web 应用程序中的 Web 表单调用 COM 对象。我在使用 AspCompat="true" 页面指令时收到以下错误消息,这是调用 STA(单线程单元)COM 对象所必需的:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: <%@ Page AspCompat="true" %> and <httpRuntime apartmentThreading="true" /> are unsupported in the current application configuration. To work around this, add the following configuration switch in Web.config:
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="false" />
</appSettings>
For more information, see http://go.microsoft.com/fwlink/?LinkId=252465.
错误消息指出我需要在 web.config 文件的应用程序设置中包含aspnet:UseTaskFriendlySynchronizationContext = false。这解决了第一个异常,但会引发一个新异常,请求删除应用程序设置,因为应用程序还使用 WebSockets(由 SignalR 使用):
Exception information:
Exception type: InvalidOperationException
Exception message: WebSockets is unsupported in the current application configuration. To work around this, remove the following configuration switch in Web.config:
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" />
</appSettings>
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(Func`2 userFunc, AspNetWebSocketOptions options)
at Microsoft.AspNet.SignalR.Transports.WebSocketTransport.AcceptWebSocketRequest(Func`2 callback)
at Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequestPostGroupRead(HostContext context, String groupsToken)
at Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod[T1,T2,T3,TResult](Func`4 func, T1 arg1, T2 arg2, T3 arg3)
是否可以在 Web 表单中包含 AspCompat="true" 页面指令并在 .Net 应用程序的其他地方仍然使用 WebSockets(由 SignalR 使用)?