Why is it slow to call WebService for the first time?

Source: Internet
Author: User

We often complain that WebService calling is slow for the first time. The common practice is to call all WebServices one by one in the background when the program is started, and some people use multithreading to solve this problem. In fact, we only saw the phenomenon of the problem and the solution to the problem, but did not come into contact with the essence of the problem. After I decompile the. NET class library and gradually look for it, I should say that I have found the fundamental solution to this problem. We all know that WebService transmits messages through the SOAP protocol. All messages are XML, and both the client and server are objects used, there must be a conversion between XML and objects, which is the culprit of slow conversion. Next, I will analyze it gradually. I hope it will help you. Please read it with patience.

1. When adding a web reference, WebService has a proxy on the client, as shown below:

[System. codedom. compiler. generatedcodeattribute ("system. web. services "," 2.0.50727.42 ")] [system. diagnostics. debuggerstepthroughattribute ()] [system. componentmodel. designercategoryattribute ("Code")] [system. web. services. webservicebindingattribute (name = "webservice1soap", namespace = "http://tempuri.org/") public partial class webservice1: system. web. services. protocols. the soaphttpclientprotocol client calls webse. Rvivce is called through this proxy class. 2. when the WebService method is called, the communication between the client and the server is XML, so there is a serialization and deserialization process between the agent class and XML. the client calls WebService as follows. A) The client calls the Hello world method of the proxy class string STR = (New service2.webservice1 ()). helloworld (); B) the proxy class calls the invoke method of the base class soaphttpclientprotocal [system. web. services. protocols. soapdocumentmethodattrispace ("http://tempuri.org/HelloWorld0766", requestnamespace = "http://tempuri.org/", responsenamespace = "http://tempuri.org/", use = system. web. servic Es. description. soapbindinguse. literal, parameterstyle = system. web. services. protocols. soapparameterstyle. wrapped)] Public String helloworld () {object [] Results = This. invoke ("helloworld", new object [0]); Return (string) (results [0]);} c) soaphttpclientprotocal to serialize SOAP Headers and methods, the input parameters and return values are xmlserializer. The input parameters must be serialized and the return values must be deserialized. Protected object [] invoke (string methodname, object [] parameters ){... Try {message1.setstream (stream1); this. serialize (message1); // Note 1 }... Response1 = This. getwebresponse (request1); stream stream2 = NULL; try {stream2 = response1.getresponsestream (); objarray1 = This. readresponse (message1, response1, stream2, false); // Note 2} Note 1: This. in serialize, the code for parameter serialization is as follows: method1.parameterserializer. serialize (writer1, message. getparametervalues (), null, flag1? Text2: NULL); Note 2: This. readresponse has the following message. setparametervalues (object []) method1.returnserializer. deserialize (reader1, flag1? Text1: NULL); d) xmlserializer caches the temporary assembly, which is used for serialization and deserialization, if the cache does not call tempassembly, a static cache will be generated (the culprit of every slow call): Private Static tempassemblycache; obtain the assembly in the cache: This. tempassembly = xmlserializer. cache [defaultnamespace, type]; load if the cache does not exist: Assembly Assembly1 = tempassembly. loadgeneratedassembly (type, defaultnamespace, out implementation1); generate a temporary file if it is not loaded (it will generate a temporary file and compile it slowly): This. tempassembly = new tempassembly (new x Mlmapping [] {This. mapping}, Assembly1, implementation1); e) the tempassemlby class is responsible for loading and generating temporary assembly. In the loadgeneratedassemlby method, There is a logic, that is, the serialization class is loaded by default, the naming rules for this class are as follows: internal static string gettempassemblyname (assemblyname parent, string NS) {return (parent. name + ". xmlserializers "+ (NS = NULL) | (NS. length = 0 ))? "":(". "+ NS. gethashcode ();} at the same time, if the load fails, the appdomain will be triggered. currentdomain. assemblyresolve event 4. conclusion 1) the serialization of WebService is to call xmlserializer. 2) the WebService is slow because the serialization class is slow. The so-called temporary files are the intermediate code of xmlserializer. You can add the following configuration to the config file. The temporary serialized file will not be deleted. The winform program is *. EXE. config, and Asp.net is Web. config. <Configuration>
<System. Diagnostics>
<Switches>
<Add name = "xmlserialization. Compilation" value = "4"/>
</Switches>
</System. Diagnostics>
</Configuration> temporary files are stored in C:/Documents and Settings/rag/Local Settings/temp. Note that serialized DLL files cannot be reused because the names are random, the re-opened process will be re-generated. 3) if you define a serialization class, you can skip the step for generating temporary serialization, which greatly increases the loading speed for the first time. That is to say, if there is an assembly name + ". the serialization class of xmlserializers does not dynamically generate the serialization assembly. 4) You can add [system. XML. serialization. xmlserializerassemblyattribute (assemblyname = "testperformance. xmlserializers ")] specifies the XML serialization class, which can be generated by a tool. However, according to the loadgeneratedassemlby code of tempassemlby, this attribute can be left empty, as long as you have a serialization class with the same name as the gettempassemblyname returned value. 5) The appdomain. currentdomain. assemblyresolve event is triggered Based on loading failure. serialization classes can be dynamically generated after loading failure, as shown below. Http://support.microsoft.com/kb/872800/zh-cn. refer to this kb private void form1_load (Object sender, eventargs e) {appdomain. currentdomain. assemblyresolve + = new resolveeventhandler (myresolveeventhandler);} static Assembly myresolveeventhandler (Object sender, resolveeventargs ARGs) {Assembly A = NULL; string [] arr = ARGs. name. split (New String [] {". "}, stringsplitoptions. none); If (ARGs. name. indexof ("x Mlserializers ")> = 0) {If (! System. io. file. exists (ARGs. name + ". DLL ") pregenns. pregen. generate (New String [] {arr [0]}); string sserializersdll = args. name + ". DLL "; string smartdeploymenthostlocation =" "; A = assembly. loadfrom (smartdeploymenthostlocation + sserializersdll);} return a;} 6) vs2005 uses release compilation to generate assemblyname + "xmlserializer. the serialization file of DLL can be deployed along with the client. This method is not the same as that of DLL 5 and can be selected based on the actual situation. 5. This is the first time that a serialization class is dynamically generated when WebService is called. 6 is generated when the software is released and deployed on the client.

 

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.