ASP. NET create XML Web Service full contact (9)

Source: Internet
Author: User

Asynchronous Web Service (1)

To improve the performance of XML Web Service methods that impede the long-term running of threads, you should consider releasing them as Asynchronous XML Web Service methods. Implement an asynchronous XML Web Service method that allows the thread to execute other code when returning the thread pool. This allows you to add a limited number of threads in a thread pool, which improves overall performance and system scalability.

Generally, the XML Web Service method that calls the method for executing input/output operations is suitable for asynchronous implementation. Examples of such methods include communicating with other XML Web Services, accessing remote databases, executing network input/output, and reading and writing large files. These methods all spend a lot of time executing hardware-level operations, and keep the threads for executing XML Web Service method blocks. If the XML Web Service method is implemented asynchronously, the thread can be released to execute other code.

Regardless of whether an XML Web Service method is implemented asynchronously, the client can communicate with it asynchronously. Use the proxy class in the. NET client generated by the Web Service Description Language tool (WSDL. EXE) to implement asynchronous communication, even if the XML Web Service method is synchronous. The proxy class contains the Begin and End methods used for asynchronous communication with each XML Web Service method. Therefore, determining whether an XML Web Service method is asynchronous or synchronous depends on the performance.

Note: implementing an asynchronous XML Web Service method does not affect the HTTP connection between the client and the XML Web Service on the server. HTTP connections are neither closed nor Connection pooling.

Implement an asynchronous XML Web Service Method

Implementing an asynchronous XML Web Service method follows the NET Framework Asynchronous Design Pattern

A synchronous XML Web Service method is divided into two methods. Each of them has the same base name-one with the name starting with in and the other with the name starting with End.

The parameter table of the Begin method contains all the in and by reference parameters in the function of the method.

By reference parameters are listed as input parameters.

The second to last parameter must be AsyncCallback. The AsyncCallback parameter allows the client to provide a delegate that is called when the method call is complete. When an asynchronous XML Web Service method calls another Asynchronous Method, this parameter can be passed into the penultimate parameter of that method. The last parameter is an object. The object parameter allows a caller to provide status information to the method. When an asynchronous XML Web Service method calls another Asynchronous Method, this parameter can be passed into the last parameter of that method.

The returned value must be of the IAsyncResult type.

The following code example is a Begin method with a method function-specific String parameter.

[C #]
[WebMethod]
Public IAsyncResult BeginGetAuthorRoyalties (String Author,
AsyncCallback callback, object asyncState)
[Visual Basic]
<WebMethod ()> _
Public Function BeginGetAuthorRoyalties (ByVal Author As String ,_
ByVal callback As AsyncCallback, ByVal asyncState As Object )_
As IAsyncResult


The End method parameter table consists of an IAsyncResult type out and by reference parameter.

The returned value type is the same as that of a synchronized XML Web Service method.

By reference parameters are listed as output parameters.

The following sample code is an End method that returns an AuthorRoyalties user-defined mode.

[C #]
[WebMethod]
Public AuthorRoyalties EndGetAuthorRoyalties (IAsyncResult
AsyncResult)

[Visual Basic]
<WebMethod ()> _
Public Function EndGetAuthorRoyalties (ByVal asyncResult _
IAsyncResult) As AuthorRoyalties


The following code example is an asynchronous XML Web Service method that asynchronously communicates with another XML Web Service method.

[C #]
Using System;
Using System. Web. Services;
[WebService (Namespace = "http://www.contoso.com/")]
Public class MyService: WebService {
Public RemoteService remoteService;
Public MyService (){
// Create a new instance of proxy class
// The XML Web service to be called.
RemoteService = new RemoteService ();
}
// Define the Begin method.
[WebMethod]
Public IAsyncResult BeginGetAuthorRoyalties (String Author, AsyncCallback callback, object asyncState ){
// Begin asynchronous communictation with a different XML Web
// Service.
Return remoteService. BeginReturnedStronglyTypedDS (Author,
Callback, asyncState );
}
// Define the End method.
[WebMethod]
Public AuthorRoyalties EndGetAuthorRoyalties (IAsyncResultasyncResult ){
// Return the asynchronous result from the other XML Web service.
Return remoteService. EndReturnedStronglyTypedDS (asyncResult );
}
}

[Visual Basic]
Imports System. Web. Services
<WebService (Namespace: = "http://www.contoso.com/")> _
Public Class MyService
Inherits WebService
Public remoteService As RemoteService

Public Sub New ()
MyBase. New ()
Create a new instance of proxy class
The XML Web service to be called.
RemoteService = New RemoteService ()
End Sub

Define the Begin method.
<WebMethod ()> _
Public Function BeginGetAuthorRoyalties (ByVal Author As String ,_
ByVal callback As AsyncCallback, ByVal asyncState As Object )_
As IAsyncResult
Begin asynchronous communictation with a different XML Web
Service.
Return remoteService. BeginReturnedStronglyTypedDS (Author ,_
Callback, asyncState)
End Function
Define the End method.
<WebMethod ()> _
Public Function EndGetAuthorRoyalties (ByVal asyncResult _
IAsyncResult) As AuthorRoyalties
Return the asynchronous result from the other XML Web service.
Return remoteService. EndReturnedStronglyTypedDS (asyncResult)
End Function
End Class


The following code example shows how to connect an XML Web Service method to more than one asynchronous call that must be executed continuously. The BeginGetAuthorRoyalties method generates an asynchronous call to determine whether the input author name is valid, and sets an intermediate callback called AuthorRoyaltiesCallback to receive the result. If the author name is valid, the intermediate callback is called asynchronously to obtain the author's royalty.

[C #]
Using System. Web. Services;
Using System. Data;
Using System;
// This imports the proxy class for the XML Web services
// That the sample communicates.
Using AsyncWS. localhost;

Namespace AsyncWS
{
[WebService (Namespace = "http://www.contoso.com/")]
Public class MyService: System. Web. Services. WebService
{
Public RemoteService remoteService;
Public MyService ()
{
Remo teService = new RemoteService ();
}

[WebMethod]
Public IAsyncResult BeginGetAuthorRoyalties (String Author,
AsyncCallback callback, Object asyncState)
{
// Saves the current state for the call that gets the authors
// Royalties.
AsyncStateChain state = new AsyncStateChain ();
State. originalState = asyncState;
State. Author = Author;
State. originalCallback = callback;

// Creates an intermediary callback.
AsyncCallback chainedCallback = new
AsyncCallback (AuthorRoyaltiesCallback );
Return remoteService. BeginGetAuthors (chainedCallback, state );
}
// Intermediate method to handle chaining
// Asynchronous CILS.
Public void AuthorRoyaltiesCallback (IAsyncResult ar)
{
AsyncStateChain state = (AsyncStateChain) ar. AsyncState;
RemoteService rs = new RemoteService ();

// Gets the result from the call to GetAuthors.
Authors allAuthors = rs. EndGetAuthors (ar );

Boolean found = false;
// Verifies that the requested author is valid.
Int I = 0;
DataRow row;
While (I <allAuthors. authors. Rows. Count &&! Found)
{
Row = allAuthors. authors. Rows [I];
If (row ["au_lname"]. ToString () = state. Author)
{
Found = true;
}
I ++;
}
If (found)
{
AsyncCallback cb = state. originalCallback;
// Callthe second XML Web ser

Related Article

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.