I have a working application using Json.NET (newtonsoft) as a custom serializer. Currently I'm adding this derivative of WebHttpBehavior in a custom WebServiceHostFactory. See the code snippet at the end of this blog for how I've attached it.
As I'm hosting this service in IIS, I would like to get rid of my custom hosting code and simply add the custom behavior to my web.config. The procedure is shown in this msdn article.
So I try to do that like so:
<behaviors>
<endpointBehaviors>
<behavior name="jsonRest">
<webHttp defaultOutgoingResponseFormat="Json" />
<NewtonsoftJsonBehavior/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="NewtonsoftJsonBehavior" type="Newtonsoft.Json.Extensions.NewtonsoftJsonBehavior, NewtonsoftJsonExtensions, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
Sadly, I'm unable to make that work. When I do that, Visual Studio tells me that
The element 'behavior' has invalid child element 'NewtonsoftJsonBehavior'
In the afforementioned msdn article, it's said that
To add configuration abilities to the element, you need to write and register a configuration element. For more information on this, see the System.Configuration documentation.
After the element and its configuration type are defined, the extension can be used, as shown in the following example.
I've got this feeling that what I'm missing is exactly that. Somehow registering the element and its configuration type. Sadly I can't make heads or tails of the System.Configuration which is supposed to tell me how to do this. So that's basically my question:
How do I write and register the configuration element, and if that's not my problem, what is the problem?
Many thanks in advance!
The missing piece is the class BehaviorExtensionElement. In the OP I was attempting to add the WebHttpBehavior-derivative as an element. The BehaviorExtensionElement tells the config-parser which Type to use for a certain element.
Here's the implementation I needed:
This wasn't sufficient to get rid of my custom WebServiceHostFactory, of course. For I also had to add a custom ContentTypeMapper:
I could then use them in my Web.config. Here are the relevant parts of the working config. Firstly setting up the extension and configuring a behavior with it:
Then configuring a webHttpBinding with my custom contentTypeMapper:
Finally setting up an endpoint utilizing the above:
Hope this stuff will help somebody out there. :)