具有复杂类型和命名空间的 WCF MessageBodyMember
WCF MessageBodyMember with Complex Type and namespace
我必须为给定的客户端实现 WCF 服务,因此名称空间和合同不是我定义的。问题是,当我将复杂类型用作 MessageBodyMember
时,在服务器端,给定成员在我的服务器端设置为 null。
这是示例请求:
<?xml version="1.0" encoding="utf-8" ?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<ns1:CustomeHeader xmlns:ns1="HEADER_NAMESPACE">
<version>1.0</version>
</ns1:CustomeHeader>
</soapenv:Header>
<soapenv:Body>
<ns2:in4 xmlns:ns2="NAMESPACE_1">
<ns39:userID xmlns:ns39="NAMESPACE_2">
<ns40:ID xmlns:ns40="NAMESPACE_3">someFakeID_123</ns40:ID>
<ns41:type xmlns:ns41="NAMESPACE_3">0</ns41:type>
</ns39:userID>
</ns2:in4>
</soapenv:Body>
</soapenv:Envelope>
如您所见,userID
是一个复杂类型,其成员定义了命名空间。我说的是 MessageBodyMember。
这是我的服务接口定义和实现:
[XmlSerializerFormat]
public interface IIntegrationService
{
[OperationContract]
[XmlSerializerFormat]
SyncOrderRelationshipRsp syncOrderRelationship(SyncOrderRelationshipReq syncOrderRelationshipReq);
}
[ServiceContract]
public class IntegrationService : IIntegrationService
{
public SyncOrderRelationshipRsp syncOrderRelationship(SyncOrderRelationshipReq syncOrderRelationshipReq)
{
//some code here ...
}
}
这里是 SyncOrderRelationshipReq
和 UserID
的定义:
[MessageContract(IsWrapped = true, WrapperName = "in4", WrapperNamespace = "HEADER_NAMESPACE")]
public class SyncOrderRelationshipReq
{
[MessageHeader(Namespace = "HEADER_NAMESPACE")]
public IBSoapHeader IBSoapHeader { get; set; }
[MessageBodyMember(Namespace = "NAMESPACE_2")]
public UserID userID { get; set; }
}
[MessageContract(WrapperNamespace = "NAMESPACE_2", IsWrapped = true)]
public class UserID
{
[MessageBodyMember(Namespace = "NAMESPACE_3")]
public string ID { get; set; }
[MessageBodyMember(Namespace = "NAMESPACE_3", Name = "type")]
public int Type { get; set; }
}
长话短说,我需要 MessageBodyMember 的内部成员设置自己的命名空间,以便我可以读取这些成员。
我终于找到了答案。对于任何来这里寻找答案的人来说,这就是答案。
首先,您应该将 XmlSerializerFormat
属性添加到您的服务接口(我已经这样做了)。
其次,您应该将 XmlType
属性用于您的复杂类型 class。
第三,对复杂类型属性使用XmlElement
属性。
所以,UserId
class 应该是这样的:
[XmlType]
public class UserID
{
[XmlElement(Namespace = "NAMESPACE_3")]
public string ID { get; set; }
[XmlElement(Namespace = "NAMESPACE_3", Name = "type")]
public int Type { get; set; }
}
希望对大家有所帮助。
我必须为给定的客户端实现 WCF 服务,因此名称空间和合同不是我定义的。问题是,当我将复杂类型用作 MessageBodyMember
时,在服务器端,给定成员在我的服务器端设置为 null。
这是示例请求:
<?xml version="1.0" encoding="utf-8" ?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<ns1:CustomeHeader xmlns:ns1="HEADER_NAMESPACE">
<version>1.0</version>
</ns1:CustomeHeader>
</soapenv:Header>
<soapenv:Body>
<ns2:in4 xmlns:ns2="NAMESPACE_1">
<ns39:userID xmlns:ns39="NAMESPACE_2">
<ns40:ID xmlns:ns40="NAMESPACE_3">someFakeID_123</ns40:ID>
<ns41:type xmlns:ns41="NAMESPACE_3">0</ns41:type>
</ns39:userID>
</ns2:in4>
</soapenv:Body>
</soapenv:Envelope>
如您所见,userID
是一个复杂类型,其成员定义了命名空间。我说的是 MessageBodyMember。
这是我的服务接口定义和实现:
[XmlSerializerFormat]
public interface IIntegrationService
{
[OperationContract]
[XmlSerializerFormat]
SyncOrderRelationshipRsp syncOrderRelationship(SyncOrderRelationshipReq syncOrderRelationshipReq);
}
[ServiceContract]
public class IntegrationService : IIntegrationService
{
public SyncOrderRelationshipRsp syncOrderRelationship(SyncOrderRelationshipReq syncOrderRelationshipReq)
{
//some code here ...
}
}
这里是 SyncOrderRelationshipReq
和 UserID
的定义:
[MessageContract(IsWrapped = true, WrapperName = "in4", WrapperNamespace = "HEADER_NAMESPACE")]
public class SyncOrderRelationshipReq
{
[MessageHeader(Namespace = "HEADER_NAMESPACE")]
public IBSoapHeader IBSoapHeader { get; set; }
[MessageBodyMember(Namespace = "NAMESPACE_2")]
public UserID userID { get; set; }
}
[MessageContract(WrapperNamespace = "NAMESPACE_2", IsWrapped = true)]
public class UserID
{
[MessageBodyMember(Namespace = "NAMESPACE_3")]
public string ID { get; set; }
[MessageBodyMember(Namespace = "NAMESPACE_3", Name = "type")]
public int Type { get; set; }
}
长话短说,我需要 MessageBodyMember 的内部成员设置自己的命名空间,以便我可以读取这些成员。
我终于找到了答案。对于任何来这里寻找答案的人来说,这就是答案。
首先,您应该将 XmlSerializerFormat
属性添加到您的服务接口(我已经这样做了)。
其次,您应该将 XmlType
属性用于您的复杂类型 class。
第三,对复杂类型属性使用XmlElement
属性。
所以,UserId
class 应该是这样的:
[XmlType]
public class UserID
{
[XmlElement(Namespace = "NAMESPACE_3")]
public string ID { get; set; }
[XmlElement(Namespace = "NAMESPACE_3", Name = "type")]
public int Type { get; set; }
}
希望对大家有所帮助。