如何从System.ServiceModel.Channels.Message获取消息内容?

Emb*_*rja 3 wcf

我有一个传递给wcf服务的消息合同,并且有一个消息检查器,我正在使用该消息检查器查找wcf客户端发送的消息。我有消息,但我不知道如何从中获取数据。以下是我传递给wcf服务的消息请求。

[MessageContract]
public  class MyMessageRequest
{
    [MessageBodyMember]
    public string Response
    {
        get;
        set;
    }

    [MessageHeader]
    public string ExtraValues
    {
        get;
        set;
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到消息的方法如下:

public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
        MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);

        request = buffer.CreateMessage();
        Console.WriteLine("Received:\n{0}", buffer.CreateMessage().ToString());
        return null;
}
Run Code Online (Sandbox Code Playgroud)

我想从消息中看到Response和ExtraValues的值,请任何人在此帮助我。

Jes*_*olm 6

我在Microsoft的Message.ToString()实现中发现了一个漏洞。然后,我找出了原因并找到了解决方案。

Message.ToString()的正文内容可能为“ ... stream ...”。

这意味着消息是使用XmlRead或XmlDictionaryReader创建的,该XmlRead或XmlDictionaryReader是从尚未读取的Stream创建的。

ToString被记录为不更改消息状态。因此,他们不阅读Stream,只是在上面放了一个标记。

由于我的目标是(1)获取字符串,(2)更改字符串,以及(3)从更改后的字符串创建新消息,因此我需要做一些额外的工作。

这是我想出的:

/// <summary>
/// Get the XML of a Message even if it contains an unread Stream as its Body.
/// <para>message.ToString() would contain "... stream ..." as
///       the Body contents.</para>
/// </summary>
/// <param name="m">A reference to the <c>Message</c>. </param>
/// <returns>A String of the XML after the Message has been fully
///          read and parsed.</returns>
/// <remarks>The Message <paramref cref="m"/> is re-created
///          in its original state.</remarks>
String MessageString(ref Message m)
{
    // copy the message into a working buffer.
    MessageBuffer mb = m.CreateBufferedCopy(int.MaxValue);

    // re-create the original message, because "copy" changes its state.
    m = mb.CreateMessage();

    Stream s = new MemoryStream();
    XmlWriter xw = XmlWriter.Create(s);
    mb.CreateMessage().WriteMessage(xw);
    xw.Flush();
    s.Position = 0;

    byte[] bXML = new byte[s.Length];
    s.Read(bXML, 0, s.Length);

    // sometimes bXML[] starts with a BOM
    if (bXML[0] != (byte)'<')
    {
        return Encoding.UTF8.GetString(bXML,3,bXML.Length-3);
    }
    else
    {
        return Encoding.UTF8.GetString(bXML,0,bXML.Length);
    }
}
/// <summary>
/// Create an XmlReader from the String containing the XML.
/// </summary>
/// <param name="xml">The XML string o fhe entire SOAP Message.</param>
/// <returns>
///     An XmlReader to a MemoryStream to the <paramref cref="xml"/> string.
/// </returns>
XmlReader XmlReaderFromString(String xml)
{
    var stream = new System.IO.MemoryStream();
    // NOTE: don't use using(var writer ...){...}
    //  because the end of the StreamWriter's using closes the Stream itself.
    //
    var writer = new System.IO.StreamWriter(stream);
    writer.Write(xml);
    writer.Flush();
    stream.Position = 0;
    return XmlReader.Create(stream);
}
/// <summary>
/// Creates a Message object from the XML of the entire SOAP message.
/// </summary>
/// <param name="xml">The XML string of the entire SOAP message.</param>
/// <param name="">The MessageVersion constant to pass in
///                to Message.CreateMessage.</param>
/// <returns>
///     A Message that is built from the SOAP <paramref cref="xml"/>.
/// </returns>
Message CreateMessageFromString(String xml, MessageVersion ver)
{
    return Message.CreateMessage(XmlReaderFromString(xml), ver);
}
Run Code Online (Sandbox Code Playgroud)

-杰西