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?
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.