none
Basics for implementing a rest service RRS feed

  • Question

  • In my plight to generate code that extends a windows service to receive input through an http post request, I have started simpler and am hoping to gain the insight I need. I took the example from https://msdn.microsoft.com/en-us/library/vstudio/bb412178(v=vs.100).aspx as it does not use an App.Config. The GET method works but I can not get the POST method to accept parameters. Adding a parameter with fiddler causes a 400 error? Anyone have any suggestions?

    Thanks!

    using System;
    using System.Collections.Generic;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    using System.ServiceModel.Web;
    using System.Text;
    
    namespace Microsoft.ServiceModel.Samples.BasicWebProgramming
    {
        [ServiceContract]
        public interface IService
        {
            [OperationContract]
            [WebGet]
            string EchoWithGet(string s);
    
            [OperationContract]
            [WebInvoke]
            string EchoWithPost(string s);
        }
    
        public class Service : IService
        {
            public string EchoWithGet(string s)
            {
                return string.Format("You said '{0}'", s);
            }
    
            public string EchoWithPost(string s)
            {
                return string.Format("You said '{0}'", s);
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost:8000/"));
    
                try
                {
                    ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "");
                    host.Open();
    
                    Console.WriteLine("Press <ENTER> to terminate");
                    Console.ReadLine();
    
                    host.Close();
                }
                catch (CommunicationException cex)
                {
                    Console.WriteLine("An exception occurred: {0}", cex.Message);
                    host.Abort();
                }
            }
        }
    }


    • Edited by Ritmo2k Thursday, February 26, 2015 9:43 PM grammar
    Thursday, February 26, 2015 9:42 PM

Answers

  • Hi Ritmo2k,

    >>however I need to send several parameters and would like to use the request body.

    What do you mean about the above message? Do you mean that you can use the RequestResponse method with only one parameter, but it does not work when using serveral parameters in the RequestResponse method. If so, I will recommand you define a DataContract, then you can use the serveral parameters as the DataMemeber of the DataContract. After that you can use a DataContract object as the only one parameter.

    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.

    • Marked as answer by Ritmo2k Thursday, March 5, 2015 2:30 AM
    Thursday, March 5, 2015 2:07 AM
    Moderator

All replies

  • Hi Ritmo2k,

    If you want to use the POST method, please do not forget to add the WebHttpBehavior in the code behind as following:

    ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "");
    ep.Behaviors.Add(new WebHttpBehavior());

    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.

    Friday, February 27, 2015 5:38 AM
    Moderator
  • Hi Amy,
    Sadly that did not help. If I decorate the method as follows:
        [ServiceContract]
        public interface IService
        {
            ...
    
            [OperationContract]
            [WebInvoke(UriTemplate = "Response?s={s}")]
            string RequestResponse(string s);
        }
    I can then POST with the parameter in the url, however I need to send several parameters and would like to use the request body. Any idea what I am doing wrong?

    Thanks!
    Friday, February 27, 2015 5:39 PM
  • Hi Ritmo2k,

    >>however I need to send several parameters and would like to use the request body.

    What do you mean about the above message? Do you mean that you can use the RequestResponse method with only one parameter, but it does not work when using serveral parameters in the RequestResponse method. If so, I will recommand you define a DataContract, then you can use the serveral parameters as the DataMemeber of the DataContract. After that you can use a DataContract object as the only one parameter.

    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.

    • Marked as answer by Ritmo2k Thursday, March 5, 2015 2:30 AM
    Thursday, March 5, 2015 2:07 AM
    Moderator
  • Hi Amy,
    That was exactly how I resolved my issue.

    Thanks.
    Thursday, March 5, 2015 2:30 AM