404 error when browsing to endpoint of a self hosted WCF service
I'm having an issue when i connect to an endpoint using WCF Test Client i get the message
Cannot obtain Metadata from http://localhost:8080/evals/basic
I'm adding the endpoints and starting the host in code using
private ServiceHost _serviceHost;
public ServiceHost ServiceHost
{
get { return _serviceHost?? (_serviceHost= new ServiceHost(typeof(EvalService))); }
}
public void Start()
{
ServiceHost.AddServiceEndpoint(typeof(IEvalService),
new BasicHttpBinding(),
"http://localhost:8080/evals/basic");
ServiceHost.AddServiceEndpoint(typeof(IEvalService),
new WSHttpBinding(),
"http://localhost:8080/evals/ws");
ServiceHost.AddServiceEndpoint(typeof(IEvalService),
new NetTcpBinding(),
"net.tcp://localhost:1002/evals");
ServiceHost.Open();
}
and when i check the status using this method
priv开发者_运维问答ate void ShowServiceInfo()
{
var message = String.Format("{0} is {1} with these endpoints:\n", _host.ServiceHost.Description.ServiceType, _host.ServiceHost.State);
foreach (var serviceEndpoint in _host.ServiceHost.Description.Endpoints)
{
message += "\n" + serviceEndpoint.Address;
}
MessageBox.Show(message);
}
then i get this message which looks like everything is running ok
HostService.EvalService is Opened with these endpoints:
http://localhost:8080/evals/basic http://localhost:8080/evals/ws net.tcp://localhost:1002/evals
Anyone have any idea why browsing to the endpoint will not work?
I see two issues - but can't verify those (you didn't provide the necessary config file):
1) You don't seem to have any MEX endpoints - an endpoint to exchange metadata about the service on. Those need to be added explicitly to your service host - you can have MEX endpoints for HTTP or Net.Tcp protocols
2) I cannot verify whether or not you've enabled service metadata as a service behavior on your service - typically this is done from config (which you didn't provide), or you can add this behavior in code, too - but it must be added explicitly.
ServiceMetadataBehavior metadataBehavior;
metadataBehavior = ServiceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
if(metadataBehavior == null)
{
metadataBehavior = new ServiceMetadataBehavior();
metadataBehavior.HttpGetEnabled = true;
ServiceHost.Description.Behaviors.Add(metadataBehavior);
}
Without any MEX infrastructure in place, no client can query your service for its methods and parameters - that's why the WCF Test Client won't work. Add the service metadata behavior, and at least one MEX endpoint, and you should be fine.
精彩评论