How do I get the WSDL/description so that I can add WebReference in another project?
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 doin开发者_如何转开发g 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 WebServiceHost
will remove the functionality that you are trying to achieve when adding the ServiceMetadataBehavior
. Looking in dotPeek (reflector) at the WebServiceHost
, inside the OnOpening
method there is:
ServiceDebugBehavior serviceDebugBehavior = this.Description.Behaviors.Find<ServiceDebugBehavior>();
if (serviceDebugBehavior != null)
{
serviceDebugBehavior.HttpHelpPageEnabled = false;
serviceDebugBehavior.HttpsHelpPageEnabled = false;
}
ServiceMetadataBehavior metadataBehavior = this.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (metadataBehavior != null)
{
metadataBehavior.HttpGetEnabled = false;
metadataBehavior.HttpsGetEnabled = false;
}
The WebServiceHost
is 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
.
You will need a ServiceMetadataBehavior added to the host refer MSDN Reference.
// Enable Mex
host.Description.Behaviors.Add(new ServiceMetadataBehavior{ HttpGetEnabled = true });
I think you need to create WSDL or disco file for your webservice,For that run your webservice locally ,as you said it is executed fine.In my case when I execute my webservice I get link on the top of my page as "Service Description" .When you will click here you will get your WSDL file in browser.Another way is to add "?wsdl" at the end of your querystring ,You will get your wsdl file.
精彩评论