C#. NET Dynamic call WebService implementation ideas and code _ practical skills

Source: Internet
Author: User
Tags wsdl
Copy Code code 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>
To invoke a Web service dynamically
</summary>
< param name= "url" >wsdl service address </param>
< param name= "methodname" > method name </param>
< param name= "args" > Parameters </param>
< returns></returns>
public static object Invokewebservice (string URL, String methodname, object[] args)
{
Return wshelper.invokewebservice (URL, null, methodname, args);
}
< summary>
To invoke a Web service dynamically
</summary>
< param name= "url" >wsdl service address </param>
< param name= "classname" > class name </param>
< param name= "methodname" > method name </param>
< param name= "args" > Parameters </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
{//Get 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 class 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");
Compiling proxy classes
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 ());
}
Generates a proxy instance and calls 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 Code code 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 ();

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.