Custom Behavior won't register in my web.confi

2019-01-22 19:15发布

问题:

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!

回答1:

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. :)



回答2:

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.