C # dynamic WebService call

Source: Internet
Author: User
Code
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. xml;
Using System. net;
Using System. Web. Services. description;
Using System. codedom;
Using System. codedom. compiler;
Using System. reflection;
Namespace Windowsservicewebdefaulthotcity
{
///   <Summary>
/// WebService proxy
///   </Summary>
Public   Class Webserviceagent
{
Private   Object Agent;
Private Type agenttype;
Private   Const   String Code_namespace =   " Beyondbit. webserviceagent. Dynamic " ;
///   <Summary>
/// Constructor
///   </Summary>
///   <Param name = "url"> </param>
Public Webserviceagent ( String URL)
{
Xmltextreader Reader =   New Xmltextreader (URL +   " ? WSDL " );

//Create and format a wsdl document
Servicedescription SD=Servicedescription. Read (Reader );

//Create a client proxy
Servicedescriptionimporter SDI= NewServicedescriptionimporter ();
SDI. addservicedescription (SD,Null,Null);

// Use codedom to compile client proxy classes
Codenamespace CN =   New Codenamespace (code_namespace );
Codecompileunit CCU =   New Codecompileunit ();
CCU. namespaces. Add (CN );
SDI. Import (CN, CCU );
Microsoft. CSHARP. csharpcodeprovider ICC =   New Microsoft. CSHARP. csharpcodeprovider ();
Compilerparameters CP =   New Compilerparameters ();
Compilerresults cr = ICC. compileassemblyfromdom (CP, CCU );
Agenttype = Cr. compiledassembly. gettypes ()[ 0 ];
Agent = Activator. createinstance (agenttype );
}

/// <Summary>
/// Call the specified Method
/// </Summary>
/// <Param name = "methodname"> Method Name, case sensitive </Param>
/// <Param name = "ARGs"> Parameters. Values are assigned in the order of parameters. </Param>
/// <Returns> Web Service Return Value </Returns>
Public   Object Invoke ( String Methodname, Params   Object [] ARGs)
{
Methodinfo Mi = Agenttype. getmethod (methodname );
Return   This . Invoke (MI, argS );
}
/// <Summary>
/// Call a specified Method
/// </Summary>
/// <Param name = "method"> Method Information </Param>
/// <Param name = "ARGs"> Parameters. Values are assigned in the order of parameters. </Param>
/// <Returns> Web Service Return Value </Returns>
Public   Object Invoke (methodinfo method, Params   Object [] ARGs)
{
Return Method. Invoke (agent, argS );
}
Public Methodinfo [] Methods
{
Get
{
Return Agenttype. getmethods ();
}
}
}
}

Code
Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;

UsingSystem. text;
UsingSystem. Windows. forms;

Namespace Windowsapplication1
{
Public   Partial   Class Form1: Form
{
Private   String _ URL =   " Http://www.baidu.com " ;
Public Form1 ()
{
Initializecomponent ();
Init_data ();
}

Public   Void Init_data ()
{
Windowsservicewebdefaulthotcity. webserviceagent agent =   New Windowsservicewebdefaulthotcity. webserviceagent (_ URL );
Object [] ARGs =   New   Object [ 6 ];
ARGs [ 0 ] =   " Pek " ;
ARGs [ 1 ] =   " Can " ;
ARGs [ 2 ] =   "" ;
ARGs [ 3 ] =   " 2008-08-02 " ;
ARGs [ 4 ] =   " 00: 00 " ;
ARGs [ 5 ] =   " Own_9588 " ;
String Text = Agent. Invoke ( " Getallflight " , ArgS). tostring ();
Textbox1.text = Text;
}
}
}

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.