如何在WCF中反序列化自定义SOAP标头?

时间:2014-08-19 17:10:48

标签: c# .net wcf soap

我试图通过WCF向所有SOAP请求添加自定义标头。我发现了this fantastic article如何做到这一点。我的MessageHeader课程如下:

public class OperatorNameMessageHeader : MessageHeader
{
    private string opName;

    public const string HeaderName = "OperatorNameMessageHeader";
    public const string HeaderNamespace = "http://schemas.microsoft.com/scout";

    public override string Name { get { return HeaderName; } }
    public override string Namespace { get { return HeaderNamespace; } }

    public string OperatorName
    {
        get { return opName; }
        set { opName = value; }
    }

    public OperatorNameMessageHeader()
    {
    }

    public OperatorNameMessageHeader(string operatorName)
    {
        opName = operatorName;
    }

    protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
    {
        writer.WriteElementString("OperatorName", opName);
    }
}

文章所说的一件事是如何读取服务器上的值。根据{{​​3}},您可以使用OperationContext.Current.IncomingMessageHeaders来阅读这些标头。当我在调试器下查看这些MessageHeaders时,我会看到包括我的自定义标题在内的3个标题。因此,它肯定会出现在SOAP数据中。但是,当我致电GetHeader时:

OperatorNameMessageHeader test = msgHeaders.GetHeader<OperatorNameMessageHeader>(OperatorNameMessageHeader.HeaderName, OperatorNameMessageHeader.HeaderNamespace);

然后test.OperatorName为空。基本上,我只是回到一个空的OperatorNameMessageHeader对象,该对象没有从SOAP中的数据反序列化。

我的下一步是运行WCF跟踪工具。当我这样做时,我可以验证自定义标头确实是通过网络发送:

<MessageHeaders>
   <ActivityId CorrelationId="66a7c5b6-3548-4f3c-9120-4484af76b64b" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">f9bef03b-4e7b-4e84-b327-5e79814d9933</ActivityId>
   <OperatorNameMessageHeader xmlns="http://schemas.microsoft.com/scout">
      <OperatorName>Correct Operator Name</OperatorName>
   </OperatorNameMessageHeader>
   <To d4p1:mustUnderstand="1" xmlns:d4p1="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://localhost:90/IRolesAndResourcesManager</To>
   <Action d4p1:mustUnderstand="1" xmlns:d4p1="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IRolesAndResourcesManager/Authenticate</Action>
</MessageHeaders>

因此,服务器拥有数据,我只能获取。这个问题的解决方案是什么?

2 个答案:

答案 0 :(得分:0)

我有类似的问题。我必须从标题中读取用户名和密码。我找到了一个时间解决方案,我正在使用XmlDictionaryReader。但是使用这段代码我只会查找名称,我仍然可以改进它,但暂时可以使用。我有它用于VB,将类似于C#

        Dim username As String = ""
        Dim password As String = ""
        Dim usernameTokenId As String = ""
        Dim passwordType As String = ""

        For i As Integer = 0 To OperationContext.Current.IncomingMessageHeaders.Count - 1
            Dim mhi As Channels.MessageHeaderInfo = OperationContext.Current.IncomingMessageHeaders.Item(i)
            Dim headers As Channels.MessageHeaders = OperationContext.Current.RequestContext.RequestMessage.Headers
            If mhi.Name.Equals("Security") Then
                Dim xr As XmlDictionaryReader = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i)
                xr.MoveToContent()
                While xr.MoveToNextAttribute()
                    Console.Write(" {0}='{1}'", xr.Name, xr.Value)
                End While
                Do
                    Select Case xr.NodeType
                        Case XmlNodeType.Element
                            If xr.LocalName.Equals("Username") Then
                                username = xr.ReadElementContentAsString()
                            End If
                            If xr.LocalName.Equals("Password") Then
                                password = xr.ReadElementContentAsString()
                            End If

                            While xr.MoveToNextAttribute()
                                If xr.LocalName.Equals("Id") Then
                                    usernameTokenId = xr.Value
                                End If
                                If xr.LocalName.Equals("Type") Then
                                    passwordType = xr.Value
                                End If
                            End While
                        Case XmlNodeType.Attribute

                            'Case XmlNodeType.Text
                            '    Console.Write(xr.Value)
                            'Case XmlNodeType.EndElement
                            '    Console.Write("</{0}>", xr.Name)
                    End Select
                Loop While xr.Read()
            End If


            Dim name As String = mhi.Name
        Next

答案 1 :(得分:0)

我遇到了完全相同的问题,并且能够使其工作如下:

[DataContract(Namespace = OperatorNameMessageHeader.HeaderNamespace)]
public class OperatorNameMessageHeader
{
    public const string HeaderName = "OperatorNameMessageHeader";
    public const string HeaderNamespace = "http://schemas.microsoft.com/scout";

    [DataMember]
    public string OperatorName { get; set; }
}

这样,我可以按以下方式读取标题:

public static OperatorNameMessageHeader DeserializeSoap(string xml)
{
    using (var reader = XmlReader.Create(new StringReader(xml)))
    {
        var m = System.ServiceModel.Channels.Message.CreateMessage(reader, int.MaxValue, MessageVersion.Soap11);
        var operatorNameHeader = m.Headers.GetHeader<OperatorNameMessageHeader>(OperatorNameMessageHeader.HeaderName, OperatorNameMessageHeader.HeaderNamespace);

        return operatorNameHeader;
    }
}

注意,我正在使用WCF对XML字符串进行反序列化,因此需要使用[DataContract][DataMember]属性-没有它们,它将无法工作。不知道您是否需要根据实际情况从MessageHeader派生,但对于我来说,这不是读取自定义标头所必需的。

希望这会有所帮助。