【问题标题】:ASP.net - connect to multiple Web Services from different URL's/host that share the same interface?ASP.net - 从共享相同接口的不同 URL/主机连接到多个 Web 服务?
【发布时间】:2013-01-25 14:57:14
【问题描述】:
我希望我的 ASP.NET 应用程序能够与任意数量的不同主机通信,所有主机都提供具有完全相同接口的 Web 服务,但在不同的域/ASMX URL 上。我找到了here 一个解决方案,它允许我为一个 Web 服务生成一个类,但是 URL 地址/前缀/命名空间在方法属性中是硬编码的,我不知道如何更改它们(相关问题 @987654322 @)。还有其他解决方案吗?
【问题讨论】:
标签:
c#
asp.net
web-services
【解决方案1】:
一种可能的解决方案是使用 DynWsib - HERE。请注意,这不适用于 WCF。
然后您可以在运行时调用。为每个 url 创建和缓存二进制文件。下面的函数是基本思想。根据需要进行更改。
public object InvokeWebserviceCall(string wsdUrl, string actionUrl, string functionName,
string domain, string username, string password, params object[] parameters)
{
///todo: validate input
var proxy = new DynamicWebServiceProxy();
//credentials if needed
if (!string.IsNullOrEmpty(domain))
{
proxy.Credentials = new NetworkCredential(username, password, domain);
}
else if (!string.IsNullOrEmpty(username))
{
proxy.Credentials = new NetworkCredential(username, password);
}
proxy.EnableMessageAccess = true;
proxy.Wsdl = wsdUrl;
//get type name
var type = proxy.ProxyAssembly.GetTypes().SingleOrDefault(t => t.BaseType == typeof(SoapHttpClientProtocolExtended));
if (type != null)
{
proxy.TypeName = type.Name;
}
proxy.MethodName = functionName;
proxy.Url = new Uri(actionUrl);
if (parameters != null)
{
parameters.ToList().ForEach(proxy.AddParameter);
}
object result = proxy.InvokeCall();
return result;
}