none
HTTP GET request handling with empty absolute url RRS feed

  • Question

  • I have created a self hosted service to handle HTTP GET requests.

    My WCF Get service getting called for a HTTP GET request with an absolute URL.
    but cannot calling with out an absolute URL.

    ie :
    http://172.18.2.101:8910/Sample       =>   will invoke into "GetResponse()" method
    http://172.18.2.101:8910/      =>   will not invoke into "GetResponse()" method


    [ServiceContract]
    public interface ITestService
    {
        [OperationContract]
        [WebGet(UriTemplate = "{*pathname}", BodyStyle = WebMessageBodyStyle.Bare)]
        Stream GetResponse(string pathname);
    }

    [ServiceBehavior(AddressFilterMode = AddressFilterMode.Exact)]
    public class TestServiceListner : ITestService
    {
        private ServiceHost Host { get; set; }
        private string BaseAddress { get; set; }
        private string Port { get; set; }
        private Uri ListenUri { get; set; }

        public TestServiceListner(string baseAddress, string port)
        {
            this.BaseAddress = baseAddress;
            this.Port = port;
            Host = SetupHost(); //  set properties for service host
            Host.Open();    // Open service host
        }

        private ServiceHost SetupHost()
        {
            this.ListenUri = new Uri(string.Format("http://{0}:{1}/", this.BaseAddress, Convert.ToString(this.Port)));
            var host = new ServiceHost(this, this.ListenUri);

            //  Add binding
            WebHttpBinding binding = new WebHttpBinding(WebHttpSecurityMode.None);
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
            binding.HostNameComparisonMode = HostNameComparisonMode.Exact;

            //  Add Endpoints
            host.AddServiceEndpoint(typeof(ITestService), binding, this.ListenUri).Behaviors.Add(new WebHttpBehavior());

            return host;
        }

        public Stream GetResponse(string pathname)
        {
            Stream response = null;
            // GET pathName
            switch (pathname.Trim())
            { 
                case "/":
                case "":
                        // GET default response
                    break;

                default:
                    response = ReadReasponse(pathname);
                    break;
            }
            return response;
        }

        private Stream ReadReasponse(string pathname)
        {
            // Read file and Generate response
            return null;
        }
    }

    Any guidance would be great.



    sujithsb

    Friday, March 7, 2014 12:49 PM

Answers

  • Hi,

    Yes, I also meet the above when testing your service, then I change the ServiceHost to WebServiceHost, then the service works.


    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 sujith sb Tuesday, March 11, 2014 4:52 AM
    Tuesday, March 11, 2014 2:58 AM
    Moderator

All replies

  • Hi,

    I have test your code in my side. And I found you have used the following:

    public Stream GetResponse(string pathname)
         {
             Stream response = null;
             // GET pathName
             switch (pathname.Trim())
             { 
                 case "/":
                 case "":
                         // GET default response
                     break;
    
                 default:
                     response = ReadReasponse(pathname);
                     break;
             }
             return response;
         }

    Then when you use the http://172.18.2.101:8910/, it will meet the case "", so it will do the break. And you will see the null value.

    So both of the two address are work.

    Then in order to prove it. I just do a simple test:

    public string GetResponse(string pathname)
         {
             stream response = null;
             // GET pathName
             switch (pathname.Trim())
             { 
                 case "/":
                 case "":
                 response="dafault"
                 break;
    
                 default:
                     response =pathname;
                     break;
             }
             return response;
         }

    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, March 10, 2014 7:08 AM
    Moderator
  • Hi,

    I tried it agian. but my problem is that the execution cannot hit into the function "GetResponse".

    Browser shows another message as below.



    sujithsb

    Monday, March 10, 2014 9:18 AM
  • Hi,

    Yes, I also meet the above when testing your service, then I change the ServiceHost to WebServiceHost, then the service works.


    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 sujith sb Tuesday, March 11, 2014 4:52 AM
    Tuesday, March 11, 2014 2:58 AM
    Moderator
  • Thanks Amy Peng. It works for me. :)

    sujithsb

    Tuesday, March 11, 2014 5:20 AM