C # dynamic WebService call

Source: Internet
Author: User

 

Method 1:
The content of the web service has not changed, but the content of the Web Service has changed to different places. For example, from localhost: 8080/a. asmx to localhost: 8090/a. asmx. In this way, you do not have to modify the Web reference again.

Webs. a =   New Webs. ();
A. url = " Http: // localhost: 8090/a. asmx "
A. remotedosomething ();

You can store the URL string in config and retrieve it when using it.

Method 2:

Private   Void Page_load ( Object Sender, system. eventargs E)
{
// Place user code here to initialize the page
Object OBJ = Invokewebservice ( " Http: // localhost/getpro. asmx " , " Etoonproduct. Web. Manage " , " Getpro " , " Getarea " , New Object [] {"1"} );
This . Datagrid1.datasource = OBJ;
This . Datagrid1.databind ();
}  
/**/ /// Call the remote WebService Method Based on the specified information
///  
/// HTTP address of WebService
/// Namespace of the WebService to be called
/// The Class Name of the WebService to be called (excluding the namespace prefix)
/// Method Name of the WebService to be called
/// Parameter List
/// WebService execution result
///  
/// If the call fails, an exception is thrown. During the call, an exception is intercepted as appropriate.
/// Exception information may occur in two places:
/// 1. compileassembly fails when a WebService is dynamically constructed.
/// 2. the WebService itself fails to be executed.
///  
///  
///  
/// Object OBJ = invokewebservice (" Http: // localhost/gsp_workflowwebservice/common. asmx "," Genersoft. Platform. Service. Workflow "," common "," gettooltype ", new object [] {" 1 "});
///  
///  
Private   Object Invokewebservice ( String URL, String @ Namespace, String Classname, String Methodname, Object [] ARGs)
{
Try  
{
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)
{
Throw NewException (ex. innerexception. Message,NewException (ex. innerexception. stacktrace ));
}  
}  

From: http://www.cnblogs.com/prolifes/articles/1235685.html

 

The WebService on the new address is not the same as the WebService on the old location, so that problems may occur during the call.
Otherwise, it would be okay if the WebService only changes the address.
Mywebservice. Service m_service = new testapp. mywebservice. Service ();
M_service.url = @ "Current WebService address ";
In this way, you can use it normally.

Related Article

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.