Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6789509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:36:46+00:00 2026-05-26T17:36:46+00:00

I have a working application using Json.NET (newtonsoft) as a custom serializer. Currently I’m

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:36:47+00:00Added an answer on May 26, 2026 at 5:36 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on web application using VB in ASP.NET. Right now I have
I have a small VB.NET application that I'm working on using the full version
I have been working on on an AJAX chat application using php, mysql. It's
Greetings, I'm working in windows application using C#. I have typed-dataset called packetsDBDataSet and
I'm working on a web application using JSP/Servlets, etc. And I have a lot
I am working on windows. I have built a twitter application using the twitter
I have an application that is working as a proxy using QTcpSocket to intercept
I have been working with Visual Studio (WinForm and ASP.NET applications using mostly C#)
I am trying to make an ajax json callback using a .net application. So
Hi I am working Android application development using titanium studio.I have developed small application.my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.