Mattpowell describes how to create a high-performance Microsoft ASP.NET web service on the server side to create high-performanc

zhaozj2021-02-16  101

Summary: Matt Powell describes how to create high-performance Microsoft ASP.NET Web services on the server side to create high-performance Microsoft ASP.NET Web services. Introduction In the third column (English) in September, I talked about the issue of using Microsoft® .NET Framework's client function to call the Web service through HTTP. This method of calling a web service is very useful. You do not have to lock your app or generate too many background threads when you use. Now let's take a look at the asynchronous web ways of the server side to provide similar functions. Asynchronous Web methods have high performance similar to HSAPI extensions, but do not need to write code to manage your own thread pool, and all the advantages running in managed code. First we consider the regular synchronization Microsoft® ASP.NET Web method. When you return from the synchronization web method, the response to this method will be sent. If a longer time is required to complete the request, the process of processing the request will always occupy until the method call ends. Unfortunately, most longer calls are caused by a longer database query or an event such as a call to another Web service. For example, if you call the database, the current thread will wait for the call to complete. Threads can be done, just waiting until the return of the query is heard. Similar problems will occur when the thread waits to complete the call to the TCP socket or the backend web service. Let the thread are in a waiting state, especially if the server's operating pressure is large. Waiting for threads do not work any effective job, such as providing services for other requests. We need to find a way to start longer background processes on the server, while returning the current thread to the ASP.NET process pool. Then, when a longer backend process is completed, we call a callback function to end the process of the request, and notify the ASP.NET request in some way. In fact, this feature can be provided by ASP.NET to use asynchronous web methods. Working principle of asynchronous Web methods When you write a typical ASP.NET Web service using the web method, Microsoft? Visual Studio? Net just compiles your code to create an assembly; when receiving the request for its web method, Call the assembly. The assembly itself does not know anything about SOAP. Therefore, when your application starts for the first time, the ASMX handler must reflect your assembly to determine which Web methods are provided. For conventional synchronous requests, these operations are simple: find which methods have associated webMethod properties, based on the SOAPAction HTTP header to set the logic of calling the correct way. For asynchronous requests, during the reflection, the ASMX handler finds a Web method with some signature and identifies the signature as an asynchronous. This handler will find ways to meet the following rules: Beginxxx and Endxxx Web methods, where xxx is any string, indicating the name of the method to provide. The Beginxxx function returns an IASYNCRESULT interface and accepts asyncCallback and an object, as its last two input parameters. The ENDXXX function accepts an IASYNCRESULT interface as its unique parameter. Both methods must be identified using the WebMethod property. If the ASMX handler finds that two methods meet all of the above conditions, the method XXX is provided as a conventional web method in its WSDL.

This method will accept the parameters defined before the AsyncCallback parameter in Beginxxx's signature as input and return the content returned by the ENDXXX function. Therefore, if a web method has the following synchronization declaration: [WebMethod] public string lengthprocedure (int MilliseConds) {...}

The asynchronous declaration will be:

[WebMethod] Public IASYNCRESULT BeginlengthyProcedure (INT MilliseConds, AsyncCallback CB, Object S) {...} [WebMethod] public string endplayprocedure (IasyncResult Call) {...}

The WSDL of each method is the same. After the ASMX handler reflects the assembly and detects an asynchronous web method, it must handle requests to the method in a way different from the processing synchronization request. It will call the Beginxxx method, not a simple method. It sequences the incoming request restore to the parameters to be passed to the function (like the processing synchronous request); however, it also passes the pointer to an internal callback function (as an additional ASYNCALLBACK parameter for the Beginxxx method). This approach is similar to the asynchronous programming mode of the Web service client application in the .NET Framework. If the client supports the asynchronous web service call, you can release the usage thread for the client computer; if the server supports the asynchronous web service call, you can release the threads occupied by the server computer. But there are two key differences here. First, it is not called the server code to call the Beginxxx and Endxxx functions, but is called by the ASMX handler. Second, you have to write code for the Beginxxx and Endxxx function, and cannot use the code generated by the Add Web Reference wizard by WSDL.exe or Visual Studio .NET. However, the result is the same, that is, release the thread to enable it to perform other processes. After the ASMX handler calls the server's Beginxxx function, the thread returns the thread to the process thread pool to process any other requests received. However, the requestible HTTPCONTEXT cannot be released. The ASMX handler will wait until it passes the callback function of the Beginxxx function being called, it ends the processing request. Once the callback function is called, the ASMX handler will call the ENDXXX function to make your web method to complete any of the processed to be executed, and can be serially serized to the return data in the SOAP response. The ENDXXX function returns a response, only the requested HTTPCONText is released.

