Web Service is familiar to everyone. When interactions between multiple systems are very frequent, Web services must always be tested during development to ensure that web services can run normally. The common method is to enter the Web service address for testing. However, WebService is often deployed on other machines and cannot be directly tested. You can only write the testCodeThe test is very complicated. Here, we can dynamically call the Web Service for testing.
Dynamic call of web service methods
Public Static Object Invokewebservice ( String URL, String Methodname, Object [] ARGs)
{
Try
{
String @ Namespace = " WebService. dynamicwebcalling " ;
String [] Parts = URL. Split ( ' / ' );
String [] PPS = Parts [parts. Length - 1 ]. Split ( ' . ' );
String Classname = PPS [ 0 ];
System. net. WebClient WC = New System. net. WebClient ();
System. Io. Stream stream = WC. openread (URL + " ? WSDL " );
System. Web. Services. description. servicedescription SD = System. Web. Services. description. servicedescription. Read (Stream );
System. Web. Services. description. servicedescriptionimporter SDI = New System. Web. Services. description. servicedescriptionimporter ();
SDI. addservicedescription (SD, "" , "" );
System. codedom. codenamespace CN = New System. codedom. codenamespace (@ namespace );
System. codedom. codecompileunit CCU = New System. codedom. codecompileunit ();
CCU. namespaces. Add (CN );
SDI. Import (CN, CCU );
Microsoft. CSHARP. csharpcodeprovider CSC = New Microsoft. CSHARP. csharpcodeprovider ();
System. codedom. compiler. icodecompiler ICC = CSC. createcompiler ();
System. codedom. compiler. compilerparameters cplist = New System. codedom. compiler. 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 " );
System. codedom. compiler. 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 ());
}
System. reflection. 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 );
}
Catch (Exception ex)
{
ThrowEx;
}
}
ProgramRun Interface
Source codeDownload
/Files/binbin1845/wstest.rar