Dynamic WebServices calling through reflection

Source: Internet
Author: User

This method can makeProgramThe WebServices method is called without web reference.CodeTo dynamically call WebServices. Before using the SDK, reference the system. Web. Services dynamic link library, which is a. Net built-in DLL.

The method is as follows:

Using System;
Using System. Collections. Generic;
Using System. text;
Using System. net;
Using System. IO;
Using System. Web. Services. description;
Using System. codedom;
Using Microsoft. CSHARP;
Using System. codedom. compiler;

Namespace Testskin
{
Class WebServices
{
///   <Summary>
/// Instantiate WebServices
///   </Summary>
///   <Param name = "url"> WebServices address </Param>
///   <Param name = "methodname"> Call Method </Param>
///   <Param name = "ARGs"> Put the parameters required in WebServices in this object [] in order. </Param>
Public   Static   Object Invokewebservice ( String URL, String Methodname, Object [] ARGs)
{

// The namespace here is the namespace of the WebServices to be referenced. It is written to death. You can add a parameter to pass it in from the outside.
String @ Namespace =   " Client " ;
Try
{
// Get WSDL
WebClient WC =   New WebClient ();
Stream stream = WC. openread (URL +   " ? WSDL " );
Servicedescription SD = Servicedescription. Read (Stream );
String Classname = SD. Services [ 0 ]. Name;
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 CSC =   New Csharpcodeprovider ();
Icodecompiler ICC = CSC. createcompiler ();

// 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 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 proxy instance and call Method
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);
}< br> catch
{< br> return null ;
}< BR >}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.