Search code examples
wcfxml-serializationdatacontractserializer

WCF Service ref string param that accepts XML?


I'm writing a WCF service that receives events. It's to an agreed standard so I've got to stick to the service definition, and I don't control the data the clients send. Again this is to an agreed standard although the data can vary.

Here's one of the methods on my service:

complexType ErrorEvent(int requestId, complexType returnValue, ref string errorInfo)

Clients send XML in the errorInfo string that my function will manipulate and return.

The data I get is like this (full SOAP request):

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ErrorEvent xmlns="http://blah">
      <requestId>1</requestId>
      <returnValue>
        <returnCode>0</returnCode>
      </returnValue>
      <errorInfo>
        <ErrorMessage>An error message</ErrorMessage>
        <DefaultTask><!-- Complex data --></DefaultTask>
        <Task><!-- Complex data --></Task>
        <Task><!-- Complex data --></Task>
        <Task><!-- Complex data --></Task>
        <ExtraMessage>hello</ExtraMessage>
        <ExtraMessage>world</ExtraMessage>
      </errorInfo>
    </ErrorEvent>
  </s:Body>
</s:Envelope>

However, when I try and run this I get this error (edited):

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter errorInfo. The InnerException message was 'There was an error deserializing the object of type System.String. End element 'errorInfo' from namespace '' expected. Found element 'ErrorMessage' from namespace ''.

So my question is, is there any way I achieve what I want to do without altering the signature of my method? For example adding attributes to my service etc? Or do I need to intercept the message?

Thanks for any pointers.


Solution

  • I didn't find a way to make this work and as @luksan says, it's a misuse by a client to send unescaped XML to a string parameter.

    The workaround I adopted was to make a class that implements IClientMessageInspector, IDispatchMessageInspector and IEndpointBehavior to intercept, check and modify any messages that were incorrect.

    If I could have changed the interface, another workaround would have been to accept XmlNode[] instead of string.