none
WCF REST Service POST response not in XML RRS feed

  • Question

  • The WCF REST service is implemented as follows:

    IMatch.cs:
    
    namespace XmlWcfService
    {
    
        [ServiceContract(Namespace="")]
        public interface IMatch
        {
    
            [OperationContract]
            [WebInvoke(Method = "POST",
    		RequestFormat = WebMessageFormat.Xml,
    	ResponseFormat = WebMessageFormat.Xml,
    		BodyStyle = WebMessageBodyStyle.WrappedResponse,
    	UriTemplate = "xml/match")]
    
            [XmlSerializerFormat]
            Match AddMatch(Match m);
    
    
    	[OperationContract]
            [WebInvoke(Method = "POST",
               RequestFormat = WebMessageFormat.Xml,
               ResponseFormat = WebMessageFormat.Xml,
                BodyStyle = WebMessageBodyStyle.WrappedResponse,
                UriTemplate = "xml/match")]
    
    
            bool AddListOfMatch(ListOfMatch match);   
    	
    
    	[CollectionDataContract(Namespace = "")]
    	public class ListOfMatch : List<Match>
    	{
    
    	}
    
    
        [XmlRoot(ElementName = "MyMatch",Namespace = "")]
        public class Match
        {
    
            [XmlElement]
            public string MatchId { get; set; }
    
            [XmlElement]
            public string Winner { get; set; }
    
            [XmlElement]
            public string Score { get; set; }
    
            [XmlElement]
            public string Location { get; set; }
    
    
            public Match()
            {
            }
            public Match(string id, string win, string score, string loc)
            {
                MatchId = id;
                Winner = win;
                Score = score;
                Location = loc;
            }
        }
    }
    
    
    
    Match.svc.cs:
    
    
    
    
    public bool AddListOfMatch(ListOfMatch match)
            {
    
                try
                {
                    using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["connstr"].ToString()))
                    {
                        SqlCommand command = new SqlCommand();
                        command.Connection = conn;
                        command.Connection.Open();
                        for (int count = 0; count < match.Count; count++)
                        {
                            string matchid = mat.ElementAt(count).MatchId;
                            string winner = mat.ElementAt(count).Winner;
                            string score = mat.ElementAt(count).Score;
                            string location = mat.ElementAt(count).Location;
    
    
                            string str = String.Format("INSERT into Match (MatchId, Winner ,Score ,Location) VALUES ('" + matchid + "' ,'" + winner + "' , 
    
    '" + score + "' , '" + location + "')");
    
                            command.CommandText = str;
                            command.ExecuteNonQuery();
                        }
                        command.Connection.Close();
    
                    }
                }
                catch(Exception ex)
                {
                    Console.WriteLine("Service failed to carry out request: " + ex.ToString());
                    return false;
    
                } 
                return true;
    
            }
    
    
    
    	//Method to Add a single Mat
            public Match AddMatch(Match m)
            {
                try
                {
    
                    string matchid = mat.ElementAt(count).MatchId;
                    string winner = mat.ElementAt(count).Winner;
                    string score = mat.ElementAt(count).Score;
                    string location = mat.ElementAt(count).Location;
    
                    using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["connstr"].ToString()))
                    {
    
                        string str = String.Format("INSERT into Match (MatchId, Winner ,Score ,Location) VALUES (@matchid ,@winner ,@score ,@location)");
    
                        SqlCommand command = new SqlCommand();
                        command.Connection = conn;
                        command.Connection.Open();
                        command.CommandText = str;
    
                        SqlParameter Matid = new SqlParameter("@matchid", matchid);
                        SqlParameter Winner = new SqlParameter("@winner", winner);
                        SqlParameter Score = new SqlParameter("@score", score);
                        SqlParameter Location = new SqlParameter("@location", location);
    
                        command.Parameters.AddRange(new SqlParameter[]{Matchid, Winner, Score, Location});
    
                        command.ExecuteNonQuery();
                        command.Connection.Close();
    
                    }
                }
                catch(Exception ex)
                {
                    Console.WriteLine("Service failed to carry out request: " + ex.ToString());
                }
                return match;  
            }


    The service is expected to return XML in the response to the client,as the client is to parse the XML displayed on the web page.As a client doesnt exist at the moment i use Fiddler to test the service.
    I created the AddMatch method first(using datacontract and datamembers) and tested using Fiddler but the response was not XML(in Webview):

    01James150abc

    So i used the XmlSerializer instead of the DataContractSerializer by adding the XmlSerializerFormat attribute.It seemed to work as the response was like such(in WebView on Fiddler):

    <?xml version="1.0" encoding="UTF-8"?>
    -<AddMatchResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">-
    
    <AddMatchResult><MatchId/><Winner>James</Winner><Score>150</Score><Location>abc</Location></AddMatchResult></AddMatchResponse>



    Later on I added another method which could take more than one match if say 3-4 matches were sent by the client.I used list<match> to make the method take multiple matches.Then i used CollectionDataContract and tested but always got count=0 while debugging.I then swapped xmlroot and xmlelement attribute with datacontract and datamember attribute.It did work(it added matches to the database) but it did not return response(in WebView on fiddler) in XML format either and plus it broke my AddMatch method:
    RESPONSE :
    true
    Is XmlSerializerFormat attribute required to get XML in the response?

    Could someone point out what I'm doing wrong? 

     Also is using List as a parameter to Add more than 1 match a restful approach or is there a more restful way of doing so?                                                                                                                                                                                                                                                                                        


    • Edited by JotD Monday, July 1, 2013 3:10 PM
    Friday, June 28, 2013 6:00 PM

Answers