IDispatchMessageInspector用于记录纯文本soap消息?

时间:2010-01-06 16:49:49

标签: c# xml wcf soap

我编写了一个WCF IDispatchMessageInspector,因此我可以记录输入和传出的消息。但是我不知道如何获得一个格式良好的XML字符串来记录。

我的代码看起来像这样:

        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
        request = buffer.CreateMessage();
        Log("Received", request.ToString();
        return null;
    }

此日志记录的结​​果包括“ampersand lt;”之类的内容以及某种二进制编码数据。

如何获得看起来像标准soap XML文档的内容?我知道这应该是显而易见的,但我太密集了,无法弄明白。

感谢。

2 个答案:

答案 0 :(得分:1)

您是否有任何特定原因在使用XmlTextWriter输出的跟踪功能中不使用.Net构建?

如果没有具体原因,请查看http://msdn.microsoft.com/en-us/library/ms730064.aspx

希望这有帮助,

答案 1 :(得分:1)

这是一个工作示例

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

    XDocument doc;

    using (MemoryStream ms = new MemoryStream())
    {
        XmlWriter writer = XmlWriter.Create(ms);
        request.WriteMessage(writer);
        writer.Flush();
        ms.Position = 0;

        doc = XDocument.Load(ms);
    }

    if (SaveLog != null)
    {
        LogSaveFileEventArgs logEventArgs = new LogSaveFileEventArgs(doc, false);
        SaveLog(this, logEventArgs);
    }

    request = buffer.CreateMessage();

    //The return value can be any object that you want to use for correlation purposes;
    //it is returned to you as the correlationState parameter in the BeforeSendReply method.
    return null;
}