title: Replace default JSON serializer in WCF 4 to JSON.NET
body: <p>I want to replace the default WCF JSON (for all data types) serialization with JSON.NET.
  I've searched all over the net and couldn't find a working solution.</p>
  
  <p>This is my object:</p>
  
  <pre><code>    [JsonObject]
  public class TestObject
  {
      [JsonProperty("JsonNetName")]
      public string Name = "John";
  
      [JsonProperty]
      public DateTime Date = DateTime.Now;
  }
  </code></pre>
  
  <p>This is my WCF function:</p>
  
  <pre><code>    [OperationContract]
      [WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
      List&lt;TestObject&gt; Get();
  </code></pre>
  
  <p>This is the code in Global.asax:</p>
  
  <pre><code>        protected void Application_Start(object sender, EventArgs e)
      {
          // Create Json.Net formatter serializing DateTime using the ISO 8601 format
          var serializerSettings = new JsonSerializerSettings();
  
          serializerSettings.Converters.Add(new IsoDateTimeConverter());
          serializerSettings.Converters.Add(new BinaryConverter());
          serializerSettings.Converters.Add(new JavaScriptDateTimeConverter());
          serializerSettings.Converters.Add(new BinaryConverter());
          serializerSettings.Converters.Add(new StringEnumConverter());
  
          var config = HttpHostConfiguration.Create().Configuration;
  
          Microsoft.ApplicationServer.Http.JsonMediaTypeFormatter jsonFormatter = config.OperationHandlerFactory.Formatters.JsonFormatter;
  
          config.OperationHandlerFactory.Formatters.Remove(jsonFormatter);
  
          config.OperationHandlerFactory.Formatters.Insert(0, new JsonNetMediaTypeFormatter(serializerSettings));
  
          var httpServiceFactory = new HttpServiceHostFactory
          {
              OperationHandlerFactory = config.OperationHandlerFactory,
              MessageHandlerFactory = config.MessageHandlerFactory
          };
  
          //Routing
          RouteTable.Routes.Add(
             new ServiceRoute(
                 "Brands", httpServiceFactory,
                 typeof(Brands)));
  
        }
  </code></pre>
  
  <p>This is Web.Config:</p>
  
  <pre><code> &lt;endpointBehaviors&gt;
      &lt;behavior name="Behavior_Brands"&gt;
        &lt;webHttp defaultOutgoingResponseFormat="Json" defaultBodyStyle="Bare" /&gt;
      &lt;/behavior&gt;
    &lt;/endpointBehaviors&gt;
  </code></pre>
  
  <p>and the services section:</p>
  
  <pre><code>&lt;service name="TestApp.CoreWCF.Brands"&gt;
      &lt;endpoint address="" behaviorConfiguration="Behavior_Brands" binding="webHttpBinding" contract="TestApp.CoreWCF.IBrands"&gt;
        &lt;identity&gt;
          &lt;dns value="localhost" /&gt;
        &lt;/identity&gt;
      &lt;/endpoint&gt;
    &lt;/service&gt;
  </code></pre>
  
  <p>And finally, this is what I'm getting when launching the URL: </p>
  
  <p>"<a href="http://localhost:30000/Brands/Get" rel="nofollow noreferrer">http://localhost:30000/Brands/Get</a>"</p>
  
  <pre><code>[{"Date":"\/Date(1354364412708+0200)\/","Name":"John"}, {"Date":"\/Date(1354364412708+0200)\/","Name":"John"}]
  </code></pre>
  
  <p>The JSON answer obviously ignores the JSON.NET attributes.</p>