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

ASP.NET Core调用SOAP服务遇System.PlatformNotSupportedException问题求解

解决ASP.NET Core 6调用SOAP服务时的System.PlatformNotSupportedException错误

错误原因

该错误源于.NET Core环境下,WCF客户端生成的异步调用代码依赖了已被移除的脚本编译功能,或是默认绑定配置与.NET Core不兼容。

解决方案

1. 手动配置WCF客户端绑定与终结点

在Program.cs中显式配置HrmServiceServiceClient的绑定和终结点,替代自动生成的可能存在问题的配置:

builder.Services.AddSingleton<HrmServiceServiceClient>(sp =>
{
    // 创建适配第三方SOAP服务的BasicHttpBinding
    var binding = new BasicHttpBinding();
    // 根据服务实际要求调整配置,比如安全模式、超时时间
    binding.Security.Mode = BasicHttpSecurityMode.None;
    binding.SendTimeout = TimeSpan.FromSeconds(30);
    binding.ReceiveTimeout = TimeSpan.FromSeconds(30);

    // 服务终结点地址
    var endpointAddress = new EndpointAddress("http://thirdparty:2022/HrmService/HrmService");

    return new HrmServiceServiceClient(binding, endpointAddress);
});

2. 改用同步调用包装异步返回(临时兼容方案)

若异步方法生成存在问题,可暂时使用同步调用并包装为异步任务:

public async Task<EmployeeResponse> GetEmployeeDetailAsync(EmployeeDto model)
{
    QUERYEMPLOYEE_REQ requestBody = new QUERYEMPLOYEE_REQ
    {
        EmployeeIO = new EmployeeIOType
        {
            EMP = model.EmployeeCode,
            DEP = model.DepartmentCode
        }
    };
    // 调用同步方法,包装为异步返回
    var response = _client.QueryEmployee(requestBody);
    return await Task.FromResult(response);
}

3. 重新生成服务引用

使用最新版WCF Connected Service工具重新生成服务引用:

  • 在Visual Studio中右键项目 → 选择「添加」→「Connected Service」
  • 选择「Microsoft WCF Web Service Reference Provider」
  • 输入WSDL地址,在生成选项中选择.NET Core兼容配置,确保异步方法生成逻辑符合.NET Core规范

4. 清理无效依赖

检查生成的服务引用代码,若存在System.ServiceModel.Scripting相关引用,直接移除(.NET Core不支持该组件)。

验证修改

完成配置后重启API,调用对应接口测试是否能正常获取第三方SOAP服务响应。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 23:25:12