![](/img/trans.png)
[英]How to deserialize a WCF soap response message from a file with DataContractSerializer?
[英]WCF: How to deserialize parameters of a SOAP message with different namespaces?
我是WCF和Stackoverflow的新手。 我正在尝试处理来自现有客户端的SOAP(1.2)请求。 该消息将是:
<s:Body> <ns1:MyMethod> <ns1:Parameter1> A string value </ns1:Parameter1> <ns2:Parameter2> Another string value </ns2:Parameter2> </ns1:MyMethod> </s:Body>
这是我的服务器端代码:
[SerivceContract(Namespace = "ns1...")] public class IMyService { [OperationContract(Action="http://the action url")] void MyMethod(string Parameter1, string Parameter2); }
我可以正确反序列化“ Parameter1”,但是“ Parameter2”始终为空。 我想这是由于不同的名称空间(ns1与ns2)造成的。 有帮助吗?
必须创建一种服务来适应客户已经发送的请求是非常不寻常的。
这就像试图购买一台支持15年旧打印机驱动程序的新计算机一样。
解决方案:只需购买一台新打印机。
客户端应该使用服务,而不是相反。
意识到这并不能直接回答您的问题,并且可能超出了解决方案的范围。
这是一个古老的问题,但是当我试图找到类似问题的答案时,我偶然发现了这个问题。 反序列化Parameter1而未反序列化Parameter2的原因可能是因为两个字段都没有名称空间定义,所以它们从其父级(MyMethod)继承了名称空间。 在此线程上也对此进行了说明。 对于当前情况,您将需要使用自定义XmlSerializerFormat并将ns2命名空间添加到Param2中:
[ServiceContract(Namespace = "http://ns1.com")]
[XmlSerializerFormat]
public interface IOpenInvoiceInterface
{
[OperationContract]
MyMethod Test(MyMethod req);
}
public class MyMethod
{
[MessageBodyMember]
public string Param1 { get; set; }
[MessageBodyMember(Namespace = "http://ns2.com")]
public string Param2 { get; set; }
}
使用此设置,将按预期反序列化以下调用:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://ns1.com" xmlns:ns2="http://ns2.com">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ns1:MyMethod >
<ns1:Param1>abc</ns1:Param1>
<ns2:Param2>cde</ns2:Param2>
</ns1:MyMethod>
</s:Body>
</s:Envelope>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.