升级至.NET Core 3.0/3.1后WCF客户端无法工作求助
问题背景
我创建了三个ASP.NET Core MVC测试项目,仅在HomeController中添加WCF客户端调用逻辑,核心代码如下:
客户端(ASP.NET Core MVC)代码
public class HomeController : Controller { private readonly WCFGetClient _getClient; public HomeController() { _getClient = new WCFGetClient(); } // 模板默认代码省略 public async Task<IActionResult> Privacy() { _ = await _getClient.GetAsync("some info", false); return View(); } }
WCF服务端代码
public DbModel GetAsync(string Id, bool withDeleted) { return withDeleted ? _db.Table.SingleOrDefault(c => c.Id == Id) : _db.Table.Any(c => c.Id == Id && c.Deleted == false) ? _db.Table.Single(c => c.Id == Id) : null; }
异常信息
调试时发现:
- ASP.NET Core 2.2项目能正常调用部署在Windows Server 2012R2 IIS 6.2上的.NET 4.7.2版本WCF服务
- ASP.NET Core 3.0/3.1项目均抛出以下异常:
SocketException: An established connection was aborted by the software in your host machine.
IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine..
System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable+ConfiguredValueTaskAwaiter.GetResult()
已尝试升级System.ServiceModel相关类库(Duplex、Federation、Http、NetTcp、Primitives、Security)至4.10.0版本,问题未解决。
更新#0:将WCF服务部署到Windows 10专业版的IIS 10后,三个版本的Core项目均能正常交互,无报错。
可能原因及解决方案
1. TLS协议版本不兼容
ASP.NET Core 3.0+ 默认禁用了TLS 1.0/1.1等旧版协议,而Windows Server 2012R2默认可能未启用TLS 1.2或更高版本,导致客户端与服务端协商失败。
解决步骤:
- 在Windows Server 2012R2上启用TLS 1.2:通过注册表修改开启(路径:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols,新建TLS 1.2的Client和Server项,将Enabled和DisabledByDefault分别设为1和0) - 在ASP.NET Core 3.0/3.1项目的WCF客户端初始化前,显式指定TLS版本:
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
2. IIS 6.2的WCF宿主配置差异
Windows Server 2012R2的IIS 6.2与IIS 10的WCF宿主环境(进程模型、应用池设置)存在差异,可能导致Core 3.0+客户端连接失败。
解决步骤:
- 检查WCF服务所在应用程序池的.NET Framework版本,确保设置为v4.0(与服务的4.7.2版本兼容)
- 若服务依赖32位组件,启用应用程序池的32位支持
- 检查IIS站点绑定,确认HTTP/HTTPS协议及端口配置正确,无端口占用
3. ASP.NET Core 3.0+的WCF客户端行为变更
.NET Core 3.0对WCF客户端的底层传输实现做了调整,旧版WCF服务的绑定配置(如basicHttpBinding的安全设置)可能与新客户端不兼容。
解决步骤:
- 核对WCF服务与客户端的绑定安全配置,确保模式匹配(例如服务用
Transport安全,客户端需同步配置) - 在Core项目中显式指定绑定参数,避免依赖默认值:
var binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport); binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; var endpoint = new EndpointAddress("https://your-wcf-service-url"); var client = new WCFGetClient(binding, endpoint);
4. Windows Server 2012R2的防火墙/网络策略限制
服务器端防火墙或网络策略可能拦截了Core 3.0+客户端的连接请求。
解决步骤:
- 检查Windows防火墙规则,确保允许WCF服务端口的入站/出站连接
- 临时关闭防火墙测试,确认是否为防火墙导致的连接中断
内容的提问来源于stack exchange,提问作者TryHard

