gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 22:37:59 24 4
gpt4 key购买 nike

我有一个消息契约(Contract),我将它传递给我的 wcf 服务,并且我有一个消息检查器,我用它来查找 wcf 客户端发送的内容。
我有消息,但我不知道如何从中获取数据。
以下是我传递给 wcf 服务的消息请求。

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

[MessageHeader]
public string ExtraValues
{
get;
set;
}
}

我获取消息的方法如下:
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;
}

我想从消息中看到 Response 和 ExtraValues 的值,
请任何人帮我解决这个问题。

最佳答案

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

Message.ToString() 的正文内容可能是“...流...”。

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

ToString 被记录为不改变消息的状态。
所以,他们不阅读流,只是放入一个标记。

由于我的目标是 (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);
}

-杰西

关于wcf - 如何从 System.ServiceModel.Channels.Message 获取消息内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1319043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com