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. :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…