Link to home
Start Free TrialLog in
Avatar of pzozulka
pzozulka

asked on

Creating a RESTful WCF web service. Not sure what UriTemplate to use.

I need to build a RESTful web service using WCF called GetProductionDetail. It will be passed two dates as parameters, and fetch all records from the databases that were created between those two dates. It will then output those records as a STRING in a CSV format. So for each row in the database, it will return all the columns (comma-separated), and a carriage-return at the end of each row.

I was reviewing the following article, and in it, it provides a sample UriTemplate: UriTemplate = "/ProductName/{productID}"

However, what if you want to pass two dates as paramters, and you want to use POST, not GET. What would a sample ServiceContract look like in my situation? Also any relevant links would be helpful. All the links I've come across seem to always use GET, and return XML or JSON, which is NOT what I need in this situation.

http://dotnetmentors.com/wcf/how-to-create-wcf-restful-services.aspx

*Note: I am adding this service to an existing ASP.NET (C#) application.
ASKER CERTIFIED SOLUTION
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pzozulka
pzozulka

ASKER

I tried your suggestion to use GET instead of POST, and am getting an exception: Cannot send a content-body with this verb-type.

Consumer
private void processRestService(string url, string requestXml)
        {
            try
            {
                string result = string.Empty;
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "GET";
                req.ContentType = "application /x-www-form-urlencoded";
                byte[] byteXml = Encoding.Default.GetBytes(requestXml);

                Stream stream = req.GetRequestStream();
                stream.Write(byteXml, 0, byteXml.Length);
                stream.Close();

                using (WebResponse response = req.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        result = reader.ReadToEnd();
                        req = null;
                    }
                }
                txtResponse.Text = HttpUtility.HtmlDecode(result);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Open in new window


Contract
[ServiceContract]
    public interface ISPPService
    {
        [OperationContract]
        [WebGet(UriTemplate = "GetProductionDetail")]
        string GetProductionDetail(Stream data);
    }

Open in new window