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.
http://172.18.2.101:8910/Sample => will 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.