C #. Net Dynamic webService call Implementation ideas and code

Source: Internet
Author: User

Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Net;
Using System. IO;
Using System. Web. Services. Description;
Using System. CodeDom;
Using Microsoft. CSharp;
Using System. CodeDom. Compiler;
Using System. Reflection;
Namespace HTTPS
{
Public class WSHelper
{
/// <Summary>
/// Dynamically call the web Service
/// </Summary>
/// <Param name = "url"> WSDL service address </param>
/// <Param name = "methodname"> method name </param>
/// <Param name = "args"> parameter </param>
/// <Returns> </returns>
Public static object InvokeWebService (string url, string methodname, object [] args)
{
Return WSHelper. InvokeWebService (url, null, methodname, args );
}
/// <Summary>
/// Dynamically call the web Service
/// </Summary>
/// <Param name = "url"> WSDL service address </param>
/// <Param name = "classname"> class name </param>
/// <Param name = "methodname"> method name </param>
/// <Param name = "args"> parameter </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 = WSHelper. GetWsClassName (url );
}
Try
{// Obtain the 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 client proxy code
CodeCompileUnit ccu = new CodeCompileUnit ();
Ccu. Namespaces. Add (cn );
Sdi. Import (cn, ccu );
CSharpCodeProvider icc = new CSharpCodeProvider ();
// 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 the 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 a 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 );
// PropertyInfo propertyInfo = type. 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];
}
}
}

Call
Copy codeThe Code is as follows:
String url = "http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx ";
String [] args = new string [2];
Args [0] = "k123 ";
Args [1] = "";
Object result = WSHelper. InvokeWebService (url, "getDetailInfoByTrainCode", args );
DataSet ds = (DataSet) result;
This. GridView1.DataSource = ds;
This. GridView1.DataBind ();

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.