none
WCF Rest Service for list objects using web invoke for XML request format RRS feed

  • Question

  • Hi,

    Quick question...

    Is it possible for a WCF rest service to accept list of objects using WebInvoke.

    For example, if i want a 3 persons in a single api call, can we expose our service to accept list of person object?

    Or should i make calls?

    If anyone can you give a quick example for xml request format, that would be greatful..........

    Friday, June 6, 2014 5:39 PM

All replies

  • Hi,

    Yes, it is possible in WCF Rest Service to accept list of objects as parameter using WebInvoke, then I have created the following example, please try to check it:
    In the IService1:

    namespace WcfServiceTestDome
    {
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            [WebInvoke(Method = "POST", UriTemplate = "getpersonlist",
    RequestFormat=WebMessageFormat.Xml,ResponseFormat=WebMessageFormat.Xml)] string getpersonlist(List<person> personlist); } [DataContract] public class person { [DataMember] public string id { get; set; } [DataMember] public string name { get; set; } } }
    In the Service1:
    namespace WcfServiceTestDome
    {
        public class Service1 : IService1
        {      
           public string getpersonlist(List<person> personlist)
            {
                return personlist.Count.ToString();
            }
        }
    }

    The web.config file:
    <system.serviceModel>
        <services>
          <service name="WcfServiceTestDome.Service1">
            <endpoint address="" contract="WcfServiceTestDome.IService1" binding="webHttpBinding" 
    behaviorConfiguration="web"></endpoint> </service> </services> <behaviors> <endpointBehaviors> <behavior name="web"> <webHttp helpEnabled="true"/> </behavior> </endpointBehaviors> ...................................

    After creating the above service, we need to write the xml file for calling the service.

    The Persons.xml:

    <ArrayOfperson xmlns="http://schemas.datacontract.org/2004/07/WcfServiceTestDome">
      <person>
        <id>111</id>
        <name>Amy</name>
      </person>
      <person>
        <id>222</id>
        <name>John</name>
      </person>
    </ArrayOfperson>

    In the client side:

    public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                HttpWebRequest req = null;
                HttpWebResponse res = null;
    
               string url = "http://localhost:11775/Service1.svc/getpersonlist";
    
                req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                req.ContentType = "application/xml; charset=utf-8";
                req.Timeout = 30000;
                req.Headers.Add("SOAPAction", url);
    
                var xmlDoc = new XmlDocument { XmlResolver = null };
                xmlDoc.Load(Server.MapPath("Persons.xml"));
                string sXml = xmlDoc.InnerXml;
                req.ContentLength = sXml.Length;
                var sw = new StreamWriter(req.GetRequestStream());
                sw.Write(sXml);
                sw.Close();
    
                res = (HttpWebResponse)req.GetResponse();
                Stream responseStream = res.GetResponseStream();
                var streamReader = new StreamReader(responseStream);
    
                //Read the response into an xml document
                var soapResonseXmlDocument = new XmlDocument();
                soapResonseXmlDocument.LoadXml(streamReader.ReadToEnd());
    
                //return only the xml representing the response details (inner request)
                TextBox1.Text = soapResonseXmlDocument.InnerXml;//Here Retun Guid as xml
               
            }
        }


    Then the result:

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, June 9, 2014 3:30 AM
    Moderator
  • Hi Amy,

    thanks for your time and providing the sample example...

    I have implemented everything in the same way. I have used WCF Service Library instead of WCF Service application template.

    The only change i have was i have namespace in my data contract, i added that in my xml file also.

    But i get "The remote server returned an error: (400) Bad Request."...


    i am also using fiddler to test the service....svc works for all other cases...i am able to retrieve multiple person object, single person, add single person...

    but fails only when i try to add list object...

    When i pull list of persons this is the format of result structure that i get and i am passing the same below xml value for adding them,,

    <ArrayOfPerson xmlns="urn:Rest.API" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Person><PersonId>1</PersonId><Name>John Smith</Name><Location>Delaware</Location></Person><Person><PersonId>2</PersonId><Name>Thomas Orie</Name><Location>Philly</Location></Person><Person><PersonId>3</PersonId><Name>Byrd Mary</Name><Location>Erie</Location></Person></ArrayOfPerson>

    Any thoughts or suggestions??


    Monday, June 9, 2014 3:35 PM
  • Hi,

    Please try to mark sure that your namespace is correct. In my first reply I have used this <webHttp helpEnabled="true"/> which will enable the help page. When we browser the service address in the IE, we can add the "/help" below the service address as following:



    Then please click the "POST", it will give us the namespace:

    For more information, please try to refer to:
    #How to enable help page in WCF Rest Service:
    http://stack247.wordpress.com/2011/04/18/enable-wcf-web-service-help-page-in-web-config/ .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Tuesday, June 10, 2014 1:49 AM
    Moderator
  • i made sure the namespace was right...

    it was working the namespace for all other scenarios.... even it works... for adding a single person..

    i also enabled the help in config file....

    anyother suggestions?? 

    I have separate projects for Interface, Implemtation (wcf host project) and data contract .

    does this make any change??

    i even used the sample example that shows on help page, but still see bad request
    Tuesday, June 10, 2014 2:18 AM
  • Hi,

    Could you please try to upload your project in the onedrive? Then I want to test it in my side.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, June 10, 2014 3:26 AM
    Moderator
  • Hi Amy,

    I have uploaded the project along with xml request at the below link

    https://onedrive.live.com/redir?resid=70A4E59531AA38C7!111&authkey=!APg5N7luVd4vTP0&ithint=folder%2c.zip

    Please let me know about your findings or solution.


    Tuesday, June 10, 2014 2:11 PM
  • Hi Amy,

    I have uploaded the project along with xml request at the below link

    https://onedrive.live.com/redir?resid=70A4E59531AA38C7!111&authkey=!APg5N7luVd4vTP0&ithint=folder%2c.zip

    Please let me know about your findings or solution.


    Hi Amy,

    Did you get chance to look at my code?

    Wednesday, June 11, 2014 12:49 PM
  • Your project can not run, it seems that it has no host application. Also what is the content of the

    C:\Temp\WcfRest\PersonsNoResp.xml. It seems that we can not write file in the client side to a server xml file.

    Monday, June 16, 2014 9:25 AM
  • Your project can not run, it seems that it has no host application. Also what is the content of the

    C:\Temp\WcfRest\PersonsNoResp.xml. It seems that we can not write file in the client side to a server xml file.

    Hi...

    I am able to run the project and test the other methods successful. I used WCF service Library and that has capabilities to host the svc itself....Its just that we wont see the browser running by default.

    I am capturing the response received by the service in an xml file to make sure the service has received the request.

    I am able to do this for other operations that i have exposed....


    Monday, June 16, 2014 6:47 PM