There is an external SOAP service I must interact with.
To this end I opened VisualStudio, right clicked References, clicked "Add service reference", entered the address of the WSDL document and clicked OK. This worked, VisualStudio generated for me a library - a WCF client - that can be used to interact with the said external API.
The only problem is that sometimes I was getting a System.ServiceModel.CommunicationException: Unrecognized message version.
Fiddler showed me the reason. This is the response of the server:
<h1>CException</h1>
<p>A non-numeric value encountered</p>
OK, that's definitely NOT a SOAP response, no wonder .NET refuses to parse it. But in this case can I please be able to retrieve the raw response? (perhaps it would make sense to retrieve the error from HTML tags and show it to the user)
What I tried: I read this article from the Microsoft documentation: How to: Inspect or Modify Messages on the Client It says:
To inspect or modify messages
- Implement the
System.ServiceModel.Dispatcher.IClientMessageInspector
interface.- Implement a
System.ServiceModel.Description.IEndpointBehavior
orSystem.ServiceModel.Description.IContractBehavior
depending upon the scope at which you want to insert the client message inspector.System.ServiceModel.Description.IEndpointBehavior
allows you to change behavior at the endpoint level.System.ServiceModel.Description.IContractBehavior
allows you to change behavior at the contract level.- Insert the behavior prior to calling the
ClientBase<TChannel>.Open
or theICommunicationObject.Open
method on theSystem.ServiceModel.ChannelFactory<TChannel>
. For details, see Configuring and Extending the Runtime with Behaviors.
(this is also what this SO answer suggests)
I tried both IContractBehavior
and IEndpointBehavior
. The result is, unfortunately, the same: the CommunicationException
is thrown before my custom behaviors are called and therefore I cannot get the raw response. (If the response is a SOAP message then the exception is not thrown, my behaviors are called and I can get the raw response, but in such a case I have no interest in it :( )
Is there any way to get the raw response even if it is not a SOAP message?