How to call a web service with a configurable URL

混江龙づ霸主 提交于 2020-01-07 07:48:06

问题


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:

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);
    }
}

回答1:


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




回答2:


.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?



来源:https://stackoverflow.com/questions/7571253/how-to-call-a-web-service-with-a-configurable-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!