You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET 4.8环境WebSocket不支持问题:无需硬编码版本如何让IIS识别?

ASP.NET 4.8环境下WebSocket报错要求配置targetFramework="4.5"的解决方案

问题描述

运行WebSocket时触发InvalidOperationException异常,具体信息如下:

Exception information:
Exception type: InvalidOperationException
Exception message: 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>

服务器实际运行ASP.NET 4.8,未安装4.0或4.5版本,已确认注册表中.NET版本显示正确,但希望无需硬编码特定版本让IIS识别当前环境满足要求,问题仍未解决。

解决方案

  • 检查应用程序池CLR版本
    打开IIS管理器,找到对应站点的应用程序池,确认.NET CLR版本设置为v4.0.30319(ASP.NET 4.x系列统一CLR版本,包含4.8),避免设置为无托管代码或旧版本。

  • 同步Web.config的版本配置
    尽管不想硬编码版本,但ASP.NET 4.8需要明确指定targetFramework才能启用WebSocket特性,直接配置为4.8即可,不会依赖低版本:

    <system.web>
      <httpRuntime targetFramework="4.8" />
      <compilation targetFramework="4.8" />
    </system.web>
    

    确保httpRuntime和compilation的targetFramework版本一致,让框架正确识别运行环境。

  • 确认IIS WebSocket功能已安装
    打开服务器管理器的添加角色和功能,检查Web服务器(IIS) -> Web服务器 -> 应用程序开发下的WebSocket协议是否已勾选安装,未安装则添加该功能。

  • 验证应用信任级别
    检查Web.config中的<trust level="Full" />设置,受限信任级别可能会阻止WebSocket功能启用。

内容的提问来源于stack exchange,提问作者Neil Carmichael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 20:20:56