Simple asynchronous web method For an exemplary web method, I started from a simple synchronization web method called Length, and its code is as follows. Then we look at how to make the same task asynchronously. LengthyProcedure only occupies a given millisecond.

[WebService] public class SyncWebService: System.Web.Services.WebService {[WebMethod] public string LengthyProcedure (int milliseconds) {System.Threading.Thread.Sleep (milliseconds); return "success";}} Now we will convert LengthyProcedure Asynchronous web method. We must create the BeginLengthyProcedure function and endlengthyprocedure functions as described above. Keep in mind that our BeginLengthyProcedure call needs to return an IASYNCRESULT interface. Here, I intend to use a delegate and the BeginInvoke method on the commission, let our BeginLengthyProcedure call for asynchronous method calls. The callback function passing to the BeginLengthProcedure will be passed to the BeginInvoke method on the commission, and the IASYNCRESULT returned from BeGinInvoke will be returned by the BeginLength Procedure method. The endlengthyprocedure method will be called when the commission is completed. We will call the EndInvoke method on the commission to pass it into the IASYNCRESULT and use it as an input called endlengthyprocedure. The returned string will be a string returned from the web method. Below is its code:

[WebService] public class AsyncWebService: System.Web.Services.WebService {public delegate string LengthyProcedureAsyncStub (int milliseconds); public string LengthyProcedure (int milliseconds) {System.Threading.Thread.Sleep (milliseconds); return "success";} public class MyState {public object previousState; public LengthyProcedureAsyncStub asyncStub;} [System.Web.Services.WebMethod] public IAsyncResult BeginLengthyProcedure (int milliseconds, AsyncCallback cb, object s) {LengthyProcedureAsyncStub stub = new LengthyProcedureAsyncStub (LengthyProcedure); MyState ms = new MyState ( ); ms.previousState = s; ms.asyncStub = stub; return stub.BeginInvoke (milliseconds, cb, ms);} [System.Web.Services.WebMethod] public string EndLengthyProcedure (IAsyncResult call) {MyState ms = (MyState) Call.asyncState; return ms.asyncstub.endinvoke (call);}}

