I played with attributes over web services, and I saw that the SoapHttpClientProtocol class should define the WebServiceBinding attribute.
Watching the question , it seems like you cannot change attributes at runtime, how can I achieve a dynamic web service call by changing this attribute at runtime? is it possible?
[Change] I am looking for an approach to dynamically invoke the "universal style", and not to change the WebServiceBinding attribute.
This, in a nutshell, is 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;
}
public void ChangeNamespace(string ns) {
var att = GetType().GetCustomAttributes(typeof (WebServiceBindingAttribute), true);
if (att.Length > 0) {
((WebServiceBindingAttribute)att[0]).Namespace = ns;
}
}
private void ChangeBinding(string bindingName) {
var att = GetType().GetCustomAttributes(typeof(WebServiceBindingAttribute), true);
if (att.Length > 0) {
((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) {
var res = Invoke(method, null);
return MyUtils.ForceCast<TRet>(res);
}
public TRet InvokeFunction<T1, TRet>(string method, T1 par1) {
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) {
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) {
var args = new object[] {par1, par2, par3};
var res = Invoke(method, args);
return MyUtils.ForceCast<TRet>(res);
}
public void InvokeAction(string metodo) {
Invoke(method, null);
}
public void InvokeAction<T1>(string method, T1 par1) {
var args = new object[] { par1 };
Invoke(method, args);
}
public void InvokeAction<T1, T2>(string method, T1 par1, T2 par2) {
var args = new object[] { par1, par2 };
Invoke(method, args);
}
public void InvokeAction<T1, T2, T3>(string method, T1 par1, T2 par2, T3 par3) {
var args = new object[] { par1, par2, par3 };
Invoke(method, args);
}
}
}
[Edit] I would call my class as follows:
var miProxy = new WebServiceInvoker("http://webServiceLocation", "ns", "Binding");
var res = miProxy.InvokeFunction<string, string, Entity>("MyWebMethod", stringPar1, stringPar2);