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

使用SoapCore从Postman向.NET Core7发SOAP POST时参数为NULL

解决SoapCore接收SOAP请求时参数为NULL的问题

看起来你遇到的核心问题是SOAP请求的参数绑定失败,我帮你梳理几个关键问题点和对应的修复方案:

1. 错误使用[FromBody]特性

ASP.NET Core的[FromBody]是给Web API的JSON/XML请求用的,SOAP请求完全不需要这个特性——SoapCore会通过SOAP信封的XML结构自动解析参数,加上这个特性反而会干扰绑定逻辑,导致参数为NULL。

修改SampleService.cs里的TestCustomModel方法,移除[FromBody]:

public MyCustomModel TestCustomModel(MyCustomModel customModel)
{
    return customModel;
}

2. 序列化器与模型特性不匹配

你在模型MyCustomModel上用了[DataContract]和[DataMember](这是DataContractSerializer的特性),但在Startup里配置SoapEndpoint时用了SoapSerializer.XmlSerializer,两者不兼容,这也是参数无法解析的重要原因。

方案一:改用DataContractSerializer

修改Startup.cs里的端点配置:

app.UseSoapEndpoint<ISampleService>("/Service.asmx", new SoapEncoderOptions(), SoapSerializer.DataContractSerializer);

方案二:将模型改为适配XmlSerializer

如果你想继续用XmlSerializer,需要把模型的特性换成XML序列化相关的:

[XmlRoot("MyCustomModel")]
public class MyCustomModel
{
    [XmlElement]
    public int Id { get; set; }

    [XmlElement]
    public string Name { get; set; }

    [XmlElement]
    public string Email { get; set; }
}

3. Postman请求格式必须严格匹配WSDL定义

从你的WSDL截图来看,请求必须包含正确的命名空间和参数层级。比如针对TestCustomModel的正确SOAP请求应该是这样的(注意替换成你WSDL里的实际命名空间,通常是类似http://tempuri.org/的格式):

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <TestCustomModel xmlns="http://tempuri.org/">
      <customModel>
        <Id>1</Id>
        <Name>测试名称</Name>
        <Email>test@example.com</Email>
      </customModel>
    </TestCustomModel>
  </soap:Body>
</soap:Envelope>

另外要确保Postman的请求头里Content-Type设置为text/xml; charset=utf-8。

关于XmlMethod方法的额外说明

如果要接收XElement类型的参数,同样要保证请求里的参数结构匹配WSDL定义,比如请求的XmlMethod节点下要有一个名为xml的子元素,里面包含你要传递的XML内容。

按照上面的步骤修改后,应该就能正常接收参数了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 16:50:16