为什么在WCF中忽略XmlRoot属性以及如何克服这个问题
发布时间:2020-12-16 07:59:33 所属栏目:百科 来源:网络整理
导读:我们观察到,当我们公开一个使用用各种xml序列化属性修饰的类的WCF服务时,尽管我们在接口上使用XmlSerializerFormat属性,但任何操作参数的XmlRoot属性都会被完全忽略. 参数的名称空间始终是服务的名称空间,而不是我们指定的名称空间. 这引起了我们的问题,因为
我们观察到,当我们公开一个使用用各种xml序列化属性修饰的类的WCF服务时,尽管我们在接口上使用XmlSerializerFormat属性,但任何操作参数的XmlRoot属性都会被完全忽略.
参数的名称空间始终是服务的名称空间,而不是我们指定的名称空间. 这引起了我们的问题,因为它似乎没有向后兼容ASMX,也因为我们正在使用BizTalk,并且需要更严格地控??制所交换的XML的形状. 那么几个问题 – >任何人都知道什么是理由 我看过this帖子,但我不相信这与我的问题有关 – 根据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,XmlRoot(Namespace = "http://datacontract")] public class MyOtherType { [XmlAttribute] public string OtherStringValue { get; set; } }
我假设你使用SOAP作为消息格式.在这种情况下,您要序列化的对象不是XML的根,肥皂信封是.因此,忽略XmlRoot是有道理的.默认情况下,WCF将为您创建一个消息合同并命名响应,并且它具有服务的命名空间.你可以做的是
create your own message contract完全控制SOAP.
创建以下两个类: [MessageContract] public class MyTestMethodRequest { [MessageBodyMember( Namespace = "http://datacontract" )] public MyType MyType; } [MessageContract] public class MyTestMethodResponse { [MessageBodyMember( Namespace = "http://datacontract" )] public MyOtherType MyOtherType; } 然后将服务操作的签名更改为以下内容. [OperationContract] public MyTestMethodResponse MyTestMethod( MyTestMethodRequest request ) { return new MyTestMethodResponse { MyOtherType = new MyOtherType { OtherStringValue = "bar" } }; } 现在,如果您举例说明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> 响应 <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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |