public class Ccbwebsevicehelper
{
public static int rowCount = 0;
< summary>
Dynamic invocation of Web services when there is an entity class (only for one)
</summary>
< param name= "url" >wsdl service address </param>
< param name= "classname" > class name </param>
< param name= "classname" > Entity class Class name (change method when there are multiple entity classes, loop get entity class re-assign value to called method parameters object[]) </param>
< param name= "methodname" > method name </param>
< param name= "args" > parameters, assign values to incoming parameter arrays according to the Order of the service-parameter fields </param>
< returns></returns>
public static object Invokewebservice (string url, string classname, String entityname, String methodname, object[] args)
{
String @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
if ((classname = = null) | | (ClassName = = ""))
{
classname = getwsclassname (URL);
}
Try
{//Get 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 the client proxy class code
CodeCompileUnit CCU = new CodeCompileUnit ();
Ccu. Namespaces.add (CN);
Sdi. Import (CN, CCU);
CSharpCodeProvider ICC = new CSharpCodeProvider ();
Setting 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");
Compiling proxy classes
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 ());
}
Build the proxy instance and call the method
System.Reflection.Assembly Assembly = cr.compiledassembly;
Get entity class
Type entittype = assembly. GetType (@namespace + "." + EntityName, True, true);
Object entitobj = Activator.CreateInstance (Entittype);
system.reflection.fieldinfo[] fields = Entittype.getfields ();
for (int i = 0; i < args. Count (); i++)
{
Fields[i]. SetValue (Entitobj, args[i]);
}
Incoming entity class parameters
Object[] Methodargs = {Entitobj};
Type t = assembly. GetType (@namespace + "." + ClassName, True, true);
Object obj = activator.createinstance (t);
System.Reflection.MethodInfo mi = t.getmethod (methodname);
Return MI. Invoke (obj, Methodargs);
System.Reflection.PropertyInfo PropertyInfo = T.getproperty (PropertyName);
return propertyinfo.getvalue (obj, null);
}
catch (Exception ex)
{
rowcount++;
Object o;
if (RowCount = = 3)
{
throw new Exception (ex. Innerexception.message, New Exception (ex. Innerexception.stacktrace));
}
Else
{
o = Invokewebservice (URL, classname, entityname, MethodName, args);
}
return o;
}
}
< summary>
Dynamic invocation of Web services, common method incoming
</summary>
< param name= "url" >wsdl service address </param>
< param name= "classname" > class name </param>
< param name= "methodname" > method name </param>
< param name= "args" > parameters, assign values to incoming parameter arrays according to the Order of the service-side parameters </param>
< returns></returns>
public static object Invokewebservice (string url, string classname, String methodname, object[] args)
{
String @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
if ((classname = = null) | | (ClassName = = ""))
{
classname = getwsclassname (URL);
}
Try
{//Get 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 the client proxy class code
CodeCompileUnit CCU = new CodeCompileUnit ();
Ccu. Namespaces.add (CN);
Sdi. Import (CN, CCU);
CSharpCodeProvider ICC = new CSharpCodeProvider ();
Setting 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");
Compiling proxy classes
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 ());
}
Build the 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);
System.Reflection.PropertyInfo PropertyInfo = T.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];
}
}
Dynamic invocation of Web services when there is an entity class (only for one)