Adding .ashx as a web reference
For my application, I'm currently using a webservice to retrieve some information I need in XML format. The way I'm doing it now is via a HTTP web request, so the code looks something like this:
serviceURL = "http://longurl/webservice.ashx?apikey=key&action=ge开发者_开发百科tDetail&id=ID";
HttpWReq = (HttpWebRequest)WebRequest.Create(serviceURL);
HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
dataXML.Load(HttpWResp.GetResponseStream());
I feel that including the entire URL inside my code looks really clunky, so I've thought of adding the web service as a web reference. However, I came across this error when trying to add the web reference:
The document at the url longurl/webservice.ashx was not recognized as a known document type.
The error message from each known type may help you fix the problem: - Report from 'DISCO Document' is 'Discovery document at the URL longurl/webservice.ashx could not be found.'. - The document format is not recognized. - Report from 'WSDL Document' is 'There is an error in XML document (2, 2).'. - was not expected. - Report from 'XML Schema' is 'The root element of a W3C XML Schema should be and its namespace should be ''.'.
Am I missing any steps that I had to do before attempting to add the web service? Or is this a problem with the web service itself?
I'd also appreciate any advice on alternative methods to code this a bit more elegantly. Thanks.
I would honestly just put that url in an AppSetting.
so that serviceURL = ConfigurationManager.AppSettings["ServiceUrl"];
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx
A "web reference" is intended for an "old-style" ASMX web service, a "service reference" for a WCF service. You can't use (or abuse) that functionality for what you're trying to do.
Either just put your URL into config as "Min" suggested, or then change to a real WCF service. With the WCF Rest Starter Kit, you can create these types of "HTTP Plain Old XML (POX)" services quite easily - see the Pluralsight screencast on that topic for more information.
Marc
精彩评论