Click here to Skip to main content
15,885,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am trying to pass a complex object to WCF service through a Client Application.
When i debug the code, I see object is coming as null.

I tried the following code.

Interface method

C#
[OperationContract]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            UriTemplate = "Testmethod/JSON/")]
        bool TestmethodJson(Complain comp);


Service Implementation

C#
public bool TestmethodJson(Complain comp)
        {
            if (comp != null)
            {
             //code goes here  
                return true;
            }
            else
            {
              //code goes here
                return false;
            } 



Client application code goes as follows

C#
static void Main(string[] args)
        {
            Complain comp = new Complain()
            {
                CompainType = "type1",
                CompainBody = "Body1"
            };

             //JavaScriptSerializer serializer = new JavaScriptSerializer();

            string output = JsonConvert.SerializeObject(comp);

            //string output = serializer.Serialize(complain);

             string strUri = "http://localhost:35798/NavGAT.svc/Testmethod/JSON/";
            Uri uri = new Uri(strUri);
            WebRequest request = WebRequest.Create(uri);
            request.Method = "POST";
            request.ContentLength = output.Length;
            request.ContentType = "application/json; charset=utf-8";

          //  JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            string serOut = JsonConvert.SerializeObject(comp);
          //  string serOut = jsonSerializer.Serialize(complain);

            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(serOut);
            }

            WebResponse responce = request.GetResponse();
            Stream reader = responce.GetResponseStream();

            StreamReader sReader = new StreamReader(reader);
            string outResult = sReader.ReadToEnd();
            sReader.Close();
        }


I am able to call a service from client but object comes as null.Please let me know the problem in passing complex object service.


Thanks in Advance.. :)
Posted
Updated 11-Jul-20 10:43am
v2

Please ensure you have Complain entity class declaration with DataContract attribute over it and CompainType and CompainBody must have DataMemeber attribute over it.

C#
using System.Runtime.Serialization;

 [DataContract]
    public class Complain 
    {
        #region Private Members
        private string _CompainBody ;
        private string _CompainType ;
        #endregion

        #region Public Members

        [DataMember]
        public int CompainBody 
        {
            get { return _CompainBody ; }
            set { _CompainBody = value; }
            }
        [DataMember]
        public string CompainType 
        {
            get { return _CompainType ; }
            set { _CompainType = value; }
        }

        }
}
 
Share this answer
 
v2
Comments
[no name] 23-Mar-15 0:19am    
hi,
i have declared my class with datacontract and properties with datamember attributes only.
Rajat_RJT 23-Mar-15 2:30am    
what data you are passing with which url?
[no name] 23-Mar-15 2:41am    
I have copied Complain data in above code.Please look at "Client application code goes as follows" Code section.
You have BodyStyle as WrappedRequest,so you need to do-
Either remove
BodyStyle = WebMessageBodyStyle.WrappedRequest
or pass your object like
{
    "comp": {
        "ComplainType": "1",
        "ComplainBody": "message"
    }
}

That's how you can see values in in object.
 
Share this answer
 
v3
Comments
[no name] 27-Mar-15 6:39am    
still the data is coming as null..
Rajat_RJT 30-Mar-15 3:13am    
@syedkhaleel, i have updated my answer, this will surely help you.
[no name] 30-Mar-15 4:04am    
thanx for taking time to solve the issue.
Rajat_RJT 30-Mar-15 5:35am    
Thanks @syedkhleel,please mark this solution as answer if it helped you.
You have BodyStyle as WrappedRequest,so you need to do-
Either remove
Hide   Copy Code
BodyStyle = WebMessageBodyStyle.WrappedRequest
or pass your object like
Hide   Copy Code
{
    "comp": {
        "ComplainType": "1",
        "ComplainBody": "message"
    }
}


This solution worked for me
 
Share this answer
 
Comments
CHill60 13-Jul-20 10:50am    
Then post a comment against the solution using the "Have a Question or Comment?" link. Do not just copy the solution and repost it as your own.
If members deem this to be plagiarism then your account could be suspended. I advise you to delete this solution now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900