I'd like my ASP.NET application to be able to talk to any number of different hosts, all providing a Web Service that has exactly the same interface, but on different domains/ASMX URL's. I've found here a solution that allows me to generate a class for one web service, but the URL address/prefix/namespaces are hardcoded in the method attributes and I don't know how to change them (related question here). Are there any other solutions?
我希望我的ASP.NET应用程序能够与任意数量的不同主机通信,所有主机都提供具有完全相同接口但在不同域/ ASMX URL上的Web服务。我在这里找到了一个解决方案,允许我为一个Web服务生成一个类,但URL地址/前缀/命名空间在方法属性中是硬编码的,我不知道如何更改它们(这里的相关问题)。还有其他解决方案吗?
1 个解决方案
#1
1
One possible solution is using DynWsib - HERE. Please note this does not work with WCF.
一种可能的解决方案是使用DynWsib - HERE。请注意,这不适用于WCF。
You can then invoke at runtime. Binaries are created and cached for each url. Function below is basic idea. Change as needed.
然后,您可以在运行时调用。为每个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;
}
#1
1
One possible solution is using DynWsib - HERE. Please note this does not work with WCF.
一种可能的解决方案是使用DynWsib - HERE。请注意,这不适用于WCF。
You can then invoke at runtime. Binaries are created and cached for each url. Function below is basic idea. Change as needed.
然后,您可以在运行时调用。为每个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;
}