WCF不反序列化.为什么我的参数为NULL?

Pap*_*ndy 5 c# xml wcf soap web-services

我正在尝试设置一个Web服务,它将接受预定义的传入SOAP/XML消息.我无法控制客户端代码或发送的SOAP消息.我正在尝试一个简单的例子,我遇到了问题.假设这是SOAP消息:

    <?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
  <env:Body>
    <CustomerRequest xmlns="http://tempuri.org">
      <Customer>
        <FirstName>John</FirstName>
        <LastName>Doe</LastName>
      </Customer>
    </CustomerRequest>
  </env:Body>
</env:Envelope>
Run Code Online (Sandbox Code Playgroud)

我的数据合同对象:

[DataContract(Name = "Customer", Namespace = "http://tempuri.org")]
public class Customer
{
    [DataMember]
    public string FirstName { get; set; }

    [DataMember]
    public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

服务接口:

[ServiceContract(Namespace = "http://tempuri.org")]
public interface IService1
{
    [OperationContract(Action="*")]
    [WebInvoke(Method = "POST")]
    bool Customer(Customer customer);
}
Run Code Online (Sandbox Code Playgroud)

当我发送SOAP请求时,我可以查看fiddler中的所有内容,看起来没问题.但是当它命中我的代码时,Customer对象为null.我觉得我错过了很简单的事情.

这也是原始请求:

POST http://127.0.0.1.:3619/Service1.svc HTTP/1.1
SOAPAction: http://tempuri.org/IService1/Customer
Content-Type: text/xml;charset=utf-8
Host: 127.0.0.1.:3619
Content-Length: 339
Expect: 100-continue
Connection: Keep-Alive

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
  <env:Body>
    <CustomerRequest xmlns="http://tempuri.org">
      <Customer>
        <FirstName>John</FirstName>
        <LastName>Doe</LastName>
      </Customer>
    </CustomerRequest>
  </env:Body>
</env:Envelope>
Run Code Online (Sandbox Code Playgroud)

Nic*_*yan 2

我在您的界面或服务实现中看不到任何提及CustomerRequest。我认为 SOAP 请求应该发送 aCustomer而不是CustomerRequest. 您可以通过使用 SOAPUI 并根据 WSDL 生成示例请求来验证这一点。会告诉您请求实际上应该是什么样子。

  • 我从操作合同中删除了 (Action="*") 并使用 WCF 测试客户端查看请求并能够使其正常工作。另外,我必须将 XMLSerializerFormat 设置为 [XmlSerializerFormat(Style=OperationFormatStyle.Document, Use=OperationFormatUse.Literal)] (2认同)