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

Source: Internet
Author: User

Asynchronous Web Service (2)

Asynchronous Communication with XML Web Services

Asynchronous Communication with an XML Web Service follows the asynchronous design pattern used by other parts of Microsoft. NET Framework. However, before you get the details, it is important to note that an XML Web Service does not need to be specially written to process asynchronous requests for asynchronous calls. You can use wsdl.exe to automatically create a method for Asynchronously calling the XML Web Service Method for the proxy class created for your client. This is true even if only one XML Web Service method is used for synchronization.

. NET Framework Asynchronous Method call Design Mode

The design pattern used to call asynchronous methods, especially for. NET Framework. There are two asynchronous methods for each synchronous method. For each synchronous method, there is a Begin Asynchronous Method and an End Asynchronous Method. The Begin method is called by the client to start the method call. That is to say, the client instructs this method to start processing method calls, but returns immediately. The End method is called by the client to obtain the processing result of the XML Web Service method call.

How does a client know when to call the End method ?. NET Framework defines two methods for the client to determine its time. The first is to send a callback function to the Begin method, which is called when the method has been processed. The second method is to use a WaitHandle class method to cause the client to wait for the method to complete. When a client implements the second method and calls the Begin method, the returned value is not the data type specified by the XML Web Service method, but a type that implements the IAsyncResult interface. The IAsyncResult Interface contains the AsyncWaitHandle attribute of the WaitHandle type, which supports waiting for the synchronization object to be changed to methods with the WaitHandle. WaitOne, WaitAny, and WaitAll tags. When a synchronization object is marked, it indicates that the thread waiting for a specific resource can access the resource. If an XML Web Service client uses the wait method to call only one XML Web Service method asynchronously, it can call WaitOne to wait for the XML Web Service method to complete processing.

It is important to note that no matter which of the two methods the client chooses to communicate asynchronously with the XML Web Service, the SOAP message sending and receiving are consistent with the synchronous communication. That is to say, only one SOAP request and SOAP response are sent and received over the network. The proxy class processes the SOAP response by using a different thread rather than the thread used by the client to call the in method. Therefore, the client can continue to execute other work on the thread, while the proxy class handles the receipt and operation of SOAP responses.

Implement an XML Web Service client that generates asynchronous method calls

The architecture used to generate an asynchronous call from an XML Web Service client created using ASP. NET to an XML Web Service is embedded in. NET frameworkand a proxy class constructed by wsdl.exe. The design pattern used for asynchronous calls is defined by. NET Framework. The proxy class provides a mechanism for asynchronous communication with an XML Web Service. When a proxy class for XML Web Services is constructed using wsdl.exe, three methods are created respectively for the common XML Web Service methods in XML Web Services. The following table describes the three methods.

Description of the method name in the proxy class

<NameOfWebServiceMethod> synchronously sends messages for the XML Web Service method named <NameOfWebServiceMethod>.

Begin <NameOfWebServiceMethod> starts asynchronous message communication with the XML Web Service method named <NameOfWebServiceMethod>.

End <NameOfWebServiceMethod> ends asynchronous message communication with the XML Web Service method named <NameOfWebServiceMethod> and obtains the completed message from the XML Web Service method.

The following code example is an XML Web Service method, which may take a relatively long time to complete processing. Therefore, when you set your XML Web Service client to asynchronously call the XML Web Service method, it is a good example.

[C #]
<% @ WebService Language = "C #" Class = "PrimeFactorizer" %>

Using System;
Using System. Collections;
Using System. Web. Services;

Class PrimeFactorizer {

[WebMethod]
Public long [] Factorize (long factorizableNum ){
ArrayList outList = new ArrayList ();
Long I = 0;
Int j;
Try {
Long Check = factorizableNum;

// Go through every possible integer
// Factor between 2 and factorizableNum/2.
// Thus, for 21, check between 2 and 10.
For (I = 2; I <(factorizableNum/2); I ++ ){
While (Check % I = 0 ){
OutList. Add (I );
Check = (Check/I );
}
}
// Double-check to see how many prime factors have been added.
// If none, add 1 and the number.
J = outList. Count;
If (j = 0 ){
OutList. Add (1 );
OutList. Add (factorizableNum );
}
J = outList. Count;

// Return the results and
// Create an array to hold them.
Long [] primeFactor = new long [j];
For (j = 0; j <outList. Count; j ++ ){
// Pass the values one by one, making sure
// To convert them to type ulong.
PrimeFactor [j] = Convert. ToInt64 (outList [j]);
}
Return primeFactor;
}
Catch (Exception ){
Return null;
}
}
}

[Visual Basic]
<% @ WebService Class = "PrimeFactorizer" Language = "VB" %>
Imports System
Imports System. Collections
Imports System. Web. Services

Public Class PrimeFactorizer
<WebMethod> _
Public Function Factorize (factorizableNum As Long) As Long ()
Dim outList As New ArrayList ()
Dim I As Long = 0
Dim j As Integer
Try
Dim Check As Long = factorizableNum

Go through every possible integer
Factor between 2 and factorizableNum/2.
Thus, for 21, check between 2 and 10.
For I = 2 To CLng (factorizableNum/2)-1
While Check Mod I = 0
OutList. Add (I)
Check = CLng (Check/I)
End While
Next I
Double-check to see how many prime factors have been added.
If none, add 1 and the number.
J = outList. Count
If j = 0 Then
OutList. Add (1)
OutList. Add (factorizableNum)
End If
J = outList. Count

Return the results and
Create an array to hold them.
Dim primeFactor (j-1) As Long
For j = 0 To outList. Count-1
Pass the values one by one, making sure
To convert them to type ulong.
PrimeFactor (j) = CLng (outList (j ))
Next j
Return primeFactor
Catch
Return Nothing
End Try
End Function
End Class


The code example below is a part of the proxy class generated by wsdl.exe, used for the preceding XML Web Service method. Note that the BeginFactorize and EndFactorize methods are used for asynchronous communication with the Factorize XML Web Service method.

Public class PrimeFactorizer: System. Web. Services. Protocols. SoapHttpClientProtocol {

Public long [] Factorize (long factorizableNum ){
Object [] results = this. Invoke ("Factorize", new object [] {factorizableNum });
Return (long []) (results [0]);
}

Public System. IAsyncResult BeginFactorize (long factorizableNum, System. AsyncCallback callback, object asyncState ){
Return this. BeginInvoke ("Factorize", new object [] {
FactorizableNum}, callback, asyncState );
}

Public long [] EndFactorize (System. IAsyncResult asyncResult ){
Object [] results = this. EndInvoke (asyncResult );
Return (long []) (results [0]);
}
}


Two methods are used to asynchronously communicate with the XML Web Service method. The following code example illustrates how to asynchronously communicate with an XML Web Service method and use a callback function to obtain the results of the XML Web Service method.

[C #]
Using System;
Using System. Runtime. Remoting. Messaging;
Using MyFactorize;

Class TestCallback
{
Public static void Main (){
Long factorizableNum = 12345;
PrimeFactorizer pf = new PrimeFactorizer ();

// Instantiate an AsyncCallback delegate to use as a parameter
// In the BeginFactorize method.
AsyncCallback cb = new AsyncCallback (TestCallback. FactorizeCallback );

// Begin the Async call to Factorize, passing in our
// AsyncCalback delegate and a reference
// To our instance of PrimeFactorizer.
IAsyncResult ar = pf. BeginFactorize (factorizableNum, cb, pf );

// Keep track of the time it takes to complete the async cal

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.