0

I am new to WCF, but after going through articles and several questions on StackOverflow, have managed to bringup a self hosted RESTful service. The issue I am finally facing is that the parameter value returned to the operation contract is null. I realize there are several previous questions on this, but I think I have taken care of all the settings raised there and is not solving the problem. I am able to get a json response, however its the json body that is sent to a "POST" call which is not getting deserialized into the parameter object.

Heres the code:-

[ServiceContract]
public interface IExchServer
{
    [OperationContract]
    [WebInvoke(Method = "GET",  
        ResponseFormat = WebMessageFormat.Json, 
        UriTemplate = "/init")]
    DomainInfo init();

    [OperationContract]
    [WebInvoke(Method = "POST", 
        RequestFormat = WebMessageFormat.Json, 
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "/close")]
    string close(DomainInfo connection); }

And here is the class

[DataContract]
public class DomainInfo
{

    [DataMember]
    public string Domain { get; set; }

    [DataMember]
    public string UserName { get; set; }

    [DataMember]
    public string password {get;set;}}

And in the rest client I am using (PostMan) I have set the header to application/json. I have tried with the following payload:

{"DomainInfo":{
"Domain": "domain.com",
"UserName": "admin@domain.com",
"password": "pass@123"}}

as well as

{"Domain": "domain.com",
"UserName": "admin@domain.com",
"password": "pass@123"}

In the first one I get the below WCF trace

Incoming HTTP request to URI 'http://localhost:9001/ExchServer/close' matched operation 'close'
Opening System.ServiceModel.InstanceContext/17818390
Opened System.ServiceModel.InstanceContext/17818390
A message was read
An unrecognized element was encountered in the XML during deserialization which was ignored.

while in the second one I get

Incoming HTTP request to URI 'http://localhost:9001/ExchServer/close' matched operation 'close'.
Opening System.ServiceModel.InstanceContext/5923895
Opening System.ServiceModel.InstanceContext/5923895
A message was read
An unrecognized element was encountered in the XML during deserialization which was ignored
An unrecognized element was encountered in the XML during deserialization which was ignored
An unrecognized element was encountered in the XML during deserialization which was ignored

There are no other errors reported. On Starting VS in debug mode, I see the paremeter being null. Please do guide me on this as it seems to be a very basic usage. The init() contract works as expected and is returning me a json string. Thanks in advance.

djs
  • 53
  • 5

1 Answers1

0

Well this answer solved it.

Sending JSON to WCF Rest Service - object is always null The problem is that the parameter name must match the json object name. So the correct json is

{"connection":{
"Domain": "domain.com",
"UserName": "admin@domain.com",
"password": "pass@123"}}
Community
  • 1
  • 1
djs
  • 53
  • 5