Asp.net dynamic WebService cache improves efficiency

Source: Internet
Author: User

In the original project, most of the WebServices were used to obtain data through static connections. However, the disadvantage was that the WebService can be updated by the Department, which always had to be regenerated, if a project is deployed with WebService, the static reference must be modified and updated accordingly.

So I thought of using dynamic WebService, but the disadvantage is that the efficiency is low, and the response speed is really worrying.

 

There are many examples of dynamic WebService on the Internet, such:

// Http://blog.csdn.net/lzy_1515 <br/> public class link_webservice <br/> {<br/> Public link_webservice () <br/> {<br/> // todo: add the constructor logic here <br/> // <br/>}</P> <p> // <summary> <br/> // call WebService dynamically <br/> // </Summary> <br/> // <Param name = "url"> WebService address </param> <br/> // <param name = "methodname"> method name (Module name) </param> <br/> /// <Param name = "ARGs"> parameter list </param> <br/> /// <returns> Object </Returns> <br/> Public static object invokewebservice (string URL, string methodname, object [] ARGs) <br/>{< br/> return invokewebservice (URL, null, methodname, argS ); <br/>}< br/> // http://blog.csdn.net/lzy_1515 <br/> // <summary> <br/> // dynamic WebService call <br/> // </Summary> <br/> /// <Param name = "url"> WebService address </param> <br/> /// <Param name = "classname"> class name </param> <br/> // <Param name = "met Hodname "> method name (Module name) </param> <br/> /// <Param name = "ARGs"> parameter list </param> <br/> /// <returns> Object </returns> <br/> Public static object invokewebservice (string URL, string classname, string methodname, object [] ARGs) <br/>{< br/> string @ namespace = "servicebase. webService. dynamicwebload "; <br/> If (classname = NULL | classname =" ") <br/>{< br/> classname = getclassname (URL ); <br/>}< br/> // get the service Description Language (WSDL) <br/> WebClient WC = new WebClient (); <br/> stream = WC. openread (URL + "? WSDL "); <br/> servicedescription SD = servicedescription. read (Stream); <br/> servicedescriptionimporter SDI = new servicedescriptionimporter (); <br/> SDI. addservicedescription (SD, "", ""); <br/> codenamespace Cn = new codenamespace (@ namespace ); <br/> // generate client proxy Code <br/> codecompileunit CCU = new codecompileunit (); <br/> CCU. namespaces. add (CN); <br/> SDI. import (CN, CCU); <br/> csharpcodeprovider CSC = new csharpcodeprovider (); <br/> icodecompiler ICC = CSC. createcompiler (); <br/> // set the compiler parameters <br/> compilerparameters cplist = new compilerparameters (); <br/> cplist. generateexecutable = false; <br/> cplist. generateinmemory = true; <br/> cplist. referencedassemblies. add ("system. DLL "); <br/> cplist. referencedassemblies. add ("system. XML. DLL "); <br/> cplist. referencedassemblies. add ("system. web. services. DLL "); <br/> cplist. referencedassemblies. add ("system. data. DLL "); <br/> // compile the proxy class </P> <p> compilerresults Cr = ICC. compileassemblyfromdom (cplist, CCU); <br/> If (true = CR. errors. haserrors) <br/>{< br/> system. text. stringbuilder sb = new stringbuilder (); <br/> foreach (compilererror Ce IN Cr. errors) <br/>{< br/> Sb. append (CE. tostring (); <br/> Sb. append (system. environment. newline); <br/>}< br/> throw new exception (sb. tostring (); <br/>}</P> <p> // generate a proxy instance and call the method </P> <p> system. reflection. assembly = CR. compiledassembly; <br/> type T = assembly. getType (@ namespace + ". "+ classname, true, true); <br/> Object OBJ = activator. createinstance (t); <br/> system. reflection. methodinfo MI = T. getmethod (methodname); <br/> return mi. invoke (OBJ, argS); </P> <p >}</P> <p> Private Static string getclassname (string URL) <br/>{< br/> string [] parts = URL. split ('/'); <br/> string [] PPS = parts [parts. length-1]. split ('. '); <br/> return PPS [0]; <br/>}< br/> // http://blog.csdn.net/lzy_1515

This example can achieve dynamic connections, but the efficiency is not very high, because every request is re-generated, so it is slow.

 

Later, I thought about whether the cache will increase the speed. After improvement, I will generate it for the first time.

The response time is 0 seconds.

 

The modified code is as follows:

