在WCF SOAP消息的反序列化问题 - 字段具有空值 [英] SOAP message deserialization issue in WCF - fields have null values

查看:230
本文介绍了在WCF SOAP消息的反序列化问题 - 字段具有空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的服务合同:

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



实施

The implementation:

[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 };
    }
}



消息的合同:

The message contracts:

[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文件(的一部分):

The web.config file (a part of it):

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

这是小提琴手发送的SOAP消息(请求):

And here is the soap message (request) sent from fiddler:

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>



更新()操作接收UpdateRequest对象,但的字段没有被设置(名称为空,ID为零)。响应是好的,但。

Update() operation receives an UpdateRequest object, but the fields are not set (Name is null, Id is zero). The response is ok, though.

推荐答案

的名称和ID的元素没有命名空间。因此,无论SOAP信封是不正确的,或者MessageContract是不完整的。名称/ ID应该是这样的:

The Name and Id elements don't have namespace. So either the soap envelope is not correct, or the MessageContract is not complete. The Name/Id should look like:

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

这篇关于在WCF SOAP消息的反序列化问题 - 字段具有空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