WCF: use JSON to encode SOAP message body

We have SOAP implementations of our services, and so far we have had some legacy code that wrapped our arguments and returned in another object to get around some of the serialization / generics in RPC methods.

After optimization, we implemented this class so that it serializes Json (DataContractJsonSerializer) and GZipped our complex request parameters and response objects.

Now I want to push this stuff onto the WCF stack. What I really want is the ability to encode the message body as GZipped Json on a standard SOAP service. We need transaction support and security and so on, so we need to support standard bindings.

I was able to implement the Operation Behavior serialization to Json so that the message payload contains json compliant XML. Then I wanted to add a MessageEncoder to turn the xml to json into the message body. I'm in trouble here. I can't serialize the whole message as we still have the standard soap headers and so on, and the s: body still contains the root request or response object in front of the json compliant xml. I suppose I can use JsonReaderWriterFactory, just not sure how to apply to the relevant part of the post.

I know I can do GZIP later if I can just figure out how to write and write this Json section.

Any ideas? Pete

OK. So, some clarifications.

My service looks like

[ServiceContract]
public interface IMyService {
  [OperationContract]
  Person SavePerson(Person personToSave);

  [OperationContract]
  Person GetPersons();
}

      

and the result of the GetPersons () method will look something like this:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
  <s:Header>
    <a:Action s:mustUnderstand="1">http://www.petegoo.com/wcf/MyService/IMyService/GetPersonsResponse</a:Action>
    <a:RelatesTo>urn:uuid:a18ccf1c-0793-4240-ba6f-9e86b6f2fdf6</a:RelatesTo>
  </s:Header>
  <s:Body>[{"DateOfBirth":"\/Date(286801200000+1300)\/","FirstName":"Foo","Id":1,"LastName":"Bar"},{"DateOfBirth":"\/Date(333720000000+1200)\/","FirstName":"Foo","Id":1,"LastName":"Bar"}]</s:Body>
</s:Envelope>

      

Note. The above was ad hoc, so it cannot be syntactically or semantically correct.

0


a source to share


2 answers


As I understand it, you like to embed JSON in XML infoset in SOAP? It would be easier to write an example of the request and response you want to achieve.



I see no compelling reason for JSON information to coexist with XML content. However, you can return JSON data as a string value of an XML element / attribute in SOAP.

0


a source


Ahh !! Just understand how old it is. Will go away here if it works for someone else.

I'm new to all of this, but as a suggestion, have you tried looking at MessageInspectors

? You can add them to clientRuntime

and dispatchRuntime

to "play" with SOAP messages before and after WCF submission.



By injecting IDispatchMessageInspector

and / or IClientMessageInspector

, you can receive the call before sending the message, read the current message with soap xml, change the content of the body element to contain your zipped content (perhaps adding it as a new child node), and then rebuild the message and pass it back to WCF to submit. On the receiving side, you will receive a message again in AfterReceiveRequest

and cancel the process.

Don't know if this will work for you, but hope it helps. W

0


a source







All Articles