Gainster
Gainster

Reputation: 5631

Getting a null object in wcf service when I am adding a reponse format and body style

string baseAddress = "http://" + Environment.MachineName + ":8000/RestServiceImpl";
        ServiceHost host = new ServiceHost(typeof(RestServiceImpl), new Uri(baseAddress));
        //host.AddServiceEndpoint(typeof(IRestServiceImpl), new WebHttpBinding(), "").Behaviors.Add(new GainSoft.TaskManager.Service.RestServiceImpl.MyWebHttpBehavior());
        host.AddServiceEndpoint(typeof(IRestServiceImpl), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
        ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
        sdb.HttpHelpPageEnabled = false;
        host.Open();
        Console.WriteLine("Host opened");

        WebClient client = new WebClient();
     // Console.WriteLine(client.DownloadString(baseAddress + "/InsertData?param1=John,Doe"));


        try
        {
            client = new WebClient();
            client.Headers[HttpRequestHeader.ContentType] = "application/json";
            Console.WriteLine(client.UploadString(baseAddress + "/InsertData", "{\"FirstName\":\"John\",\"LastName\":\"Doe\"}"));

Service Code If i remove the highlight code then it works fine. I don't know what i am missing here ? enter image description here enter image description here

Upvotes: 1

Views: 276

Answers (1)

aKzenT
aKzenT

Reputation: 7895

You have to include the name of the parameter in the JSON you upload. So:

{"param1": {"FirstName":"John","LastName":"Doe"}}

It doesn't explain though why it works without the highlighting code...

Does this solve your problem?

Upvotes: 1

Related Questions