如何使用可配置的 URL 调用 Web 服务 [英] How to call a web service with a configurable URL
问题描述
我写了一个网络服务.我写了一个网站.我希望网站 BLL 代码调用网络服务.
I wrote a web-service. I wrote a website. I want the website BLL code to call the web-service.
我有一个包含此服务 URL 的配置表.我将 Web 服务 URL 注入调用代码.我应该使用 C# 中的哪种 Web 客户端或套接字来接收动态 Web 服务 URL?
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?
我想用:
WebClient webClient = new WebClient();
UTF8Encoding response = new UTF8Encoding();
string originalStr = response.GetString(webClient.DownloadData(BLLConfig.Current);
但也许有更优雅的方式?
But maybe there is more elegant way?
我正在运行时从数据库表加载配置.
以下是我尝试在 Visual Studio 中使用网络引用的方法:
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);
}
}
推荐答案
您可以在 Visual Studio 中将 Web 服务的 URL 添加为 Web 引用,然后将 Service.URL 属性设置为配置中的值
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
这篇关于如何使用可配置的 URL 调用 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!