我有一个消息合同,我将传递给我的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的值, 请有人帮我解决这个问题。
答案 0 :(得分:5)
我在Microsoft的Message.ToString()实现中发现了一个缺陷。 然后我找出原因并找到了解决方案。
Message.ToString()可以将Body内容设为“... stream ...”。
这意味着Message是使用从尚未读取的Stream创建的XmlRead或XmlDictionaryReader创建的。
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);
}
-Jesse
答案 1 :(得分:3)
我想你想要
其中一个
(new TypedMessageConverter<MyMessageRequest>()).FromMessage(msg)
将为您提供所需的对象。