Yos*_*han 8 wcf xml-serialization
我们观察到,当我们公开一个使用各种xml序列化属性修饰的类的WCF服务时,尽管我们在接口上使用XmlSerializerFormat属性,但任何操作参数的XmlRoot属性都会被完全忽略.参数的名称空间始终是服务的名称空间,而不是我们指定的名称空间.
这导致了我们的问题,因为它似乎不向后兼容ASMX,也因为我们正在使用BizTalk,并且需要更严格地控制XML交换的形状.
那么几个问题 -
我看过这篇文章,但我不认为这与我的问题有关 -
根据Wagner Silveira的要求 - 我用来测试的合同是 -
[ServiceContract(Namespace = "http://servicecontract"),
XmlSerializerFormat(Style = OperationFormatStyle.Document)]
public interface ITestService
{
[OperationContract]
MyOtherType MyTestMethod(MyType obj);
}
// Composite class for DCS and XMLS
[Serializable, XmlType, XmlRoot(Namespace = "http://datacontract")]
public class MyType
{
[XmlAttribute]
public string StringValue { get; set; }
}
// Composite class for DCS and XMLS
[Serializable, XmlType, XmlRoot(Namespace = "http://datacontract")]
public class MyOtherType
{
[XmlAttribute]
public string OtherStringValue { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我假设您使用 SOAP 作为消息格式。在这种情况下,您要序列化的对象不是 XML 的根,soap 信封才是。因此,忽略 XmlRoot 是有道理的。默认情况下,WCF 将为您创建一个消息契约并命名响应,它具有服务的命名空间。您可以做的是创建自己的消息契约来完全控制 SOAP。
创建以下两个类:
[MessageContract]
public class MyTestMethodRequest
{
[MessageBodyMember( Namespace = "http://datacontract" )]
public MyType MyType;
}
[MessageContract]
public class MyTestMethodResponse
{
[MessageBodyMember( Namespace = "http://datacontract" )]
public MyOtherType MyOtherType;
}
Run Code Online (Sandbox Code Playgroud)
然后将您的服务操作的签名更改为以下内容。
[OperationContract]
public MyTestMethodResponse MyTestMethod( MyTestMethodRequest request )
{
return new MyTestMethodResponse {
MyOtherType = new MyOtherType {
OtherStringValue = "bar"
}
};
}
Run Code Online (Sandbox Code Playgroud)
现在,如果您举例说明 SOAP 消息,您应该会看到以下内容:
要求
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none"
s:mustUnderstand="1">http://servicecontract/TestService/MyTestMethod</Action>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyTestMethodRequest xmlns="http://servicecontract">
<MyType StringValue="foo" xmlns="http://datacontract" />
</MyTestMethodRequest>
</s:Body>
</s:Envelope>
Run Code Online (Sandbox Code Playgroud)
回复
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header />
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyTestMethodResponse xmlns="http://servicecontract">
<MyOtherType OtherStringValue="bar" xmlns="http://datacontract" />
</MyTestMethodResponse>
</s:Body>
</s:Envelope>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5661 次 |
| 最近记录: |