/// <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) { // ECT - Note: this whole thing falls apart with the voidMethod call, which should not really matter to our needs. // For the moment I have commented that call from the Client Sample, but it is important to note. // 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; // We need to consider the maximum length of a message for this, I expect. byte[] bXML = new byte[s.Length]; s.Read(bXML, 0, (int)s.Length); if (bXML[0] != (byte)'<') { return(Encoding.UTF8.GetString(bXML, 3, bXML.Length - 3)); } else { return(Encoding.UTF8.GetString(bXML, 0, bXML.Length)); } }
private void MessageToFile(ref Message message, string fileName) { // We copy the message to a buffer, and because it can only be used once, must create the message again from the buffer to // allow the SOAP service to get access to the message. This is only necessary because we print the stream details. MessageBuffer buffer = message.CreateBufferedCopy(Int32.MaxValue); message = buffer.CreateMessage(); // Create a copy of the buffered message for use in the print so that we don't touch the original 'message' object. var copy = buffer.CreateMessage(); // Create a string representation of the message to be written to a file. string strMessage = MessageString(ref copy); // Write the output. using (System.IO.FileStream outStream = new System.IO.FileStream($"{fileName}.txt", System.IO.FileMode.OpenOrCreate)) { StreamWriter myWriter = new StreamWriter(outStream); myWriter.Write(strMessage); myWriter.Close(); } }