Class for dynamic WebService calls by code

Source: Internet
Author: User
Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using system. net; using system. io; using system. web;// Right-click to add the. NET component system. Web, system. Web. Services, and system. Web. Extensions.Using system. web. services; using system. web. services. description; using system. web. services. protocols; using system. XML. serialization; using system. codedom; using system. codedom. compiler; using system. reflection; namespace mywebservicenamespace {class mywebserviceclass {// <summary> // WebService returns string, use this method /// </Summary> /// <Param name = "url"> call the WebService URL </param> /// <Param name = "classname"> W Ebservice class </param> /// <Param name = "methodname"> parameters of the WebService method </param> /// <Param name = "Parameters"> </param> /// <returns> string </returns> Public String resultstring (string URL, string classname, string methodname, object [] parameters) {string resulta = ""; try {// 1. use WebClient to download the WSDL information. WebClient client = new WebClient (); url = URL + "? WSDL "; stream = client. openread (URL); // 2. Create and format the WSDL document. Servicedescription description = servicedescription. Read (Stream); // 3. Create a client proxy class. Servicedescriptionimporter importer = new servicedescriptionimporter (); // create a client proxy class. Importer. protocolname = "Soap"; // specify the access protocol. Importer. Style = servicedescriptionimportstyle. Client; // generate a client proxy. Importer. codegenerationoptions = codegenerationoptions. generateproperties | codegenerationoptions. generatenewasync; importer. addservicedescription (description, null, null); // Add a WSDL document. // 4. Use codedom to compile the client proxy class. Codenamespace nmspace = new codenamespace (); // namespace codecompileunit unit = new codecompileunit (); unit. namespaces. add (nmspace); servicedescriptionimportwarnings warning = importer. import (nmspace, Unit); codedomprovider provider = codedomprovider. createprovider ("CSHARP"); compilerparameters parameter = new compilerparameters (); parameter. generateexecutable = false; parameter. referencedassemblies. ad D ("system. DLL "); parameter. referencedassemblies. add ("system. XML. DLL "); parameter. referencedassemblies. add ("system. web. services. DLL "); parameter. referencedassemblies. add ("system. data. DLL "); compilerresults result = provider. compileassemblyfromdom (parameter, Unit); // 5. use reflection to call WebService. Assembly ASM = result. compiledassembly; type T = ASM. getType (classname); // The parameter is the class name object o = Activator In the web service. createinstance (t); methodinfo method = T. getmethod (methodname); // parameter method name resulta = (string) method. invoke (O, parameters);} catch (exception ex) {resulta = ex. tostring () ;}return resulta ;}/// <summary> /// WebService returns dataset, use this method /// </Summary> /// <Param name = "url"> call the WebService URL </P Aram> /// <Param name = "classname"> WebService class </param> /// <Param name = "methodname"> call WebService method </param>/ // <Param name = "Parameters"> parameter </param> // <returns> dataset </returns> Public dataset resultdataset (string URL, string classname, string methodname, object [] parameters) {dataset resulta = NULL; try {// 1. use WebClient to download the WSDL information. WebClient client = new WebClient (); url = URL + "? WSDL "; stream = client. openread (URL); // 2. Create and format the WSDL document. Servicedescription description = servicedescription. Read (Stream); // 3. Create a client proxy class. Servicedescriptionimporter importer = new servicedescriptionimporter (); // create a client proxy class. Importer. protocolname = "Soap"; // specify the access protocol. Importer. Style = servicedescriptionimportstyle. Client; // generate a client proxy. Importer. codegenerationoptions = codegenerationoptions. generateproperties | codegenerationoptions. generatenewasync; importer. addservicedescription (description, null, null); // Add a WSDL document. // 4. Use codedom to compile the client proxy class. Codenamespace nmspace = new codenamespace (); // namespace codecompileunit unit = new codecompileunit (); unit. namespaces. add (nmspace); servicedescriptionimportwarnings warning = importer. import (nmspace, Unit); codedomprovider provider = codedomprovider. createprovider ("CSHARP"); compilerparameters parameter = new compilerparameters (); parameter. generateexecutable = false; parameter. referencedassemblies. ad D ("system. DLL "); parameter. referencedassemblies. add ("system. XML. DLL "); parameter. referencedassemblies. add ("system. web. services. DLL "); parameter. referencedassemblies. add ("system. data. DLL "); compilerresults result = provider. compileassemblyfromdom (parameter, Unit); // 5. use reflection to call WebService. Assembly ASM = result. compiledassembly; type T = ASM. getType (classname); // The parameter is the class name object o = Activator In the web service. createinstance (t); methodinfo method = T. getmethod (methodname); // parameter method name resulta = (Dataset) method. invoke (O, parameters);} catch (exception ex) {MessageBox. show (ex. tostring () ;}return resulta ;}}}

The above is the code in. CS.

        private void button3_Click(object sender, EventArgs e)        {            myWebServiceNameSpace.myWebServiceClass myWeb = new myWebServiceNameSpace.myWebServiceClass();            string url = "http://10.19.1.55/123/WebService1.asmx";            string className = "WebService1";            string methodName = "addition";            object[] parameters = new object[2];            parameters[0] = 1;            parameters[1] = 2;            double a = myWeb.resultString(url, className, methodName, parameters);            MessageBox.Show(a.ToString());                    }

The above is the code in the main program.

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.