【问题标题】:SOAP message deserialization issue in WCF - fields have null valuesWCF 中的 SOAP 消息反序列化问题 - 字段具有空值
【发布时间】:2012-08-01 06:54:47
【问题描述】:

这是我的服务合同:

[ServiceContract(Namespace = Service.Namespace)]
[XmlSerializerFormat(Style=OperationFormatStyle.Document, Use=OperationFormatUse.Literal)]
public interface IService
{
    [OperationContract]
    UpdateResponse Update(UpdateRequest request);
}

实现:

[ServiceBehavior(Namespace = Namespace)]
public class Service : IService
{
    public const string Namespace = "http://schemas.localhost.net/test";

    public UpdateResponse Update(UpdateRequest request)
    {
        return new UpdateResponse() { Succeeded = true };
    }
}

消息合约:

[MessageContract(WrapperNamespace = Service.Namespace)]
public class UpdateRequest
{
    [MessageBodyMember]
    public int Id { get; set; }
    [MessageBodyMember]
    public string Name { get; set; }
}

[MessageContract(WrapperNamespace = Service.Namespace)]
public class UpdateResponse
{
    [MessageBodyMember]
    public bool Succeeded { get; set; }
}

web.config 文件(其中的一部分):

<services>
  <service behaviorConfiguration="returnFaults" name="ServiceTest.Service">
    <endpoint binding="basicHttpBinding" contract="ServiceTest.IService" 
              bindingNamespace="http://schemas.localhost.net/test" />
  </service>
</services>

这是从提琴手发送的肥皂消息(请求):

POST http://localhost:10000/Service.svc HTTP/1.1
SOAPAction: "http://schemas.localhost.net/test/IService/Update"
Content-Type: text/xml; charset="utf-8"
Host: localhost:10000
Content-Length: 532

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body xmlns:NS1="http://schemas.localhost.net/test">       
    <NS1:UpdateRequest id="1">  
        <Name xsi:type="xsd:string">Abcdefg</Name><Id xsi:type="xsd:int">1</Id>
    </NS1:UpdateRequest>
    <parameters href="#1"/>
</SOAP-ENV:Body>

Update() 操作接收到一个 UpdateRequest 对象,但未设置字段(Name 为 null,Id 为零)。不过反应还可以。

【问题讨论】:

    标签: c# wcf serialization soap


    【解决方案1】:

    Name 和 Id 元素没有命名空间。所以要么肥皂信封不正确,要么 MessageContract 不完整。名称/ID 应如下所示:

    <NS1:Name xsi:type="xsd:string">Abcdefg</NS1:Name><NS1:Id xsi:type="xsd:int">1</NS1:Id>
    

    【讨论】:

    • 不太确定。要继承,您需要 xmlns="namespace"。这里它被定义为 NS1,不是默认的,因此不是继承的。
    • 我的立场是正确的,这确实只适用于默认命名空间。
    【解决方案2】:

    我使用的是 XmlSerializer :) 但是我没有用以下属性装饰消息的成员:

    [XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified), MessageBodyMember]
    

    反序列化现在可以工作了。

    PS:我无法按照 Tisho 所说的去做,因为我无法控制 Web 服务的客户端。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 2010-11-21
      • 1970-01-01
      相关资源
      最近更新 更多