C # Dynamic Call WebService

Source: Internet
Author: User

1. Define WebService Call Class

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Net;usingSystem.IO;usingSystem.ServiceModel.Description;usingSystem.Web.Services.Description;usingsystem.codedom;usingMicrosoft.csharp;usingSystem.CodeDom.Compiler;usingServiceDescription =System.Web.Services.Description.ServiceDescription;namespaceWebService Test { Public classWebserviceinvoke {#regionInvokewebservice/// <summary>        ///Get WebService Type/// </summary>        /// <param name= "Wsurl" >WebService Address</param>        /// <returns></returns>        Private Static stringGetwsclassname (stringWsurl) {            string[] parts = Wsurl.split ('/'); string[] pps = Parts[parts. Length-1]. Split ('.'); returnpps[0]; }        /// <summary>        ///Call WebService/// </summary>        /// <param name= "Wsurl" >WebService Address</param>        /// <param name= "MethodName" >Method Name</param>        /// <param name= "args" >parameter list</param>        /// <returns></returns>         Public Static ObjectInvokewebservice (stringWsurl,stringMethodName,Object[] args) {            returnInvokewebservice (Wsurl,NULL, MethodName, args); }        /// <summary>        ///Call WebService/// </summary>        /// <param name= "Wsurl" >WebService Address</param>        /// <param name= "ClassName" >class name</param>        /// <param name= "MethodName" >Method Name</param>        /// <param name= "args" >parameter list</param>        /// <returns></returns>         Public Static ObjectInvokewebservice (stringWsurl,stringClassName,stringMethodName,Object[] args) {            string@namespace ="EnterpriseServerBase.WebService.DynamicWebCalling"; if(ClassName = =NULL) || (ClassName = ="") ) {ClassName=Getwsclassname (Wsurl); }            Try            {                //get WSDLWebClient WC =NewWebClient (); Stream Stream= WC. OpenRead (Wsurl +"? wsdl"); ServiceDescription SD=Servicedescription.read (stream); ServiceDescriptionImporter SDI=NewServiceDescriptionImporter (); Sdi. Addservicedescription (SD,"",""); CodeNamespace cn=NewCodeNamespace (@namespace); //Generate the client proxy class codeCodeCompileUnit CCU =NewCodeCompileUnit (); Ccu.                Namespaces.add (CN); Sdi.                Import (CN, CCU); CSharpCodeProvider csc=NewCSharpCodeProvider (); ICodeCompiler ICC=CSC.                CreateCompiler (); //Setting Compilation ParametersCompilerParameters cplist =NewCompilerParameters (); 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 classesCompilerResults CR =icc.compileassemblyfromdom (cplist, CCU); if(true==Cr. errors.haserrors) {System.Text.StringBuilder sb=NewSystem.Text.StringBuilder (); foreach(System.CodeDom.Compiler.CompilerError CEinchCr. Errors) {sb. Append (CE.                        ToString ()); Sb.                    Append (System.Environment.NewLine); }                    Throw NewException (sb.)                ToString ()); }                //build the proxy instance and call the methodSystem.Reflection.Assembly Assembly =cr.compiledassembly; Type T= assembly. GetType (@namespace +"."+ ClassName,true,true); Objectobj =activator.createinstance (t); System.Reflection.MethodInfo mi=T.getmethod (methodName); returnmi.            Invoke (obj, args); }            Catch(Exception ex) {Throw NewException (ex. Innerexception.message,NewException (ex.            Innerexception.stacktrace)); }        }        #endregion    }}

2. Define the Calling method

 Public voidRewebserviceresult (Object[] jsonstri) {            Try            {                stringURL ="Http://localhost:25064/Client/IWSLedgerService.asmx"; //string url = "Http://localhost/WebService/Service.asmx"; //string[] args = new string[1]; //args[0] = "P000001";                Objectresult = Webserviceinvoke.invokewebservice (URL,"Logisticsledgerservice", Jsonstri);//Logisticsledgerservice is the WebService method that needs to be called}Catch(Exception ex) {}}

C # Dynamic Call WebService

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.