Is the WCF request body sometimes a stream and sometimes buffered?

I have a WCF web service. I am trying to do some registration whenever a request is received by injecting MessageInspector and entering the AfterReceiveRequest () event.

For some reason, when I submit a web service request using WCFTestClient.exe, everything works fine. The message is logged and the request is executed as usual.

But when I submit a request for a web service using SOAPUI as a client, making a copy of the request message causes the body to simply show <body>... stream ...</body> and cannot be loaded as an XML document later for verification .

I assume that the request from WCFTestClient.exe was received with a buffered message body, and the request from SOAPUI is accepted as a stream body? How is this possible?

  • Is there any way to write code that allows me to make a copy of any of these versions? I have yet to figure out how to safely copy the streaming version, since CreateBufferedCopy () obviously does not achieve this.

  • Or can I configure WCF to create a buffered message body and never stream?

Here is the code that I use to register and copy the request message:

 object IDispatchMessageInspector.AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext) { try { MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue); request = buffer.CreateMessage(); Message copy = buffer.CreateMessage(); LogRequest(copy); ValidateMessage(ref request); } catch (Exception e) { throw new FaultException<>()... } return null; } 

A copy of the request message cannot be loaded into the XML document in the ValidateMessage () method if it came from a SOAPUI with a streaming body. It is successfully loaded as an XML document if it comes from WCFTestClient.exe with a buffered body.

 void validateMessage(ref System.ServiceModel.Channels.Message message) { XmlDocument bodyDoc = new XmlDocument(); //This load throws exception if request came from SOAPUI with streamed body... bodyDoc.Load(message.GetReaderAtBodyContents()); ... } 

Exception thrown by the Load () method:

System.InvalidOperationException {"The specified node cannot be inserted as a valid child of this node, because the specified node is the wrong type." }

in System.Xml.XmlDocument.AppendChildForLoad (XmlNode newChild, XmlDocument doc) in System.Xml.XmlLoader.LoadDocSequence (XmlDocument parentDoc) in System.Xml.XmlLoader.Load (XmlDocument doc, XmlReml readerWhoo. XmlDocument.Load (XmlReader reader) when ...

+4
source share
3 answers

I believe that SOAPUI always sends requests for messages that it creates as Stream. I do not know for sure if this is something that you can change, either by the code of your SOAPUI test, or by some parameter / SOAPUI configuration file to SOAPUI.

The ckeck TransferMode property of your binding, as described here and here . Perhaps you may have several endpoints using different user bindings for clients that send you a buffered request and scattered requests.

Hope this helps.

+2
source

Which exception is thrown? The reader returned by GetReaderAtBodyContents() is placed in the first element inside the body, not the body tag itself. So you are loading your message incorrectly because the body may contain more than one node, in which case it will fail.

Just to check, could you use the following code to check the contents of the whole message (copy) and see if the body contains the same when sending from SOAPUI?

 using (MemoryStream stream = new MemoryStream()) { using (XmlWriter writer = XmlWriter.Create(stream)) { message.WriteMessage(writer); writer.Flush(); stream.Position = 0; } } 

If you need all the nodes inside the body, you may need to create a Body node yourself.

+1
source

The GetReaderAtBodyContents () method returns any characters between the closing element of the body and the closing element of the soapy shell. XmlReader error with the exception specified earlier in the stream when it reads the last element of the body closure.

More details here: http://www.katlaconsulting.co.uk/blog/wcfxmlschemavalidation

0
source

All Articles