none
Unable to pass Dictionary as a DataMember in Class to WCF Restful service RRS feed

  • Question

  • Hi Expert,

    I have written one WCF Restful service but when calling the service from Client, I am not getting Dictionary values. The below is my Contract and client code. I am getting the recordId but not Dictionary value.


        [ServiceContract]
        public interface ICrmService
        {
            [
            [OperationContract]
            [WebInvoke(Method = "POST",
                RequestFormat = WebMessageFormat.Json,
                ResponseFormat = WebMessageFormat.Json,
                UriTemplate = "/ARInvoice")]
            ResponseForARInvoice PushARInvoiceToCRM(ARInvocieEntityInput request);

        }

        
     [DataContract]
        public class ARInvocieEntityInput
        {
            [DataMember]
            public Dictionary<string, object> attributeCollection = new Dictionary<string, object>();
            [DataMember]
            public string recordId { get; set; }
           

        }

    Client Code-------------

     ARInvocieEntityInput ARRequest = new ARInvocieEntityInput();
                    ARRequest.recordId = "C9E4A0C0-E1C1-E511-8122-C4346BADA558";
                    Dictionary<string, object> attribute = new Dictionary<string, object>();
                    attribute.Add("mc_gstamount", 130);
                    attribute.Add("mc_user", new Guid("01ABBE38-97D0-E411-80E8-C4346BACAEB0"));
                    attribute.Add("mc_sapreferenceno", "SAP1000");
                    ARRequest.attributeCollection = attribute;

                    var webRequest = (HttpWebRequest)WebRequest.Create("http://localhost/sap/CrmService.svc/ARInvoice");
                    webRequest.ContentType = "application/json";
                    webRequest.Method = "POST";

                    using (var sw = new StreamWriter(webRequest.GetRequestStream()))
                    {
                        string jsonARRequest = JsonConvert.SerializeObject(ARRequest);
                        sw.Write(jsonARRequest);
                        sw.Flush();
                    }
                    HttpWebResponse ARresponse = (HttpWebResponse)webRequest.GetResponse();

    Can you please help what I am doing wrong and how to fix this issue?

    Thanks in advance.

    Regards,

    Arvind


    Regards, Arvind

    Wednesday, August 2, 2017 2:34 AM

Answers

  • Hi Arvind,

    Your issue is caused by the incorrect serialize json string for ARInvocieEntityInput.

    I suggest you try below code:

                ARInvocieEntityInput ARRequest = new ARInvocieEntityInput();
                ARRequest.recordId = "C9E4A0C0-E1C1-E511-8122-C4346BADA558";
                Dictionary<string, object> attribute = new Dictionary<string, object>();
                attribute.Add("mc_gstamount", 130);
                attribute.Add("mc_user", new Guid("01ABBE38-97D0-E411-80E8-C4346BACAEB0"));
                attribute.Add("mc_sapreferenceno", "SAP1000");
                ARRequest.attributeCollection = attribute;
                var webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:8285/Service1.svc/ARInvoice");
                webRequest.ContentType = "application/json";
                webRequest.Method = "POST";
    
                using (var sw = new StreamWriter(webRequest.GetRequestStream()))
                {
                    //string jsonARRequest = JsonConvert.SerializeObject(ARRequest);
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ARInvocieEntityInput));
    
                    using (MemoryStream ms = new MemoryStream())
                    {
                        serializer.WriteObject(ms, ARRequest);
                        Console.WriteLine(Encoding.Default.GetString(ms.ToArray()));
                        sw.Write(Encoding.Default.GetString(ms.ToArray()));
                        sw.Flush();
    
                    }
                }
                HttpWebResponse ARresponse = (HttpWebResponse)webRequest.GetResponse();

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ArvindSingh Wednesday, August 2, 2017 8:19 AM
    Wednesday, August 2, 2017 7:19 AM

All replies

  • Hi Arvind,

    Your issue is caused by the incorrect serialize json string for ARInvocieEntityInput.

    I suggest you try below code:

                ARInvocieEntityInput ARRequest = new ARInvocieEntityInput();
                ARRequest.recordId = "C9E4A0C0-E1C1-E511-8122-C4346BADA558";
                Dictionary<string, object> attribute = new Dictionary<string, object>();
                attribute.Add("mc_gstamount", 130);
                attribute.Add("mc_user", new Guid("01ABBE38-97D0-E411-80E8-C4346BACAEB0"));
                attribute.Add("mc_sapreferenceno", "SAP1000");
                ARRequest.attributeCollection = attribute;
                var webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:8285/Service1.svc/ARInvoice");
                webRequest.ContentType = "application/json";
                webRequest.Method = "POST";
    
                using (var sw = new StreamWriter(webRequest.GetRequestStream()))
                {
                    //string jsonARRequest = JsonConvert.SerializeObject(ARRequest);
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ARInvocieEntityInput));
    
                    using (MemoryStream ms = new MemoryStream())
                    {
                        serializer.WriteObject(ms, ARRequest);
                        Console.WriteLine(Encoding.Default.GetString(ms.ToArray()));
                        sw.Write(Encoding.Default.GetString(ms.ToArray()));
                        sw.Flush();
    
                    }
                }
                HttpWebResponse ARresponse = (HttpWebResponse)webRequest.GetResponse();

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ArvindSingh Wednesday, August 2, 2017 8:19 AM
    Wednesday, August 2, 2017 7:19 AM
  • Hi Edward,

    Thank you so much for quick response.

    Yes, your code is working perfectly fine.

    Thanks a lot.

    Regards,

    Arvind


    Regards, Arvind

    Wednesday, August 2, 2017 8:19 AM