How to call a web service with a configurable URL
I wrote a web-service. I wrote a website. I want the website BLL code to call the web-service.
I have a config table with this service URL. I inject the web-service URL to the calling code. What web client or socket in C# should I use that can receive a dynamic web-service URL?
I thought to use:
开发者_Go百科WebClient webClient = new WebClient();
UTF8Encoding response = new UTF8Encoding();
string originalStr = response.GetString(webClient.DownloadData(BLLConfig.Current);
But maybe there is more elegant way?
I'm loading the configs at run time from a DB table.
Here is how I tried to use a web-reference in Visual Studio:
using (var client = new GetTemplateParamSoapClient("GetTemplateParamSoap"))
{
TemplateParamsKeyValue[] responsArray = client.GetTemplatesParamsPerId(CtId, tempalteIds.ToArray());
foreach (var pair in responsArray)
{
string value = FetchTemplateValue(pair.Key, pair.Value);
TemplateComponentsData.Add(pair.Key, value);
}
}
You can add the URL of the web service as a Web Reference in Visual Studio and then set the Service.URL property to the value from the config
.NET has lots of built-in support for consuming web services... after adding the service reference to your project it generates the necessary code... whcih you can use as is - if you need to configure the URL the generated client class has a URL
property which you can set accordingly... for an excellent walkthrough see http://johnwsaunders3.wordpress.com/2009/05/17/how-to-consume-a-web-service/ and see SOAP xml client - using Visual Studio 2010 c# - how?
精彩评论