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.
If you open this app.config with svcconfigeditor, it should start asking to select the assembly associated with the NewtonsoftJsonBehavior. If you select that, and save the app.config again, does that resolve your issue?
BTW, we also use a custom binding extension in our service configurations. The xml in the config always underlines the extension with a blue wave indicating that the element is not know to the schema as you describe it. However, when we start the service, both the service and the behavior function as expected.
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:
public class NewtonsoftJsonBehaviorExtension : BehaviorExtensionElement
{
public override Type BehaviorType
{
get { return typeof(NewtonsoftJsonBehavior); }
}
protected override object CreateBehavior()
{
return new NewtonsoftJsonBehavior();
}
}
This wasn't sufficient to get rid of my custom WebServiceHostFactory, of course. For I also had to add a custom ContentTypeMapper:
public class NewtonsoftJsonContentTypeMapper : WebContentTypeMapper
{
public override WebContentFormat GetMessageFormatForContentType(string contentType)
{
return WebContentFormat.Raw;
}
}
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:
<extensions>
<behaviorExtensions>
<add name="newtonsoftJsonBehavior" type="Newtonsoft.Json.Extensions.NewtonsoftJsonBehaviorExtension, NewtonsoftJsonExtensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<behaviors>
<endpointBehaviors>
<behavior name="jsonRestEndpointBehavior">
<webHttp/>
<newtonsoftJsonBehavior/>
</behavior>
</endpointBehaviors>
<behaviors>
Then configuring a webHttpBinding with my custom contentTypeMapper:
<bindings>
<webHttpBinding>
<binding name="newtonsoftJsonBinding" contentTypeMapper="Newtonsoft.Json.Extensions.NewtonsoftJsonContentTypeMapper, NewtonsoftJsonExtensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</webHttpBinding>
</bindings>
Finally setting up an endpoint utilizing the above:
<services>
<service name="My.Namespaced.MyService" behaviorConfiguration="jsonRestServiceBehavior">
<endpoint address="" behaviorConfiguration="jsonRestEndpointBehavior"
binding="webHttpBinding" bindingConfiguration="newtonsoftJsonBinding"
contract="My.Namespaced.IMyService" />
</service>
</services>
Hope this stuff will help somebody out there. :)