c# - WCF 中的 SOAP 消息反序列化问题 - 字段具有空值

标签 c# wcf serialization soap

这是我的服务契约(Contract):

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

这是从 fiddler 发送的 soap 消息(请求):

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 为零)。不过,响应还可以。

最佳答案

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

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

关于c# - WCF 中的 SOAP 消息反序列化问题 - 字段具有空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11754027/

相关文章:

c# - 为什么 BufferedStream.Write 会抛出 "This stream does not support seek operations"?

c# - 具有 x 个额外对象的类

C# 公开术语

c# - IIS 7.5 找到本地网站但无法加载

c++ - 条件反序列化

c# - 获取 IHostingServices 实例的正确方法

c# - 无法在非域上访问服务 Windows 7 自托管 WCF 应用程序

c# - 如何处理一次未找到端点异常(WCF)

javascript - 序列化一个奇怪的js对象

c# - DataContract + 多维数组——有什么解决方案吗?