2011-08-19 86 views
5

外部(Java)应用程序将消息发送到我们的Web服务。此消息包含多个命名空间:WCF - 反序列化时控制名称空间

<StUF:Fo01Bericht xmlns:StUF="http://www.egem.nl/StUF/StUF0300"> 
    <LVO:stuurgegevens xmlns:LVO="http://www.vrom.nl/StUF/sector/lvo/0305"> 
     <StUF:versieStUF>0300</StUF:versieStUF> 
     <StUF:berichtcode>Fo01</StUF:berichtcode> 
    </LVO:stuurgegevens> 
    <StUF:body> 
     <StUF:code>200</StUF:code> 
     <StUF:plek>LVO</StUF:plek> 
     <StUF:omschrijving>test</StUF:omschrijving> 
    </StUF:body> 
</StUF:Fo01Bericht> 

WCF服务无法反序列化,因为在第二行的LVO前缀的该消息(它应根据WSDL已经塞入)。

我想让我们的Web服务接受这些消息。有没有办法做到这一点 - 最好使用属性?

回答

1

我不相信你可以通过修改DataContract命名空间来完成。原因是DataMember属性合理地假定类属性与类本身在同一个XML名称空间中。但是,您可以使用MessageContractMessageBodyMember属性的组合来完成此操作。另一个可能更简单的替代方法是implement a message inspector重新设置soap消息的格式以符合预期的XML模式。

+0

我们不使用d默认的WCF序列化。该服务依赖于XML序列化。我已经尝试使用XmlNamespaceDeclarations属性。但这似乎并不奏效。 –

+1

通过实现IXmlSerializable接口来修复问题。 –

+0

@WvanNoort你可以扩展你做了什么?我现在有同样的问题。 – Declan

1

我在接受来自第三方的肥皂邮件时遇到此问题。

这里是SOAPHEADER我被送到(注中的UsernameToken不同的命名空间):

<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wssu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> 
     <wsse:UsernameToken> 
      <wsse:Username>userName</wsse:Username> 
      <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">password</wsse:Password> 
      <wsse:Nonce>nonce</wsse:Nonce> 
      <wssu:Created>2015-02-19T16:24:32Z</wssu:Created> 
     </wsse:UsernameToken> 
    </wsse:Security> 

反序列化正确的,我需要实现IXmlSerializable的,在我的DataContract,如下图所示:

[DataContract(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", Name = "Security")] 
public partial class SecurityHeaderType 
{ 
    [XmlElementAttribute(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")] 
    [DataMember] 
    public UsernameToken UsernameToken { get; set; } 
} 

public class UsernameToken : IXmlSerializable 
{ 
    public string Username { get; set; } 
    public string Password { get; set; } 
    public string Nonce { get; set; } 
    public string Created { get; set; } 

    public XmlSchema GetSchema() 
    { 
     throw new NotImplementedException(); 
    } 

    public void ReadXml(XmlReader reader) 
    { 
     Dictionary<string, string> secDictionary; 
     string xml = reader.ReadOuterXml(); 

     using (var s = GenerateStreamFromString(xml)) 
     { 
      secDictionary = 
         XElement.Load(s).Elements() 
         .ToDictionary(e => e.Name.LocalName, e => e.Value); 
     } 

     Username = secDictionary["Username"]; 
     Password = secDictionary["Password"]; 
     Nonce = secDictionary["Nonce"]; 
     Created = secDictionary["Created"];   

    } 

我当时能够反序列化我的标题如下:

if (OperationContext.Current.IncomingMessageHeaders.FindHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd") != -1) 
{ 
    var securityHeader = OperationContext.Current.IncomingMessageHeaders.GetHeader<SecurityHeaderType>("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); 
} 
+1

看起来你已经找到了自己的答案。因为,我转向了另一个雇主,我再也无法访问该代码了,但这与我所做的基本相同。 –

相关问题