0

There is a WCF REST service hosted in Windows Service or in command line executable. I would like to replace the default DataContractJsonSerializer with JSON.NET serializer.

There is a similar question with a minor difference, that it uses IIS: How to set Json.Net as the default serializer for WCF REST service.

Code proposed there does not work when hosted outside the IIS. It throws the following exception: 'ServiceHostingEnvironment.EnsureServiceAvailable' cannot be invoked within the current hosting environment. This API requires that the calling application be hosted in IIS or WAS.

Any idea?

Community
  • 1
  • 1
Boris Modylevsky
  • 3,029
  • 1
  • 26
  • 42

1 Answers1

2

That code should work; just remove the [AspNetCompatibilityRequirements] attribute from the service, since it doesn't really apply for self-hosted services. You can also use a custom message formatter to replace the serializer, as described at http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/03/wcf-extensibility-message-formatters.aspx.

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • Thanks for answering. I gave it up for the moment and will go with the DataContractJsonSerializer for a while. I'll accept your answer as it sounds logical and I'll check it later on maybe. – Boris Modylevsky Apr 01 '12 at 10:57