我一直在使用Web服务上的属性,我已经看到SoapHttpClientProtocol类需要定义一个WebServiceBinding属性. 看到这个question似乎无法修改运行时的属性,如何实现动态Web服务调用在运行时更改此属性?
看到这个question似乎无法修改运行时的属性,如何实现动态Web服务调用在运行时更改此属性?可能吗?
[编辑]
我正在寻找一种方法来执行动态调用“泛型样式”,而不是修改WebServiceBinding属性.
简而言之,这是我的班级:
using System.Web.Services;
using System.Web.Services.Protocols;
namespace Whatever {
[WebServiceBinding(Name = "", Namespace = "")]
public class WebServiceInvoker : SoapHttpClientProtocol {
public WebServiceInvoker(string url, string ns, string bindingName) {
ChangeNamespace(ns);
ChangeBinding(bindingName);
Url = url;
// credentials, etc
}
public void ChangeNamespace(string ns) {
var att = GetType().GetCustomAttributes(typeof (WebServiceBindingAttribute), true);
if (att.Length > 0) {
// doesn't work
((WebServiceBindingAttribute)att[0]).Namespace = ns;
}
}
private void ChangeBinding(string bindingName) {
var att = GetType().GetCustomAttributes(typeof(WebServiceBindingAttribute), true);
if (att.Length > 0) {
// doesn't work
((WebServiceBindingAttribute)att[0]).Name = bindingName;
}
}
public object[] MakeInvoke(string method, object[] args) {
var res = Invoke(method, method);
return res;
}
public TRet InvokeFunction<TRet>(string method) {
//Funcion<T1, T2, T3, TRet>
var res = Invoke(method, null);
return MyUtils.ForceCast<TRet>(res);
}
public TRet InvokeFunction<T1, TRet>(string method, T1 par1) {
//Funcion<T1, T2, T3, TRet>
var args = new object[] { par1 };
var res = Invoke(method, args);
return MyUtils.ForceCast<TRet>(res);
}
public TRet InvokeFunction<T1, T2, TRet>(string method, T1 par1, T2 par2) {
//Funcion<T1, T2, T3, TRet>
var args = new object[] { par1, par2 };
var res = Invoke(method, args);
return MyUtils.ForceCast<TRet>(res);
}
public TRet InvokeFunction<T1, T2, T3, TRet>(string method, T1 par1, T2 par2, T3 par3) {
//Funcion<T1, T2, T3, TRet>
var args = new object[] {par1, par2, par3};
var res = Invoke(method, args);
return MyUtils.ForceCast<TRet>(res);
}
public void InvokeAction(string metodo) {
//Funcion<T1, T2, T3, TRet>
Invoke(method, null);
}
public void InvokeAction<T1>(string method, T1 par1) {
//Funcion<T1, T2, T3, TRet>
var args = new object[] { par1 };
Invoke(method, args);
}
public void InvokeAction<T1, T2>(string method, T1 par1, T2 par2) {
//Funcion<T1, T2, T3, TRet>
var args = new object[] { par1, par2 };
Invoke(method, args);
}
public void InvokeAction<T1, T2, T3>(string method, T1 par1, T2 par2, T3 par3) {
//Funcion<T1, T2, T3, TRet>
var args = new object[] { par1, par2, par3 };
Invoke(method, args);
}
}
}
[编辑]
我想这样打电话给我的班级:
var miProxy = new WebServiceInvoker("http://webServiceLocation", "ns", "Binding");
var res = miProxy.InvokeFunction<string, string, Entity>("MyWebMethod", stringPar1, stringPar2);
我不确定你要在这里完成什么…你是否正在尝试创建一个支持调用者调用任意方法的Web服务?如果是这样,这里有一个例子:
Creating a dynamic Web service to simplify code
