Answered WCF REST POST Method to pass List of strings

  • 2011年1月11日 12:52
     
     

    [WebInvoke(RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, Method = "POST", UriTemplate = "searchCustomer")]

    List<Customer> GetCustomer(SearchCustomer obj)

     

    public class SearchCustomer

    {

    [DataMember]

    List<string> customeNamer {get;set;};

     

    [DataMember]

    string country {get;set;};

     

    }

     

    When I post the xml and see in debug mode  <Country>test</country> is passing to the service. But <CustomerName><string>Ronald</string></CustomerName>  is not getting passed. Please let me know how to pass the list of strings to the REST method

     

すべての返信

  • 2011年1月12日 1:57
     
     

    Ronald,

    Your contract doesn't specify that you are passing a list of strings to the service. Rather it specifies that you are passing in a SearchCustomer instance. I thought that maybe you meant you want the service operation to return a list of strings, but again your contract doesn't specify that, it specifies that it returns a list of Customer instances. Can you please clarify what you are trying to do?

    Thanks,

    Michael Green
    WCF Documentation Team

  • 2011年1月12日 6:28
     
     

    Hi Michael,

     I have to retrieve List of customer based on the search criteria. The Search criteria will contain a list of customer name and country name.  The country name value which is posted as XML is passed to the service. But the List of customer name in xml is not getting passed to the service . I am using the below mentioned factory

    Factory

     

     

    ="System.ServiceModel.Activation.WebServiceHostFactory"

     Thanks

    Ronald

  • 2011年1月12日 7:16
     
     回答済み

    Hi Michael

    The issues is fixed using the following. I have to specify the collectionDataContract to pass the array of strings. And also I have to specify the order in the Data Memeber so that the ActivationFactory parses the xml properly.

    [

     

    CollectionDataContract(Name = "StringArray", Namespace = ContractConstants.CUSTOMERNAMESPACE)]

     

     

    public class CustomerNameList : List<string>

    {

     

     

    public CustomerNameList() { }

     

     

    public CustomerNameList(IEnumerable<string> strings) : base(strings) { }

    }

    [DataContract Namespace=ContractConstants.CUSTOMERNAMESPACE)]

    public class SearchCustomer

    {

    [DataMember(order=0)]

    CustomerNameList  customeName {get;set;};

     

    [DataMember(order=1)]

    string country {get;set;};

     

    }

     

    • 回答としてマーク Ronald Vimal 2011年1月12日 7:16
    •  
  • 2012年3月20日 5:31
     
     
    im also facing the same issue ..

    i tried defining collectiondatacontract but it didnt solve.. this is my datamember

           [DataMember(Name = "CountryList", IsRequired = false, Order = 9)]
            public List<string> CountryList{ get; set; }

    can u help me in defining this,when trying to pass im getting as count = 0
    • 編集済み Kumar's 2012年3月31日 6:34
    •