WCF Rest Post Service issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • syam s
    New Member
    • Oct 2010
    • 4

    WCF Rest Post Service issue

    WCF Rest Post Service issue

    Hi all,
    I am new to WCF Rest , I had created a WCF service with ServiceContract as follows

    Code:
     
    
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "/SampleService", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    
    string SampleService(Person personObj);
    
    
    
    Datacontract is like as  follows 
    
    
    public string SampleService(Person personObj)
            {
    
                return "Sucess";
            }
    And my Person Class is defined as

    Code:
     
    
    using System.Runtime.Serialization;
    
    namespace CMPCO.BusinessLayer.BusinessObject
    {
        [DataContract(Namespace = "http://www.test.com")]   
        public class Person
        {
            [DataMember ( Name="MiddleName")]
            public string MiddleName { get; set; }
    
            [DataMember(Name = "FirstName")]
            public string FirstName{ get ; set;}
    
             [DataMember(Name = "LastName")]
            public string LastName{ get; set;}
    
               [DataMember(Name = "Age")]
            public string Age { get; set; }
    
        }
    }
    I used Google chrome’s Simple rest client to make rest call
    And I pass following xml as data
    Code:
     
    
    <?xml version="1.0" encoding="utf-8" ?>
    <Person xmlns="http://www.test.com">
    
    <MiddleName>mName</MiddleName>
    <FirstName>fName</FirstName>
    <LastName>lname</LastName>
    <Age>23</Age>
    </Person>
    I have enabled debugging in WCF rest server. The rest call successfully entering but only one value in the requesting object is get initialised that is person object in data contract has only [MiddleName] all other values are not initialised and is set to null
    Then I changed the person class as follows

    Code:
     
    namespace CMPCO.BusinessLayer.BusinessObject
    {
        [DataContract(Namespace = "http://www.test.com")]   
        public class Person
        {
            [DataMember(Name = "item1")]
            public string MiddleName { get; set; }
    
            [DataMember(Name = "item2")]
            public string FirstName{ get ; set;}
    
             [DataMember(Name = "item3")]
            public string LastName{ get; set;}
    
               [DataMember(Name = "item4")]
            public string Age { get; set; }
    
        }
    }
    And current request xml data is as follows

    Code:
     
    
    <?xml version="1.0" encoding="utf-8" ?>
    <Person xmlns="http://www.test.com">
    <item1>mName</item1>
    <item2>fName</item2>
    <item3>lname</item3>
    <item4>26</item4>
    </Person>
    Now all the person object is get initialised with data’s in the xml. I am confused what had gone wrong with me...

    Please reply, thanks in advance
    Syam.S
  • syam s
    New Member
    • Oct 2010
    • 4

    #2
    I solved after a long search

    Answer is as follows

    Data contract types require that the parameters are sent in a specific order. If you don't explicitly specify the order (via the "Order" property in the [DataMember] attribute), the assumed order is alphabetical. In the example below (from your code), just by changing the order of the XML sent to the service, the Person object is properly populated.

    If you want to support unordered requests, you should change to the XmlSerializer (add a [XmlSerializerFo rmat] attribute to your service / operation, and decorate the Person class with the attributes from the System.Xml.Seri alization namespace instead of DataContract/DataMember)

    Code:
     
      public class Post_76644d43_cbaf_4817_87b1_cd014199035c
      {
        [ServiceContract]
        public interface ITest
        {
          [OperationContract]
          [WebInvoke(Method = "POST", UriTemplate = "/SampleService", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
          string SampleService(Person personObj);
        }
        public class Service : ITest
        {
          public string SampleService(Person personObj)
          {
            return string.Format("First={0},Middle={1},Last={2},Age={3}", personObj.FirstName, personObj.MiddleName, personObj.LastName, personObj.Age);
          }
        }
        [DataContract(Name = "Person", Namespace = "http://www.test.com")]
        public class Person
        {
          [DataMember(Name = "MiddleName")]
          public string MiddleName { get; set; }
          [DataMember(Name = "FirstName")]
          public string FirstName { get; set; }
          [DataMember(Name = "LastName")]
          public string LastName { get; set; }
          [DataMember(Name = "Age")]
          public string Age { get; set; }
        }
        public static void Test()
        {
          string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
          ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
          host.AddServiceEndpoint(typeof(ITest), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
          host.Open();
          Console.WriteLine("Host opened");
    
          string outOfOrderXml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
    <Person xmlns=""http://www.test.com"">
     <MiddleName>mName</MiddleName>
     <FirstName>fName</FirstName>
     <LastName>lname</LastName>
     <Age>23</Age>
    </Person>";
          string orderedXml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
    <Person xmlns=""http://www.test.com"">
     <Age>23</Age>
     <FirstName>fName</FirstName>
     <LastName>lname</LastName>
     <MiddleName>mName</MiddleName>
    </Person>";
    
          WebChannelFactory<ITest> factory = new WebChannelFactory<ITest>(new Uri(baseAddress));
          Console.WriteLine(factory.CreateChannel().SampleService(new Person { Age = "23", FirstName = "f", MiddleName = "m", LastName = "l" }));
    
          WebClient c = new WebClient();
          c.Headers[HttpRequestHeader.ContentType] = "text/xml";
          Console.WriteLine(c.UploadString(baseAddress + "/SampleService", outOfOrderXml));
          Console.WriteLine();
    
          c = new WebClient();
          c.Headers[HttpRequestHeader.ContentType] = "text/xml";
          Console.WriteLine(c.UploadString(baseAddress + "/SampleService", orderedXml));
    
          Console.Write("Press ENTER to close the host");
          Console.ReadLine();
          host.Close();
        }
      }

    Comment

    Working...