title: Consuming JSON in WCF service method
body: <p>In a larger project I am having trouble getting a WCF service method to consume a JSON parameter. So I produced a smaller test case and the behaviour is echoed. If I debug the service I can see the parameter value is null at the service call. Fiddler confirms that the JSON is being sent and JsonLint confirms it is valid.</p>
  
  <p>Code below with annotations from debugging.</p>
  
  <pre><code>[ServiceContract]
  public interface IWCFService
  {
  
      [OperationContract]
      [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,
              ResponseFormat = WebMessageFormat.Json,
              UriTemplate = "getstring")]
  
      string GetString();
  
      [OperationContract]
      [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,
          ResponseFormat = WebMessageFormat.Json,
          UriTemplate = "getplayer")]
      //[WebGet(BodyStyle = WebMessageBodyStyle.WrappedRequest,
      //    ResponseFormat = WebMessageFormat.Json,
      //    UriTemplate = "getplayers")]
      Player GetPlayer();
  
      [OperationContract]
      [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,
          ResponseFormat = WebMessageFormat.Json,
          UriTemplate = "getplayers")]
      List&lt;Player&gt; GetPlayers();
  
      [OperationContract]
      [WebInvoke(
          Method = "POST",
          BodyStyle = WebMessageBodyStyle.Wrapped,
          ResponseFormat = WebMessageFormat.Json,
          RequestFormat = WebMessageFormat.Json,
          UriTemplate = "totalscore")]
      string TotalScore(Player player);
  
  }
  </code></pre>
  
  <p>... and its implementation</p>
  
  <pre><code>public class WCFService : IWCFService
  {
      public string GetString()
      {
          return "hello from GetString";
      }
  
      public Player GetPlayer()
      {
          return new Player()
                  {
                      Name = "Simon", 
                      Score = 1000, 
                      Club = new Club()
                              {
                                  Name = "Tigers", 
                                  Town = "Glenelg"
                              }
                  };
      }
  
      public List&lt;Player&gt; GetPlayers()
      {
          return new List&lt;Player&gt;()
              {
                  new Player()
                      {
                          Name = "Simon", 
                          Score = 1000 , 
                          Club=new Club()
                                  {
                                      Name="Tigers", 
                                      Town = "Glenelg"
                                  }
                      }, 
                  new Player()
                      {
                          Name = "Fred", Score = 50,
                          Club=new Club()
                                  {
                                      Name="Blues",
                                      Town="Sturt"
                                  }
                      }
              };
      }
  
      public string TotalScore(Player player)
      {
          return player.Score.ToString();
      }
  }
  </code></pre>
  
  <p>Calling any of the first three methods works correctly (but no parameters as you'll note). Calling the last method (TotalScore) with this client code ...</p>
  
  <pre><code>function SendPlayerForTotal() {
          var json = '{ "player":{"Name":"' + $("#Name").val() + '"'
              + ',"Score":"' + $("#Score").val() + '"'
              + ',"Club":"' + $("#Club").val() + '"}}';
  
          $.ajax(
          {
              type: "POST",
              contentType: "application/json; charset=utf-8",
              url: "http://localhost/wcfservice/wcfservice.svc/json/TotalScore",
              data: json,
              dataType: "json",
              success: function (data) { alert(data); },
              error: function () { alert("Not Done"); }
          });
      }
  </code></pre>
  
  <p>... results in ...</p>
  
  <p>There was an error while trying to deserialize parameter <a href="http://tempuri.org/:player" rel="noreferrer">http://tempuri.org/:player</a>. The InnerException message was 'Expecting state 'Element'.. Encountered 'Text'  with name '', namespace ''. '.</p>
  
  <p>I have tried sending an unwrapped version of the JSON ...</p>
  
  <p>{"Name":"Simon","Score":"100","Club":"Rigby"}</p>
  
  <p>but at the service the parameter is null, and no formatter exceptions.</p>
  
  <p>This is the system.serviceModel branch of the service web.config:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
  &lt;services&gt;
      &lt;service name="WCFService.WCFService" behaviorConfiguration="WCFService.DefaultBehavior"&gt;
          &lt;endpoint address="json" binding="webHttpBinding" contract="WCFService.IWCFService" behaviorConfiguration="jsonBehavior"/&gt;
          &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/&gt;
      &lt;/service&gt;
  &lt;/services&gt;
  
  &lt;behaviors&gt;
      &lt;serviceBehaviors&gt;
          &lt;behavior name="WCFService.DefaultBehavior"&gt;
              &lt;serviceMetadata httpGetEnabled="true"/&gt;
              &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
          &lt;/behavior&gt;
      &lt;/serviceBehaviors&gt;
  
      &lt;endpointBehaviors&gt;
          &lt;behavior name="jsonBehavior"&gt;
              &lt;webHttp/&gt;
          &lt;/behavior&gt;             
      &lt;/endpointBehaviors&gt;
  &lt;/behaviors&gt;
  </code></pre>
  
  <p></p>
  
  <p>And here is the Player DataContract.</p>
  
  <pre><code>[DataContract(Name = "Player")]
      public class Player
      {
          private string _name;
          private int _score;
          private Club _club;
  
          [DataMember]
          public string Name { get { return _name; } set { _name = value; } }
  
          [DataMember]
          public int Score { get { return _score; } set { _score = value; } }
  
          [DataMember]
          public Club Club { get { return _club; } set { _club = value; } }
  
      }
  </code></pre>
  
  <p>Any help greatly appreciated and if any other info is required, please let me know.</p>
  
  <p>Many thanks.</p>