I know how to add a WebReference in Visual Studio, easy enough.
I also know how to create a normal ASP.NET Web Service project, but that's not what I am doing here.
So, the WebService I have running looks like this:
try
{
if (host != null)
{
host.Close();
host = null;
}
baseAddress = new Uri("http://example.com:8080");
host = new WebServiceHost(typeof(MyProxy), baseAddress);
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
host.Description.Behaviors.Add(smb);
host.Opened += new EventHandler(host_Opened);
host.Closed += new EventHandler(host_Closed);
System.ServiceModel.Description.ServiceEndpoint se = host.AddServiceEndpoint(typeof(IMyProxy), new WebHttpBinding(), baseAddress);
se.Behaviors.Add(new System.ServiceModel.Description.WebHttpBehavior());
host.Open();
}
catch (Exception e)
{
}
// .... stuff ....
[ServiceContract]
public interface IMyProxy
{
[OperationContract]
[WebGet(UriTemplate = "GetArea?searchString={searchString}")]
GetAreaResult GetArea(string searchString);
}
// more stuff of course follows here
The problem is that when I try to add a WebReference to the above service in Visual Studio, I get an error.
"Add Service Reference" --> "Add Web Reference" and in the URL I write my URL, http://example.com:8080
Then I get "Service ... Endpoint not found." and the error message in the Add Web Reference box:
There was an error downloading 'http://example.com:8080/'. The request failed with HTTP status 404: Not Found. There was an error downloading 'http://example.com:8080/$metadata'. The request failed with HTTP status 404: Not Found.
If I open up a web browser and go directly to http://example.com:8080/GetArea the service is called/executed as expected.
So to rephrase the problem shorter: The WSDL/description isn't there, so I cannot add a Web Service reference.
The problem here is that the
WebServiceHostwill remove the functionality that you are trying to achieve when adding theServiceMetadataBehavior. Looking in dotPeek (reflector) at theWebServiceHost, inside theOnOpeningmethod there is:The
WebServiceHostis designed to be used with REST/JSON services which typically have no defined contract, hence why metadata (mex) is disabled.If you are trying to create a SOAP based service, you need to use a standard
ServiceHost. It looks like this is what you want, as you are trying to add the service reference through VS.If you are trying to create a REST/JSON service, you can use
WebServiceHost.