I've been searching for hours to no avail.
I'm trying to use JSON.net as the default serializer and deserializer for WCF web services (.svc) on an ASP.NET website. To be clear, this is not an MVC site. Just plain old ASP.NET 4.0 hosted inside IIS 7.
My original motivation was to use JSON.net's superior date handling but now I just want to see if it can be done.
Below is a simple webservice that can return a date or get a date. This service is called from a page using jQuery ajax.
What I'd like to do is somehow configure ASP.NET to always use the JSON.net de/serialization routines instead of the built-in DataContractJsonSerializer or whatever is actually being called. I'm aware that I can just pass and return strings to my service methods, handling the serialization manually, but I'd like to avoid that if possible.
For all I know this could be impossible because .svc's internalize the serialization routines. That'd be an acceptable answer too.
Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using System.Text;
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
DateTime GetDateTime();
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
void SendDate(DateTime dt);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;
using AutoMapper;
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IService
{
public DateTime GetDateTime()
{
DateTime dt = new DateTime(2012, 01, 02, 03, 04, 05);
return dt;
}
public void SendDate(DateTime dt)
{
DateTime d = dt;
//Do something with the date
}
}
Service.svc
<%@ ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
Web.config relevant lines:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
</system.serviceModel>