Dynamic Call WebService

Source: Internet
Author: User
Tags add object net reflection string tostring
web| Dynamic///<summary>
Invokes the remote WebService method based on the specified information
</summary>
<param name= "url" >webservice http form of address </param>
<param name= "namespace" > the namespace of the WebService to invoke </param>
<param name= "ClassName" > The class name of the WebService to invoke (excluding the namespace prefix) </param>
<param name= "methodname" > The WebService method name to invoke </param>
<param name= "args" > Parameter list </param>
Execution results of <returns>webservice </returns>
<remarks>
If the call fails, the exception is thrown. Please call, properly intercept the exception.
Exception information can occur in two places:
1, the dynamic construction of WebService time, compileassembly failure.
2, webservice itself failed to execute.
</remarks>
<example>
<code>
Object obj = Invokewebservice ("Http://localhost/GSP_WorkflowWebservice/common.asmx", " Genersoft.Platform.Service.Workflow "," Common "," Gettooltype ", New object[]{" 1 "});
</code>
</example>
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 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 new Exception (ex. Innerexception.message,new Exception (ex. Innerexception.stacktrace));
}
}



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.