2009-08-23 5 views
2

나는 wcf 서비스에 전달하는 메시지 계약을 가지고 있으며 wcf 클라이언트가 보낸 메시지를 찾기 위해 메시지 관리자를 사용하고있다. 메시지가 있지만 데이터를 가져 오는 방법을 모르겠습니다. 다음은 wcf 서비스에 전달할 내 메시지 요청입니다.System.ServiceModel.Channels.Message에서 메시지 콘텐츠를 가져 오는 방법?

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

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

난 다음되는 메시지를 얻고있다 방법은 :

라는 메시지 중 응답의 가치와 ExtraValues을보고 싶어
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; 
} 

, 사람이 저를 도와주세요.

답변

3

은 내가

(new TypedMessageConverter<MyMessageRequest>()).FromMessage(msg) 

는 당신이 필요로하는 개체를 다시 줄 것이다 어디

http://msdn.microsoft.com/en-us/library/system.servicemodel.description.typedmessageconverter.frommessage.aspx

을 원하는 생각합니다.

+1

내가 어떤 일반적인 TypedMessageConverter를 찾을 수 없습니다 :

는 여기에 내가 무엇을 최대 온입니다. 그것은 어디에 있습니까, 네가 네임 스페이스를 말해 줄 수 있니? –

+1

네임 스페이스는 URL과 링크 된 문서 페이지의 맨 위에 표시됩니다 (System.ServiceModel.Description). – Brian

2

Microsoft의 Message.ToString() 구현에서 발견했습니다. 나는 그 원인을 알아 냈고 해결책을 찾았습니다.

Message.ToString() 의 Body 내용은 "... 스트림 ..."입니다.

메시지가 아직 읽지 않은 스트림에서 만든 XmlRead 또는 XmlDictionaryReader를 사용하여 만들어 졌음을 의미합니다.

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(); 
    SmlWriter xw = CmlWriter.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

관련 문제