When to use asynchronous web methods to determine if it is suitable for use in your application, there are several issues to consider it. First, the called Beginxxx function must return an IASYNCRESULT interface. IASYNCRESULT is returned from multiple asynchronous I / O operations, including access to data streams, performing Microsoft? Windows? Socket call, execute file I / O, interact with other hardware devices, call asynchronous methods, of course, also include call Other Web services. You can get an IASYNCRESULT from these asynchronous operations to return it from the Beginxxx function. You can also create your own classes to implement an IASYNCRESULT interface, but you may need to pack an I / O operation mentioned in some way. For most asynchronous operations mentioned earlier, use asynchronous web methods Packaging the back-end asynchronous calls make sense, so that the web service code is more efficient. However, except for the use of delegate for asynchronous methods. The entrustment will cause asynchronous methods to call a thread in the process thread pool. Unfortunately, these threads are also used when providing services for entry requests. So unlike the call to the hardware or network resource, the invoking method of using the delegated asynchronous method will still take up one of the process threads. You can also occupy the original thread and run your web method. The following example shows an asynchronous web method that calls the backend web service. It has used the webMethod property to identify the BegingeTage and Endgetage methods for asynchronous operation. This asynchronous web method calls the backend web method called UserInfoQuery to get the information it needs to return. The call to UserInfoQuery is executed asynchronously and is passed to the AsyncCallback function, and the latter is passed to the BegingeTage method. This will cause the internal callback function when the rear end request is completed. Then, the callback function will call the Endgetage method to complete the request. The code in this example is much simpler than the code in the previous example, and has another advantage, that is, the rear end processing is started in the same thread pool as the intermediate layer web method request provides the service providing service. [WebService] public class getMYINFO: system.Web.Services.Webservice {[WebMethod] Public IASYNCRESULT BEGINGETAGE (ASYNCALLBACK CB, OBJECT State) {// Call asynchronous web service call. localhost.UserInfoQuery proxy = new localhost.UserInfoQuery (); return proxy.BeginGetUserInfo ( "User Name", cb, proxy);} [WebMethod] public int EndGetAge (IAsyncResult res) {localhost.UserInfoQuery proxy = (localhost.UserInfoQuery) res .AsyncState; int Age = proxy.endgetuserInfo (res) .age; // This other // is handled in this result of the web service. Return Age;}

One of the most common I / O operation types that happen in a web method is the call to the SQL database. Unfortunately, the current Microsoft? ADO.NET has not yet defined a good asynchronous call mechanism; but only the SQL call package to the asynchronous commissioning is not helpful to improve efficiency. Although sometimes you can choose the cache result, you should also consider using Microsoft SQL Server 2000 Web Services Toolkit to publish your database as a web service. This way you can use the support in .NET Framework, asynchronously call the web service to query or update the database. When you use the web service to access SQL, you need to pay attention to numerous backend resources. If you use TCP sockets to communicate with UNIX, or access to other available SQL platforms through a dedicated database driver, even with resources accessed using DCOM, you can consider using numerous web service kits to use many of these resources. Published as a web service. One of the advantages of using this method is that you can use the advantages of the client web service structure, such as use .NET Framework's asynchronous web service call. This way you will be free to get asynchronous calls, and your client access mechanism works highly efficiently with asynchronous web methods. Using asynchronous web methods aggregated data now, many web services access multiple resources of the backend and the web service aggregation information for the front end. Although multiple backend resources will increase the complexity of the asynchronous web method model, it will eventually improve efficiency. Suppose your Web method calls two backend Web services: Services A and Services B. From your beginxxx function, you can call the service A and service B asynchronously. You should pass your own callback function to each asynchronous call. After receiving the result from the service A and the service B, in order to trigger the completion of the web method, the callback function you provide verify that all requests have been completed, all processing on the returned data, and then call to the Beginxxx function. Callback. This will trigger the call to the endxxx function, and the return will result in the completion of the asynchronous Web method. Small junction asynchronous web methods provide a valid mechanism in the ASP.NET Web service, which can call backend services without causing occupation of valuable threads in the process thread pool. By combining asynchronous requests for backend resources, the server can use its own web method to maximize the number of requests at the same time. You should consider using this method to develop high-performance Web service applications. http://msd2d.com/newsletter_tip.aspx?section=dotNet&ID=887A74F8-E8DC-4B67-8072-BC45DA89FACB

Definition AsyncCallback commission

Private asyncCallback CallbackHandler;

Initialization must be in the form of the form (best constructor)

CallbackHandler = New AsyncCallback; MyCallback;

(The instance of yourself is because it is defined in this way.

Private asyncCallback CallbackHandler = New asyncCallback (MyCallback); compiled nothing to find a long reason

But now I still don't understand the principle of this problem.

Second use the agent's BeginInvoke method to call the agent and bind a callback function

TargetHandler.BeginInvoke (CallbackHandler, Nothing)

The return value of the agent here will be delivered to AsyncCallback to the AsyncCallback to the AsyncCallBack. CallbackHandler

The definition of MyCallback must be as follows

Private Void MyCallback (IASYNCRESULT IAR)

Get the return worthpane method = getCustomersHandler.Endinvoke (IAR); functions MyCallback handles what you need to do

In short, asynchronous call is achieved by the FCL commissioning asyncCallback.

转载请注明原文地址:https://www.9cbs.com/read-11153.html

New Post(0)