none
Sending Custom Message through IClientMessageFormatter RRS feed

  • Question

  • Hello,

    We are trying to send a custom SOAP message by using IClientMessageFormatter. We are overriding the OnWriteStartBody method to create custom XML body tags with prefixes.

    The custom message is created in the SerlializeRequest method of IClientMessageFormatter.

    public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
            {
                Message message = this.formatter.SerializeRequest(messageVersion, parameters);
                return new CustomMessage(message);
            }

    Use of this custom message introduces an Ellipsis (...) in the SOAP body just before the closing tag. The ellipsis is present even without adding any custom content in the CustomMessage object. If we use, the default Message in SerializeRequest, ellipsis is not present. Any help on removing the ellipsis is appreciated.

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
      <s:Header>
        <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://action url</Action>
      </s:Header>
      <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">...</s:Body>
    </s:Envelope>


    • Edited by Shrek 2 Tuesday, May 31, 2016 8:33 PM
    Tuesday, May 31, 2016 8:32 PM

All replies

  • Hi Shrek 2,

    According to your description, it seems that you want to replace ellipsis to empty on your message body. If so, you could use the following method to achieve it.

    public Message ChangeString(Message oldMessage, string from, string to)
    
            {
    
                MemoryStream ms = new MemoryStream();
    
                XmlWriter xw = XmlWriter.Create(ms);
    
                oldMessage.WriteMessage(xw);
    
                xw.Flush();
    
                string body = Encoding.UTF8.GetString(ms.ToArray());
    
                xw.Close();
    
    
                body = body.Replace(from, to);
    
    
                ms = new MemoryStream(Encoding.UTF8.GetBytes(body));
    
                XmlDictionaryReader xdr = XmlDictionaryReader.CreateTextReader(ms, new XmlDictionaryReaderQuotas());
    
                Message newMessage = Message.CreateMessage(xdr, int.MaxValue, oldMessage.Version);
    
                newMessage.Properties.CopyProperties(oldMessage.Properties);
    
                return newMessage;
    
            } 
    

    Usage:

    public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
    
            {
    
                Message message = this.formatter.SerializeRequest(messageVersion, parameters);
    
                return new ChangeString(message, "...", "");
    
            }
    

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 1, 2016 9:17 AM
  • Hi Cole,

    We are not writing the ellipsis to the message body. It is introduced by the  System.ServiceModel.Channels.Message Class when we inherit it, in our custom message class. Is this a bug or something incorrect in the hooking up of the message class?

    The fix you have provided is a string manipulation approach and does not address the root cause of the problem.

    Thanks.

    Wednesday, June 1, 2016 2:02 PM
  • Hi Shrek 2,

    According to your description, do you mean that when you use IClientMessageFormatter to custom your messages, it generates an ellipsis by automatically? Could you please share a simple demo about the issue via OneDriver? We’ll reproduce your issue on our side and try to find a solution to solve it.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 6, 2016 9:22 AM
  • Hi Cole,

    Yes, the ellipsis are introduced automatically when we use IClientMessageFormatter.

    We are able to recreate the issue, but unable to upload the demo project file on OneDrive. Please provide an alternate method of sharing the project. Below are the issue details.

    We are using a custom endpoint behavior to implement IClientMessageInspector, to inspect the outgoing messages. This combination of IClientMessageFormatter and IClientMessageInspector is introducing the ellipsis.

    When the outgoing message is intercepted in the BeforeSendRequest (below), the ellipsis are present. This is because the message is customized using the IClientMessageFormatter. If the message is not customized, ellipsis are not present.

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
            {
                XmlDocument doc = new XmlDocument();
                doc.PreserveWhitespace = true;
                doc.LoadXml(request.ToString());
    
                XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
    
                XmlDictionaryReader xdr = XmlDictionaryReader.CreateTextReader(Encoding.UTF8.GetBytes(doc.InnerXml), quotas);
    
                Message newMessage = Message.CreateMessage(xdr, System.Int32.MaxValue, request.Version);
                request = newMessage;
                
                return null;
            }
    Thanks!

    Tuesday, June 7, 2016 6:35 PM
  • Hi Shrek 2,

    >> We are able to recreate the issue, but unable to upload the demo project file on OneDrive. Please provide an alternate method of sharing the project. Below are the issue details.

    Please refer to the following steps about uploading your project via OneDrieve.

    1. On the OneDrive website sign in with your Microsoft account, and then browse to the location where you want to add the files.

    2. Select Upload.

    3. Select the files you want to upload, and then select Open.

    For more information, please refer to:

    https://support.office.com/en-us/article/Upload-photos-and-files-to-OneDrive-b00ad3fe-6643-4b16-9212-de00ef02b586

    After you upload the files, please share the files or folder.

    1. Pick the file or folder you want to share by selecting the circle in the upper corner of the item. You can also pick multiple items to share them together.

    2. Tap or click Share at the top of the page.

    3. In the Share box that appears, choose one of the sharing options: Get a link or Email

    4. Tap or click Get a link.

    5. Choose the type of link you want from the drop-down list above the Get a link button. The link automatically updates when you choose a different option from the drop-down list.

    6. Tap or click Copy to copy the link your clipboard.

    7. Paste the link in an email message or wherever else you’re sharing the link. To post the link on here.

    For more information, please refer to:

    https://support.office.com/en-us/article/Share-OneDrive-files-and-folders-9fcc2f7d-de0c-4cec-93b0-a82024800c07

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, June 15, 2016 2:35 AM
  • Hi Cole,

    Demo project is shared at the below location on OneDrive.

    https://1drv.ms/u/s!AoNhEibW1kNShh5zhiZWnPczKdeU

    Thanks.

    Friday, June 24, 2016 11:00 AM
  • Hi!

    Thank you for sharing the demo project.  The issue that you're seeing is by design.

    What's happening is that you're relying to the ToString() method of the Message (request) to generate a valid XML document, and this is not possible.  A message itself can't be converted into a string just like that.  ToString() is used mostely for debugging purposes so it's expected that you see the ellipsis.  The way you should think of all this is more like a stream.

    Here's the code that you want to use:

            public object BeforeSendRequest(ref Message request, IClientChannel channel)
            {
                XmlDocument doc = new XmlDocument();
                doc.PreserveWhitespace = true;
    
                MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
                Message msg = buffer.CreateMessage();
    
                XmlWriterSettings xmlWriterSettings = new XmlWriterSettings()
                {
                    Encoding = new UTF8Encoding(false)
                };
    
                string result;
                using (MemoryStream output = new MemoryStream())
                {
                    using (XmlWriter writer = XmlWriter.Create(output, xmlWriterSettings))
                    {
                        writer.WriteStartDocument();
                        msg.WriteMessage(writer);
                    }
    
                    result = Encoding.UTF8.GetString(output.ToArray());
                }
    
                doc.LoadXml(result);
    
                XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
    
                XmlDictionaryReader xdr = XmlDictionaryReader.CreateTextReader(Encoding.UTF8.GetBytes(doc.InnerXml), quotas);
    
                Message newMessage = Message.CreateMessage(xdr, System.Int32.MaxValue, request.Version);
                request = newMessage;
    
                return null;
            }

    Thanks,

    Jorge del Conde (MSFT)


    Wednesday, July 20, 2016 1:18 AM