Server-side asynchronous Web method (3)

zhaozj2021-02-16  59

When to use asynchronous web methods

There are several issues that need to be considered when it is appropriate to use asynchronous web methods in your application. 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 calls, executing files I / O, interact with other hardware devices, call asynchronous methods, of course, also include calling 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 (AsyncCallback 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. degasyncState;

INT Age = proxy.endgetuserinfo (res) .age

/ / Other results for web services here

// deal with.

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 helps the SQL call to the asynchronous commissioning call 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.

Use asynchronous web methods to aggregate data

Now, many web services access multiple resources in the backend and the web service aggregation information. 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.

summary

Asynchronous web methods provide a valid mechanism in the ASP.NET Web service, you 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.

at your service

Matt Powell is a member of the MSDN Architectural Samples group, helping to develop the latest SOAP Toolkit 1.0. Matt also combined with others, "Running Microsoft Internet Information Server" published by Microsoft Press, and wrote a lot of articles for a variety of publications. He has a happy family, that is his daily place.

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

New Post(0)