使用空绝对URL处理HTTP GET请求

时间:2014-03-07 12:54:57

标签: http-get

我创建了一个自托管服务来处理HTTP GET请求。 我的WCF Get服务被调用带有绝对URL的HTTP GET请求。 但不能使用绝对URL进行呼叫。

例如: -

http://xxx.xx.x.xxx:8910/Sample =>将调用“GetResponse()”方法

http://xxx.xx.x.xxx:8910/ =>不会调用“GetResponse()”方法

[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;
    }
}

任何指导都会很棒。

0 个答案:

没有答案