Parse multicast messages using the WCF special tool for WCF

I am writing a WCF client to a SOAP service that returns the result of a mime animation with binary data (in fact, a PDF file). It uses a special message encoder.

The service does not seem to mind if I make the request a single-part format, so I can return the result. As a result, I see two problems:

  • It only seems to return the first part of a message with several parts.
  • The data that I receive cannot be decoded by my custom encoder.

I tried using MTOM binding, but that messed up the request. It cannot add the border parameter to the content type, so the server cannot understand the request.

I think what I want is a basic SOAP text request, but the response is decoded in MTOM style. However, I do not know how to do this.

The closest solution I found is this: http://blogs.msdn.com/b/carlosfigueira/archive/2011/02/16/using-mtom-in-a-wcf-custom-encoder.aspx

But this seems like a very invasive change in my project.

+4
source share
1 answer

I get it. First of all, I was incorrect when I said that I was getting the first part of a message with several parts using the MTOM encoder; I got it. I looked at it in the debugger, and the bottom should be trimmed in the debug viewer. Note this to my inexperience by manually looking and decrypting frequent messages.

At the second moment, all I had to do was use the MTOM encoder when the Content-Type was multipart / related and everything worked fine. If you read the article mentioned above, all this concerns the dynamic determination of whether the message is plural or plain text, and on the basis of this selects the correct encoder. In fact, it is a custom encoder with a built-in encoding encoder and built-in MTOM codec and switches back and forth based on the type of content of the incoming message.

Our project requires some subsequent processing of the response message before it is passed to the main program logic. Thus, we get the incoming SOAP content as an XML string and do some XML manipulation.

This is a slight deviation from the solution recommended in the article. All that is required in solving the article is reading the message using the correct encoder in System.ServiceModel.Channels.Message and returning it. In our solution, we need to interrupt this process and perform post-processing.

To do this, follow these steps in a custom encoder:

public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType) { //First, get the incoming message as a byte array var messageData = new byte[buffer.Count]; Array.Copy(buffer.Array, buffer.Offset, messageData, 0, messageData.Length); bufferManager.ReturnBuffer(buffer.Array); //Now convert it into a string for post-processing. Look at the content-type to determine which encoder to use. string stringResult; if (contentType != null && contentType.Contains("multipart/related")) { Message unprocessedMessageResult = this.mtomEncoder.ReadMessage(buffer, bufferManager, contentType); stringResult = unprocessedMessageResult.ToString(); } else { //If it not a multi-part message, the byte array already has the complete content, and it simply needs to be converted to a string stringResult = Encoding.UTF8.GetString(messageData); } Message processedMessageResult = functionToDoPostProccessing(stringResult); return processedMessageResult; } 
+5
source

All Articles