Creating a .NET WCF 4.0 JSON Service

I had a lot of trouble getting configuring a .NET 4.0 WCF service which could be called from an HTML page using JQUERY.  The issue was pretty much all with configuring the service and so I thought I would share the web.config that I finally got that allowed the service to work properly.

First, ensure your service methods are decorated as shown:


    public interface ICommonService



        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]

        string[] GetData(string id);



Second, use this as a starting point for your web.config:

<?xml version="1.0"?>



    <customErrors mode="Off" />

    <compilation debug="true" targetFramework="4.0" />








          <serviceMetadata httpGetEnabled="true"/>

          <serviceDebug includeExceptionDetailInFaults="true"/>




        <behavior name="MyServiceBehavior">

          <enableWebScript />





    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />



      <service name="MyJsonWcfService.Service1">

        <endpoint address="" behaviorConfiguration="MyServiceBehavior"

          binding="webHttpBinding" contract="MyJsonWcfService.IService1" >


            <dns value="localhost"/>









    <directoryBrowse enabled="true"/>

  <modules runAllManagedModulesForAllRequests="true"/>