|
| Invoke a Method in a Web Service |
 |
Wed, 26 Mar 2008 22:00:08 +000 |
To all,
I'd like to create a web service that handles all the DB IO for my web site.
However I don't want to set a reference from my website to the WSDL created for
that web service or include a proxy class for that particular web service in my
solution.
Ideally I'd like to be able to store the URL where that web service is running
in the web config file and instansiate a generic proxy class (I think) and then
call the particular web method in the referenced web service url by a string
parameter. So if the method in the web service was called HelloWorld, I'd like
to call it by passing a string parameter to a procedure indicating the method I
want to invoke, in this case "HelloWorld".
Is this possible?
Thanks.
Steve
|
| Post Reply
|
| Re: Invoke a Method in a Web Service |
 |
Wed, 26 Mar 2008 23:10:12 +000 |
This is possible, but why add such complexity?
I'll try to piece together how I was doing this before but good luck...
The following code has been grossly modified, but it still lists the important
parts.
You will need a reference to a dll that I have too and I have no really good way
of getting it to you.... I could email it to you.
internal static void RunTask(string Input)
{
bool result = false;
CallWebTask_Delegate callWebTask = CallWebTask;
ServiceInfo serviceInfo =
new
ServiceInfo(http://www.youdomain.com/webservice/service.asmx/methodname?param1=v
alue1¶m2=value2);
result = callWebTask(serviceInfo);
}
private delegate bool CallWebTask_Delegate(ServiceInfo serviceInfo);
internal static bool CallWebTask(ServiceInfo serviceInfo)
{
object result;
bool retVal = false;
try
{
DynamicClient.WebServiceProxyFactory WS_factory = new
DynamicClient.WebServiceProxyFactory(WebServiceUrl + "?wsdl");
object proxy = WS_factory.Build();
System.Reflection.MethodInfo info =
proxy.GetType().GetMethod(serviceInfo.WebMethod);
System.Reflection.ParameterInfo[] paramInfo = info.GetParameters();
string[] sParams = serviceInfo.Parameters;
object[] oParams = new object[serviceInfo.Parameters.Length];
for (int i=0; i < sParams.Length; i++)
oParams[i] = System.Convert.ChangeType(sParams[i],
paramInfo[i].ParameterType);
result = proxy.GetType().InvokeMember(
serviceInfo.WebMethod, System.Reflection.BindingFlags.InvokeMethod, null,
proxy, oParams);
retVal = true;
}
catch (System.Web.Services.Protocols.SoapException exSOAP)
{
ReportError();
}
catch (System.Exception ex)
{
ReportError();
}
return retVal;
}
internal class ServiceInfo
{
private string _URL;
public string WebMethod {
get { // return WebMethod part of URL }
}
public string WebService {
get { // return WebService part of URL }
}
public string[] {
get { // return an array of query string parameters }
}
public ServiceInfo(string URL)
{
_URL = URL;
}
}
|
| Post Reply
|
|
|
|
|
|
|
|
|
|