Hi, i've been playing around with Attributes over web services, and i've seen that The SoapHttpClientProtocol class need to define a WebServiceBinding Attribute.
Watching this question there seems one cannot modify the attributes on runtime, how can I achieve a dynamic web service invocation changing this attribute on runtime? is it possible?
[Edit] I'm searching for an approach to do a dynamic invoke "generic style", rather to modify the WebServiceBinding attribute.
This is, in a nutshell, my class:
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);
}
}
}
[Edit] I would like to call my class like so:
var miProxy = new WebServiceInvoker("http://webServiceLocation", "ns", "Binding");
var res = miProxy.InvokeFunction<string, string, Entity>("MyWebMethod", stringPar1, stringPar2);