简体   繁体   English

如何使用可配置的URL调用Web服务

[英]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. 我希望网站的BLL代码调用该Web服务。

I have a config table with this service URL. 我有一个带有此服务URL的配置表。 I inject the web-service URL to the calling code. 我将Web服务URL注入到调用代码中。 What web client or socket in C# should I use that can receive a dynamic web-service URL? 我应该使用C#中的哪些Web客户端或套接字来接收动态Web服务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: 这是我尝试在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);
    }
}

您可以在Visual Studio中将Web服务的URL添加为Web引用,然后将Service.URL属性设置为配置中的值

.NET具有许多内置的支持使用Web服务的功能...将服务引用添加到项目中后,它会生成必要的代码...您可以按原样使用-如果需要配置URL,请生成的客户端类具有可以相应设置的URL属性...有关出色的演练,请参见http://johnwsaunders3.wordpress.com/2009/05/17/how-to-consume-a-web-service/并查看SOAP xml客户端-使用Visual Studio 2010 C#-如何?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM