Get the object is null using JSON in WCF Service

I have a WCF service and I created a website to test my service. However the object is always null in the method. I used the if statement to check the object and it return false for the null object. Would someone solve me how to fixed it.

There is my service:

 [OperationContract]
    [WebInvoke(Method = "POST",
       ResponseFormat = WebMessageFormat.Xml,
        RequestFormat = WebMessageFormat.Json,
       BodyStyle = WebMessageBodyStyle.WrappedRequest,          
       UriTemplate = "BookInfo/")]

    BookingResult Booking(BookInfo bookInfo);


 public BookingResult Booking(BookInfo bookInfo)
    {
        BookingResult result = new BookingResult();            
        if (bookInfo == null)
        {
            result.isSucceed = false;
        }
        else
        {
            result.isSucceed = true;
        }

        return result;
    }

There is the method in my website to call the service.

private string callService(BookInfo input)
    {

        string serviceUrl = "http://localhost:1599026/Booking.svc/BookInfo/";            
        var stringPayload = JsonConvert.SerializeObject(input);   

        WebClient client = new WebClient();
        client.Headers["Content-type"] = "application/json";                    
        client.Encoding = Encoding.UTF8;
        string rtn = client.UploadString(serviceUrl,"POST", stringPayload);
        return rtn;

    }

I have made a demo, wish it is useful to you.

Server-side.

 class Program
{
    static void Main(string[] args)
    {
        Uri uri = new Uri("http://localhost:2000");
        WebHttpBinding binding = new WebHttpBinding();
        using (ServiceHost sh=new ServiceHost(typeof(MyService),uri))
        {
            ServiceEndpoint se = sh.AddServiceEndpoint(typeof(IService), binding, "");
            se.EndpointBehaviors.Add(new WebHttpBehavior());

            Console.WriteLine("service is ready....");
            sh.Open();

            Console.ReadLine();
            sh.Close();
        }
    }
}
[ServiceContract(ConfigurationName ="isv")]
public interface IService
{
    [OperationContract]
    [WebInvoke(Method = "POST",
  ResponseFormat = WebMessageFormat.Xml,
   RequestFormat = WebMessageFormat.Json,
  BodyStyle = WebMessageBodyStyle.WrappedRequest,
        UriTemplate ="BookInfo/")]
    BookingResult Booking(BookInfo bookInfo);
}
[ServiceBehavior(ConfigurationName = "sv")]
public class MyService : IService
{

    public BookingResult Booking(BookInfo bookInfo)
    {
        BookingResult result = new BookingResult();
        if (bookInfo==null)
        {
            result.isSucceed = false;
        }
        else
        {
            result.isSucceed = true;
        }
        return result;
    }
}
[DataContract]
public class BookInfo
{
    [DataMember]
    public string Name { get; set; }
}
[DataContract]
public class BookingResult
{
    [DataMember]
    public bool isSucceed { get; set; }
}

Client-side.

class Program
{
    static void Main(string[] args)
    {
        string uri = "http://localhost:2000/BookInfo";
        WebClient client = new WebClient();
        client.Headers["Content-type"] = "application/json";
        client.Encoding = Encoding.UTF8;
        BookInfo input = new BookInfo()
        {
            Name = "Apple"
        };
        string str2 = "{\"bookInfo\":" + JsonConvert.SerializeObject(input) + "}";
        string result = client.UploadString(uri, "POST", str2);
        Console.WriteLine(result);
    }
}
[DataContract]
public class BookInfo
{
    [DataMember]
    public string Name { get; set; }
}

Result.

Result

If I call it in Postman.

Postman

Depending on the combination of BodyStyle, ResquestFormat and ResponseFormat. We will have the different formats.

1:

ResponseFormat = WebMessageFormat.Json,RequestFormat =
    WebMessageFormat.Json, BodyStyle =
    WebMessageBodyStyle.WrappedRequest,

Request:

{“bookInfo”:{“name”:”value”}}

Response:

{“BookingResult”:{“isSucceed”:value}}

2:

ResponseFormat = WebMessageFormat.Json,RequestFormat =
    WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,

Request:

{“name”:”value”}

Response:

{“isSucceed”:value}

3:

ResponseFormat = WebMessageFormat.Xml,RequestFormat = WebMessageFormat.Xml,BodyStyle = WebMessageBodyStyle.Bare,

Request:

   <BookInfo xmlns="http://schemas.datacontract.org/2004/07/Server6" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <Name>true</Name>
    </BookInfo>

Response.

<BookingResult xmlns="http://schemas.datacontract.org/2004/07/Server6" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <isSucceed>true</isSucceed>
</BookingResult>

4:

 ResponseFormat = WebMessageFormat.Xml,RequestFormat WebMessageFormat.Xml,BodyStyle= WebMessageBodyStyle.Wrapped

Request:

<Booking xmlns="http://tempuri.org/">
    <bookInfo>
    <Name>abcd</Name>
    </bookInfo>
</Booking>

Response:

<BookingResponse xmlns="http://tempuri.org/">
<BookingResult xmlns:a="http://schemas.datacontract.org/2004/07/Server6" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <a:isSucceed>true</a:isSucceed>
</BookingResult>

We could also use the MessageParameter attribute to assign the parameter’s name manually.

[return: MessageParameter(Name ="result")]
BookingResult Booking([MessageParameter(Name ="book")] BookInfo bookInfo);

Request:

{“book”:{“Name”:”value”}}

Response:

{“result”:{“isSucceed”:value}}

Feel free to contact me If you have any questions.