1

I'm building a WCF service that also has an endpoint for JSON.

The problem is the DataContractSerializer default in WCF uses a standard I don't want to use. I looked into changing this, but I found no good way to do it. All threads and solutions I find for this are pretty old, 2012-2013. Is there a clean solution for this or have Microsoft made some changes to WCF to make this easier than writing your own DispatchMessageFormatter, etc.?

Note I'm not talking about the Web. This is a pure self-hosted WCF service.

I tried already implementing a DispatchMessageFormatter. It works, but it has some issues coming along with it, for example, all WebContentFormat has to be Raw, etc.

This question specifically mentions the accepted answers in that question and asking for another way to do this now five years later without all of the negative side effects it brings.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SomeRandomName
  • 593
  • 1
  • 8
  • 23
  • Additional note: the question might seem off topic for you (talking about web bindings, etc.), but the accepted answer is valid for any WCF service. – Christian.K Jan 11 '17 at 13:44
  • This question specificaly mentions the accepted answers in that question and asking for another way to do this now 5 years later without all of the negative side effects it brings. – SomeRandomName Jan 11 '17 at 14:32
  • "specificaly mentions the accepted answers in that question" - sorry, I didn't see any link in your question. The date alone didn't ring a bell. Still [the answer](http://stackoverflow.com/a/6792899/21567) is _the_ way to do it in WCF. If that isn't good enough for your purposes, which it may very well be, then you should probably layout those issues you talk about more detailed so we can find a more specific solution. – Christian.K Jan 11 '17 at 15:21
  • Mabey that is the answer to my question then, that that is still the way to do it in wcf and rest is pretty limited there. – SomeRandomName Jan 12 '17 at 07:37
  • Yes, and maybe WCF is not the proper tool for you. You might actually look into ASP.NET MVC (no kidding), read something [like this](https://blog.tonysneed.com/2016/01/06/wcf-is-dead-long-live-mvc-6/) or google for "wcf webapi mvc" for more information. – Christian.K Jan 12 '17 at 07:46
  • Yes i agree, i should proabably just go another route. Thanks for your input. I also read about selfhosted webapis using Owin. – SomeRandomName Jan 12 '17 at 09:15

1 Answers1

1

Try to intercept the message in the MessageInspector class (derives from IClientMessageInspector or IDispatchMessageInspector for the client and service respectively). Then convert XML to JSON and send as response.

I have not tried it; it is just a wild guess.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Maximus
  • 3,458
  • 3
  • 16
  • 27