Public class WebServiceHelper
{
/// <Summary>
/// Dynamically call the web Service
/// </Summary>
/// <Param name = "methodname"> method name </param>
/// <Param name = "args"> parameter </param>
/// <Returns> </returns>
Public static object InvokeWebService (string methodname, object [] args)
{
String url = "http: // localhost: 8080/myWebService. asmx ";
String @ namespace = "myWebService"; // namespace
String classname = WebServiceHelper. GetWsClassName (url); // "myWebService ";
Try
{
// Obtain the WSDL
WebClient wc = new WebClient ();
Stream stream = wc. OpenRead (url + "? WSDL ");
ServiceDescription sd = ServiceDescription. Read (stream );
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter ();
Sdi. AddServiceDescription (sd ,"","");
CodeNamespace cn = new CodeNamespace (@ namespace );
// Generate client proxy code
CodeCompileUnit ccu = new CodeCompileUnit ();
Ccu. Namespaces. Add (cn );
Sdi. Import (cn, ccu );
CSharpCodeProvider icc = new CSharpCodeProvider ();
// Set compilation Parameters
CompilerParameters cplist = new CompilerParameters ();
Cplist. GenerateExecutable = false;
Cplist. GenerateInMemory = true;
Cplist. ReferencedAssemblies. Add ("System. dll ");
Cplist. ReferencedAssemblies. Add ("System. XML. dll ");
Cplist. ReferencedAssemblies. Add ("System. Web. Services. dll ");
Cplist. ReferencedAssemblies. Add ("System. Data. dll ");
// Compile the proxy class
CompilerResults cr = icc. CompileAssemblyFromDom (cplist, ccu );
If (true = cr. Errors. HasErrors)
{
System. Text. StringBuilder sb = new System. Text. StringBuilder ();
Foreach (System. CodeDom. Compiler. CompilerError ce in cr. Errors)
{
Sb. Append (ce. ToString ());
Sb. Append (System. Environment. NewLine );
}
Throw new Exception (sb. ToString ());
}
// Generate a proxy instance and call the Method
System. Reflection. Assembly assembly = cr. CompiledAssembly;
Type t = assembly. GetType (@ namespace + "." + classname, true, true );
Object obj = Activator. CreateInstance (t );
System. Reflection. MethodInfo mi = t. GetMethod (methodname );
Return mi. Invoke (obj, args );
// PropertyInfo propertyInfo = type. GetProperty (propertyname );
// Return propertyInfo. GetValue (obj, null );
}
Catch (Exception ex)
{
Throw new Exception (ex. InnerException. Message, new Exception (ex. InnerException. StackTrace ));
}
}
Private static string GetWsClassName (string wsUrl)
{
String [] parts = wsUrl. Split ('/');
String [] pps = parts [parts. Length-1]. Split ('.');
Return pps [0];
}
}
// Webservice call example:
/// <Summary>
/// User Logon Verification
/// </Summary>
/// <Param name = "username"> User name </param>
/// <Param name = "pwd"> password </param>
/// <Param name = "userid"> User ID </param>
/// <Param name = "error"> interface exception information </param>
/// <Returns> return true If logon succeeds </returns>
Public bool Login (string username, string pwd, out string userid, out string error)
{
Object [] obj = new object [] {username, pwd, null, null };
Bool res = (bool) WebServiceHelper. InvokeWebService ("Login", obj );
Userid = (string) obj [2];
Error = (string) obj [3];
Return res;
}