// Http://blog.csdn.net/lzy_1515 <br/> // string url = "http://url.asmx"; <br/> // string [] ARGs = new string [2]; <br/> // ARGs [0] = 0; <br/> // ARGs [1] = "12"; <br/> // object result = dynamicwebservices. invokewebservice (URL, "getcc", argS); <br/> // This. label_result.text = result. tostring (); </P> <p> public class dynamicwebservices <br/> {<br/> static sortedlist <string, type> _ typelist = new sortedlist <String, type> (); </P> <p> # region invokewebservice </P> <p> static string getcachekey (string URL, string classname) <br/>{< br/> return URL. tolower () + classname; <br/>}< br/> static type gettypefromcache (string URL, string classname) <br/>{< br/> string key = getcachekey (URL, classname); <br/> foreach (keyvaluepair <string, type> pair in _ typelist) <br/>{< br/> If (Key = pair. key) <br/>{< br/> Return pair. value; <br/>}</P> <p> return NULL; <br/>}< br/> static type gettypefromwebservice (string URL, string classname) <br/>{< br/> string @ namespace = "enterpriseserverbase. webService. dynamicwebcalling "; <br/> If (classname = NULL) | (classname = "")) <br/>{< br/> classname = getwsclassname (URL ); <br/>}< br/> // http://blog.csdn.net/lzy_1515 <br/> // get WSDL <br/> WebClient WC = New WebClient (); <br/> stream = WC. openread (URL + "? WSDL "); <br/> servicedescription SD = servicedescription. read (Stream); <br/> servicedescriptionimporter SDI = new servicedescriptionimporter (); <br/> SDI. addservicedescription (SD, "", ""); <br/> codenamespace Cn = new codenamespace (@ namespace ); </P> <p> // generate client proxy Code <br/> codecompileunit CCU = new codecompileunit (); <br/> CCU. namespaces. add (CN); <br/> SDI. import (CN, CCU); <br/> csharpcodeprovider CSC = new csharpcodeprovider (); <br/> icodecompiler ICC = CSC. createcompiler (); </P> <p> // set the compilation parameter <br/> compilerparameters cplist = new compilerparameters (); <br/> cplist. generateexecutable = false; <br/> cplist. generateinmemory = true; <br/> cplist. referencedassemblies. add ("system. DLL "); <br/> cplist. referencedassemblies. add ("system. XML. DLL "); <br/> cplist. referencedassemblies. add ("system. web. services. DLL "); <br/> cplist. referencedassemblies. add ("system. data. DLL "); </P> <p> // compile the proxy class <br/> compilerresults Cr = ICC. compileassemblyfromdom (cplist, CCU); <br/> If (true = CR. errors. haserrors) <br/>{< br/> system. text. stringbuilder sb = new system. text. stringbuilder (); <br/> foreach (system. codedom. compiler. compilererror Ce IN Cr. errors) <br/>{< br/> Sb. append (CE. tostring (); <br/> Sb. append (system. environment. newline); <br/>}< br/> throw new exception (sb. tostring (); <br/>}</P> <p> // generate a proxy instance and call the method <br/> system. reflection. assembly = CR. compiledassembly; <br/> type T = assembly. getType (@ namespace + ". "+ classname, true, true); </P> <p> return T; <br/>}</P> <p> // dynamically call the Web Service <br/> Public static object invokewebservice (string URL, string methodname, object [] ARGs) <br/>{< br/> return invokewebservice (URL, null, methodname, argS ); <br/>}</P> <p> Public static object invokewebservice (string URL, string classname, string methodname, object [] ARGs) <br/>{< br/> try <br/>{< br/> type T = gettypefromcache (URL, classname); <br/> If (t = NULL) <br/>{< br/> T = gettypefromwebservice (URL, classname ); </P> <p> // Add to buffer <br/> string key = getcachekey (URL, classname); <br/> _ typelist. add (Key, T); <br/>}</P> <p> Object OBJ = activator. createinstance (t); <br/> methodinfo MI = T. getmethod (methodname); </P> <p> return mi. invoke (OBJ, argS); <br/>}< br/> catch (exception ex) <br/>{< br/> throw new exception (ex. innerexception. message, new exception (ex. innerexception. stacktrace); <br/>}</P> <p> Private Static string getwsclassname (string wsurl) <br/>{< br/> string [] parts = wsurl. split ('/'); <br/> string [] PPS = parts [parts. length-1]. split ('. '); </P> <p> return PPS [0]; <br/>}< br/> # endregion </P> <p> // http://blog.csdn.net/lzy_1515 

 

This is my personal idea. If there is a better way, please talk more.

 

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.