Study/WebService2008. 2. 20. 01:31

Download source and demo project - 10.3 Kb

Sample Image

Introduction

The purpose of this article is to demonstrate how to expose a method from an ASP.NET Web Service or WinFX Service that uses the asynchronous pattern internally, to increase the scalability of the service. Specifically, how to prevent the threads that are being used to service requests from blocking while background I/O operations take place.

Please note, this article does not deal with applications simply making asynchronous calls to Web Services, as this has been amply covered already. However, if you are familiar with that, you'll notice plenty of similarities between the client-side and the server-side implementation.

Contents

Requirements

I've written this using Visual Studio 2005, .NET 2.0, and the WinFX Feb CTP, on a Windows XP machine. The ASP.NET Web Service code should run on .NET 1.0 and .NET 1.1, although I haven't tested this.

Background

Before we start, let's just recap what normally happens when a consumer makes a request against an ASP.NET Web Service. The consumer issues a SOAP request to IIS which will hand the request over to the ASMX handler. If it hasn't already done so, the ASMX handler will build/reflect over the assemblies in the bin folder, looking for the methods decorated with the WebMethod attribute. The ASMX handler can then match a SOAP request with a specific method, and hence deserialises the request into the input parameters of that method. When that method returns, the thread running it is returned to the pool, and the associated HttpContext is released.

Writing the code - ASP.NET Web Service

Before we start writing any code to exploit the asynchronous features of the hosting platform, we need to create a synchronous method that performs some kind of I/O. I've chosen file-based I/O, but the principles apply equally to network I/O, such as a call to a backend ASP.NET Web Service or database. Below is the code for a method that opens a large file and counts the number of zero bytes inside it. I've called it SyncWork. The large file I've chosen is the TrueType Arial font because it's installed on most XP machines and weighs in at 22 MB, but any large file will do.

[WebMethod]
public Int32 SyncWork() 
{
    FileStream fs = 
      new FileStream(@"c:\windows\fonts\arialuni.ttf", 
      FileMode.Open);

    try
    {
        Byte[] buffer = new Byte[fs.Length];
        fs.Read(buffer, 0, (Int32) fs.Length);

        Int32 count = 0;
        foreach (Byte b in buffer)
        {
            if (b == 0) count++;
        }
        return count;
    }
    finally
    {
        fs.Close();
    }
}

Looking at SyncWork, we can identify that the call to FileStream.Read is going to involve reading some data from the hard disk. This is going to take some time to complete, and while we're waiting, our thread will be blocked and hence not performing any useful work. It would be better if this thread could be returned to the thread pool managed by our host (in this case IIS, or specifically the ASMX handler) so that more requests could be served. This is a scenario where implementing server-side asynchronous methods can help to make our service more scalable.

I'm going to create a new method on the service called AsyncWork which will perform the same function for the consumer as SyncWork but it will behave asynchronously. Although AsyncWork will appear to the consumer as a single method, we have to write two separate methods, named BeginAsyncWork and EndAsyncWork, respectively. The required signatures for these methods are shown below:

[WebMethod]
public IAsyncResult BeginAsyncWork(AsyncCallback callback, Object state)
{
    ...
}

[WebMethod]
public Int32 EndAsyncWork(IAsyncResult ar)
{
    ...
}

Notice that the BeginAsyncWork method has two additional input parameters, appended to the end of the parameter list. In this case, they are the only two input parameters. The first parameter refers to a callback method that should be called when the background I/O work is complete. The state object is anything that the host would like us to associate with this asynchronous work so that it can identify it later. In my experience, this has always been null, but there's nothing to suggest it won't be used in the future so it's important to treat it properly. The BeginAsyncWork method must return an IAsyncResult. This same IAsyncResult will be passed as the sole parameter to the EndAsyncWork method when the background I/O operation is complete. The EndAsyncWork method will have the same return type as the SyncWork method, in this case an Int32.

Crucially, the EndAsyncWork method may be called on a different instance of our Service class than the call to BeginAsyncWork. This is because the thread on which the BeginAsyncWork code ran will be returned to the thread pool as soon as it is finished so that it can be used to service another request - the whole point of this exercise in fact! A thread won't be selected to run the EndAsyncWork code until the ASMX handler receives notification that the background I/O operation is complete. All of this grabbing and releasing of threads means that we can't share information between the BeginAsyncWork and EndAsyncWork methods using member variables on our Service class. We'll need to create a separate class to hold state information, and give it to the ASMX handler to look after for us. Since the ASMX handler is going to get an object that implements IAsyncResult from BeginAsyncWork, and it's going to pass this object to EndAsyncWork, it makes sense for our state to be contained in that object. So in short, we'll store our state information in the object that implements IAsyncResult. The sequence diagram below shows the messages that are sent between the various objects during the process:

The consumer issues an AsyncWork request, which causes the ASMX handler to call BeginAsyncWork, which creates a state object and starts the background I/O operation via the FileStream's BeginRead method. The service1 object is finished so it can be garbage collected and its thread returned to the pool. When the background I/O operation finishes, it calls the ReadOperationComplete method on the serviceState object which in turn invokes the ASMX handler's callback method. The ASMX handler responds to this notification by calling the EndAysncWork method which finishes the job. Notice in the diagram above that the two Service classes, service1 and service2, have short life-times with respect to the whole operation (as shown in yellow).

A state class needs to maintain several pieces of information. It must know about the callback and state that was passed into the BeginAsyncWork method. It must reference the variables shared between the BeginAsyncWork and EndAsyncWork methods, which in this case boils down to a FileStream, an IAsyncResult returned from the FileStream, and a Byte array. It must implement the IAsyncResult interface so that it can be returned to the ASMX handler. Finally, it must provide a method which the background operation (FileStream, in this case) can call to tell us that it's finished. The ServiceState class below satisfies these requirements:

public class ServiceState : IAsyncResult
{
    // callback and state object for the host
    public AsyncCallback HostCallback;
    public Object HostState;

    // information needed for the background I/O call
    public Byte[] Buffer;
    public FileStream Stream;
    public IAsyncResult ReadOperationAsyncResult;

    // implementation of the IAsyncResult interface for the host
    public object AsyncState { get { return HostState; } }
    public WaitHandle AsyncWaitHandle { get { 
           return ReadOperationAsyncResult.AsyncWaitHandle; } }
    public Boolean CompletedSynchronously { get { 
           return ReadOperationAsyncResult.CompletedSynchronously; } }
    public Boolean IsCompleted { get { 
           return ReadOperationAsyncResult.IsCompleted; } }

    // our callback method that will be
    // notified when the background I/O is done
    public void ReadOperationComplete(IAsyncResult ar)
    {
        ServiceState serviceState = ar.AsyncState as ServiceState;
        serviceState.HostCallback(serviceState);
    }
}

Notice that this implementation of the IAsyncResult interface is achieved by simply wrapping most of the properties of the ReadOperationAsyncResult member. This member is provided by the FileStream object when calling BeginRead, and provides information about that specific background operation. Since this is our only background operation, we can just wrap it. If we had multiple background operations, we would need to implement IAsyncResult ourselves with properties that reflected the complete picture. For example, if only one of two background operations had completed, then the IsCompleted property would need to return false. The AsyncState property returns the HostState object. This is because if the host was to access the AsyncState property, then it would expect to get the same object that it provided in the initial call to BeginAsyncWork.

So, let's actually write the BeginAsyncWork method. We begin by constructing the object that will contain our state information, a ServiceState object in this case. I've stored the callback and the state. I've then created the FileStream, and subsequently the Byte array, that I need for the operation, and attached these to the state also. I then start the background I/O operation by providing the input parameters for both the read operations (a Byte array, start point, number of bytes) and for the asynchronous facility (a callback and my state object). My state object, serviceState, is then returned as this implements the IAsyncResult interface and allows the host to keep track of the background operation.

[WebMethod]
public IAsyncResult BeginAsyncWork(AsyncCallback callback, Object state)
{
    ServiceState serviceState = new ServiceState();
    serviceState.HostCallback = callback;
    serviceState.HostState = state;
    serviceState.Stream = new 
      FileStream(@"c:\windows\fonts\arialuni.ttf", FileMode.Open);

    try
    {
        serviceState.Buffer = new Byte[serviceState.Stream.Length];
        serviceState.ReadOperationAsyncResult = 
             serviceState.Stream.BeginRead(serviceState.Buffer, 0, 
            (Int32)serviceState.Stream.Length, 
             new AsyncCallback(serviceState.ReadOperationComplete), serviceState);
        return serviceState;
    }
    catch (IOException)
    {
        serviceState.Stream.Close();
        throw;
    }
}

When the background I/O operation is complete, it will call back to the ServiceState.ReadOperationComplete method. Taking a look at that method again below, you can see that I'm extracting the serviceState object that I provided, and I'm using it to access the HostCallback. By invoking this method, I'm letting the ASMX handler know that the job is done and that the EndAsyncWork method should now be called.

public void ReadOperationComplete(IAsyncResult ar)
{
    ServiceState serviceState = ar.AsyncState as ServiceState;
    serviceState.HostCallback(serviceState);
}

The ASMX handler will call EndAsyncWork, and this is where the original request made by one of the consumers of our service should be fulfilled. The method below shows how this is done. First, I extract the serviceState, and then call the FileStream.EndRead method to complete the Read operation. Finally, the serviceState object is accessed again in order to work on the Byte buffer that has been populated so as to determine the number of zero bytes. This value is returned from the method, and hence to the consumer, and this request has now been fulfilled.

[WebMethod]
public Int32 EndAsyncWork(IAsyncResult ar)
{
    ServiceState serviceState = (ServiceState)ar;
    try
    {
        serviceState.Stream.EndRead(serviceState.ReadOperationAsyncResult);
    }
    finally
    {
        serviceState.Stream.Close();
    }

    Int32 count = 0;
    foreach (Byte b in serviceState.Buffer)
    {
        if (b == 0) count++;
    }
    return count;
}

Writing the code - WinFX Service

To get this working under WinFX requires a few very minor modifications. Rather than creating a new project and a new class, I'm going to convert the existing class because it's quicker. I'm using the Feb CTP build for this. First, we need to define a contract for our service, which is done by simply defining an interface decorated with the ServiceContract attribute. The interface for our service is shown below. I've called it IWinfxService.

[ServiceContract()]
public interface IWinfxService
{
    [OperationContract]
    Int32 SyncWork();

    [OperationContract(AsyncPattern = true)]
    IAsyncResult BeginAsyncWork(AsyncCallback callback, Object state);

    Int32 EndAsyncWork(IAsyncResult ar);
}

The first method in our contract is the SyncWork method. As you might expect, it has the same signature as the SyncWork method defined on the Service class, but it is also decorated with the OperationContract attribute. For methods that the WinFX host (in this case, still IIS) is to treat asynchronously, we need to set the AsyncPattern property of the OperationContract to true. As a result of doing this, we no longer need to decorate the EndAsyncWork method with any attributes but it does need to be present in the interface.

Next, we need to indicate that our Service class implements this interface. We do this by appending IWinfxService to the class declaration.

public class Service : System.Web.Services.WebService, IWinfxService
{
    ...
}

Next, we need an entry point for our WinFX host, which in this case is IIS. To do this, we create a Service.svc file in the root of the website and put in the line of text shown below. This line instructs the WinFX host to look for a class called Service in a file called ~/App_Code/Service.cs. If you create your WinFX services using the 'Add New Item' dialog, then this will normally be created for you, but here we're converting an ASP.NET Web Service so we have to create it manually.

<% @ServiceHost Language="C#" Debug="true" 
        Service="Service" CodeBehind="~/App_Code/Service.cs" %>

Finally, we need to put a few extra lines into the Web.Config file. Again, if you had used Visual Studio and the 'Add New Item' dialog to create your WinFX service, then this would be done for you. The section that needs to be added to the Configuration node is shown below. Please bear in mind that this is one possible configuration - you can change the binding used for the service, and you would certainly want to change the behaviour such that it didn't return detailed faults to the consumer.

<system.serviceModel>
    <services>
        <service name="Service" behaviorConfiguration="returnFaults">
            <endpoint contract="IWinfxService" binding="wsHttpBinding"/>
        </service>
    </services>
    <behaviors>
        <behavior name="returnFaults" 
                 returnUnknownExceptionsAsFaults="true" />
    </behaviors>
</system.serviceModel>

Now, we just need to test it. First, check that the WinFX service builds OK by setting the website as the startup project and running it. When the IE window pops up, navigate to the service.svc page. Highlight the full url, which will be something like http://localhost:1234/WebSite2/Service.svc, and press Ctrl-C. Next, create a new console application. Then, right-click on the project and select 'Add Service Reference'. In the dialog that appears, paste the URL into the top box and type 'localhost' in the bottom box, and click OK. This sets up a reference to your service. In the Feb CTP of WinFX, there's no support for browsing to a WinFX service in your solution but this will almost certainly improve in newer builds.

Finally, replace the auto-generated Program class with the one below to test your WinFX service. You can use break-points to satisfy yourself that the service is using the asynchronous pattern internally.

class Program
{
    static void Main(string[] args)
    {
        localhost.WinfxServiceProxy proxy = 
           new localhost.WinfxServiceProxy();
        Console.WriteLine(proxy.AsyncWork());
        Console.ReadKey();
    }
}

If you have any problems running the ConsoleApplication test harness included in the download, then it's probably because, on your computer, the website has been assigned a different port number. One way to fix this is to just remove and re-add the Service Reference using the instructions explained two paragraphs above.

Summary

I've shown how to create a service method using the asynchronous pattern to increase the scalability of services that need to perform background I/O operations whether that be implemented using an ASP.NET Web Service or a WinFX service.

History

  • No changes made so far.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Karl M. Hulme


I live with my wife in Bournemouth in the UK. Her shining brilliance consistently inspires me to be the person I know I should be. I enjoy writing music, playing the piano, watching movies, philisophical discussion and eating jaffa cakes.

In my spare time I work for Regency as ICT Manager, dividing my time between IT strategy, software architecture, development, infrastructure, networking and crawling around under desks.
Occupation: Web Developer
Location: United Kingdom United Kingdom
출처 : CodeProject.com (http://www.codeproject.com/KB/cpp/asyncws.aspx)
Posted by 굥쓰
Study/WebService2008. 2. 20. 01:18
Sample Download : Fritz Onion

Building a customizable Web site complete with a collection of pluggable Web Parts is fairly easy with the portal infrastructure of ASP.NET 2.0. This model is very flexible, allowing users to easily place your Web Parts anywhere on the Web page so they are free to customize your site. However, these advantages can also lead to inefficiencies that may degrade the user experience, since you may not know beforehand which components will be used together, and therefore can’t make specific data retrieval optimizations for each individual component.

The most common inefficiency in a typical portal site occurs when multiple Web Parts simultaneously make network requests for data. Each request, whether to a Web service or a remote database, ends up adding to the overall time it takes to process the page even though the requests are typically independent of each other and could conceivably be issued in parallel.

Fortunately, ASP.NET 2.0 also introduces an easy-to-use asynchronous page model that, when used in combination with asynchronous Web service calls and asynchronous database access, can significantly improve the response time for a portal page as several independent Web Parts collect data in parallel. Here I’ll look at techniques for building Web Parts that perform their data retrieval asynchronously to make the portal pages that contain them more responsive and scalable.


Web Part Congestion

Let’s begin by considering the portal page shown in Figure 1. In this sample there are four Web Parts on a portal page, each retrieving data from a different source. The full source for this sample application is available for download on the MSDN®Magazine Web site and I encourage you to review the application as you read this column. In the sample, three of the Web Parts retrieve their data from a Web service, which intentionally waits for three seconds before returning. The fourth Web Part issues an ADO.NET query to a SQL Server database, which also waits three seconds before returning. This is an exaggerated example of the problem, but it’s not all that improbable.

Figure 1 Sample Portal Page
Figure 1 Sample Portal Page

Each of the Web Parts in the sample application is built with a user control and binds the results of the data retrieval to the controls that display it. The code and markup for each control is kept to a minimum so that the example is simple and lets you focus on making the Web Parts asynchronous.

Here’s the NewsWebPart.ascx user control file:

   <%@ Control Language="C#" 
           AutoEventWireup="true" 
           CodeFile="NewsWebPart.ascx.cs"  
           Inherits="webparts_
           NewsWebPart" %>

   <asp:BulletedList ID="_newsHeadlines" 
           runat="server">
   </asp:BulletedList>
And here’s the corresponding codebehind file for the news headlines sample Web Part:
public partial class webparts_NewsWebPart : UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PortalServices ps = new PortalServices();
        _newsHeadlines.DataSource = ps.GetNewsHeadlines();
        _newsHeadlines.DataBind();
    }
}

Note how it interacts with a Web service to retrieve the sample news headlines. The stock quotes Web Part and the weather forecaster Web Part are implemented in much the same way and use different methods of the same Web service to retrieve their data. Similarly, Figure 2 shows the SalesReportWebPart.ascx user control file and the corresponding codebehind file for the sales report sample Web Part. Note how the control uses ADO.NET to retrieve the sales data from a database and then populates a GridView control with that data.

Figure 3 Sequential Web Part Processing
Figure 3 Sequential Web Part Processing

As soon as the sample portal page is run, a problem becomes apparent. It takes more than 12 seconds for the request to process—a delay that will make most users avoid using the application. The reason for this lengthy delay is shown in Figure 3, which traces the path of execution a request takes when this page is executed. Like any other control in a page’s control hierarchy, each Web Part is loaded in turn, in the order defined by the control hierarchy of the page. Because this process is sequential, each Web Part must wait for the part preceding it in the hierarchy to complete before it can begin requesting its data and preparing its response. Because of the artificial 3-second delay introduced in each data retrieval, it is easy to see why it takes 12 seconds for the response to complete. Each Web Part is performing a completely independent data retrieval, one after the other. The important thing to realize is that all of these retrievals could be performed in parallel, cutting the response time by 75 percent. That is my goal here.

Back to top

Asynchronous Web Access

In the example, three Web Parts use Web services to retrieve their data, and one uses ADO.NET to access a database. Let’s start by making the Web service invocations asynchronous, since there is some nice support in the Web service proxy classes generated by the Web Services Description Language tool WSDL.exe (or the Visual Studio 2005 Add Web Service Reference tool) for performing Web method invocation asynchronously.

When a Web service proxy class is created in ASP.NET 2.0, it actually generates three different ways of invoking any particular method, one synchronous and two asynchronous. For example, the Web service proxy that the Web Parts are using has the following methods available for invoking the GetNewsHeadlines Web method:

   public string[] GetNewsHeadlines()
   
   public IAsyncResult BeginGetNewsHeadlines(
       AsyncCallback callback, object asyncState) 
   public string[] EndGetNewsHeadlines(       IAsyncResult asyncResult) 

   public void GetNewsHeadlinesAsync() 
   public void GetNewsHeadlinesAsync(       object userState)
   public event
       GetNewsHeadlinesCompletedEventHandler 
       GetNewsHeadlinesCompleted;

The first method, GetNewsHeadlines, is the standard synchronous method. The next two, BeginGetNewsHeadlines and EndGetNewsHeadlines, can be used to invoke the method asynchronously and can be tied into any number of asynchronous mechanisms in .NET through the standard IAsyncResult interface.

But the most interesting method to use in this scenario is the last one: GetNewsHeadlinesAsync. In order to use this particular method, I must register a delegate with the proxy class’s event that was specifically generated to capture the results of async invocations (the GetNewsHeadlinesCompleted event in the example). The delegate signature is strongly typed to contain the return values of the method so that I can easily extract the results in the method implementation.

Using this event-based asynchronous method, rewriting the Web method invocation in the headline news Web Part to be asynchronous is easy, as shown in Figure 4. I first subscribe a delegate to the GetNewsHeadlinesCompleted event of the proxy class, and then call the GetNewsHeadlinesAsync method. In the implementation of the method subscribed to the completed event, I bind the results of the Web method call to the BulletedList to display to the client. One additional consideration is that these asynchronous methods only work if the Web Part is placed on a page with the Async="true" attribute set, which can be checked programmatically by looking at the IsAsync property of the containing page. If the page the Web Part is placed on is not async, then I need to resort to standard synchronous binding, as shown in Figure 4.

Now, for the asynchronous Web Part to perform its data retrieval asynchronously, it must be placed on a page with the Async attribute set to true, so I modify the Page directive of the portal page to look like the following:

<%@ Page Language="C#" AutoEventWireup="true"  Async="true" %>

Once I update the other two Web Parts that use Web services to retrieve their data asynchronously, the portal page is much more responsive. In fact, depending on the order in which the parts are loaded, it may render to the client in just over three seconds (if the sales Web Part is loaded first, it will take just over six seconds)! Even though the sales report Web Part is still sequentially accessing the database, the other three Web Parts are now performing their Web service invocations asynchronously so the primary request thread is no longer waiting for their completion. Of course, I ultimately want to have all of the I/O-bound work be asynchronous so that clients could use both Web service and database-driven Web Parts without unnecessary sequential blocking.

The other reason to push the I/O-bound work onto asynchronous I/O requests is to relinquish the primary thread back to the thread pool to service other requests. Currently I’m relinquishing the thread only after my sales report database query has completed, which means I’m sitting idly for three full seconds occupying a threadpool thread that could be used to service other requests. If I can make this last I/O-bound request for data asynchronous as well, my page will use the request thread only long enough to spool off all the asynchronous I/O requests and then return immediately back to the pool.

Back to top

How It Works

If you’ve ever done any asynchronous programming, you probably have the feeling that the minor changes made to the Web service invocation can’t possibly be sufficient. I didn’t even have to touch an IAsyncResult interface, nor did I have to let the containing page know that I was performing asynchronous operations (by registering a task or some other technique), and yet it all seemed to work as I had hoped.

The secret lies in the Web service proxy class’s implementation of the asynchronous method, along with a helper class introduced in the Microsoft® .NET Framework 2.0 called the AsyncOperationManager. When I called the GetNewsHeadlinesAsync method of the proxy class, it mapped the call onto an internal helper method of the SoapHttpClientProtocol base class, called InvokeAsync, from which the proxy class derives. InvokeAsync does two important things—it registers the asynchronous operation by calling the AsyncOperationManager’s static CreateOperation method, and it then launches the request asynchronously using the WebRequest class’s BeginGetRequestStream method. At this point the call returns and the page goes on processing its lifecycle, but because the page has been marked with the Async="true" attribute, it will only continue processing the request up through the PreRender event and will then return the request thread to the thread pool. Once the asynchronous Web request completes, it will invoke the method I subscribed to the completed event of the proxy on a separate thread drawn from the I/O thread pool. If this is the last of the asynchronous operations to complete (kept track of by the synchronization context of the AsyncOperationManager), the page will be called back and the request will complete its processing from where it left off, starting at the PreRenderComplete event. Figure 5 shows this entire lifecycle when you use asynchronous Web requests in the context of an asynchronous page.

Figure 5 Asynchronous Web Requests in Asynchronous Pages
Figure 5 Asynchronous Web Requests in Asynchronous Pages

The AsyncOperationManager is a class that is designed to be used in different environments to help in the management of asynchronous method invocations. For example, if I called a Web service asynchronously from within a Windows® Forms application, it would also tie into the AsyncOperationManager class. The difference between each environment is the SynchronizationContext associated with the AsyncOperationManager. When running in the context of an ASP.NET-based application, the SynchronizationContext will be set to an instance of the AspNetSynchronizationContext class. The primary purpose here is to keep track of how many outstanding asynchronous requests are pending so that when they are all complete, the page request processing can resume. In contrast, when in a Windows Forms-based application, the SynchronizationContext will be set to an instance of the WindowsFormsSynchronizationContext class. Its primary purpose is to allow for easier marshaling of invocations from a background thread to the UI thread.

Back to top

Asynchronous Data Access

Now, back to the problem of making the last Web Part asynchronous and the general issue of performing asynchronous data retrieval with ADO.NET. Unfortunately, there is no equivalent to the simple asynchronous mechanism exposed by Web service proxies for performing asynchronous data retrieval, so I’m going to have to do a little more work to get the final Web Part to participate in the asynchronous shuffle. I can work with the new asynchronous methods on the SqlCommand class and the asynchronous task feature of ASP.NET. Using SqlCommand, I can now invoke commands asynchronously using one of the following methods:

  • IAsyncResult BeginExecuteReader(AsyncCallback ac, object state)
  • IAsyncResult BeginExecuteNonQuery(AsyncCallback ac, object state)
  • IAsyncResult BeginExecuteXmlReader(AsyncCallback ac, object state)
And I can invoke the corresponding completion methods once the data stream is ready to begin reading:
  • SqlDataReader EndExecuteReader(IAsyncResult ar)
  • int EndExecuteNonQuery(IAsyncResult ar)
  • XmlReader EndExecuteXmlReader(IAsyncResult ar)

To use any of these asynchronous retrieval methods, "async=true" must be added to the connection string. For this scenario, I am interested in populating a GridView by binding it to a SqlDataReader, so I will use the BeginExecuteReader method to initiate the asynchronous call.

To tie this into the asynchronous page, ASP.NET 2.0 also allows you to register asynchronous tasks that need to be executed before the page completes rendering. This is a more explicit model than the one I used with the Web service proxies, but it also provides more flexibility. To register an asynchronous task, I create an instance of the PageAsyncTask class and initialize it with three delegates: begin handler, end handler, and timeout handler. The begin handler must return an IAsyncResult interface, so this is where I will launch my asynchronous data request using BeginExecuteReader. The end handler is called once the task is complete (when there is data ready to read in this example), at which point I can use the results. ASP.NET will take care of invoking the begin handler just before it relinquishes the request thread (immediately after the PreRender event completes). Figure 6 shows the updated implementation of the sales report Web Part performing asynchronous data access using the asynchronous tasks and the asynchronous BeginExecuteReader method of the SqlCommand class.

Note that I could use this same technique with my Web service requests by using the alternate asynchronous methods provided on the proxy class (BeginGetNewsHeadlines, for example). One potential advantage to this technique is that I can also specify a timeout handler. If the remote invocations fail to return in time, the associated timeout handler will be invoked. This timeout is specified in the Page directive using the AsyncTimeout attribute and defaults to 20 seconds. Also note that unlike when using the event-based asynchronous pattern, when using Page.RegisterAsyncTask I don’t have to branch to a synchronous invocation based on the result of Page.IsAsync. Asynchronous page tasks in Web Parts work just fine on synchronous pages, and even allow for parallel execution of Web Parts. The core difference is that on synchronous pages (ones without the Async="true" attribute), the main page thread won’t be released back to the thread pool during the execution of the asynchronous operations.

With all of the Web Parts now performing their data retrieval asynchronously, I can now use these parts in any page marked as asynchronous and know that the response time will no longer be the sum of the time it takes for all the Web Parts to retrieve their data, but the maximum amount of time taken by any one Web Part. By marking the page as asynchronous and using Web Parts that perform asynchronous I/O, I also increase the potential scalability of the site since the page will free up the primary request thread to service other clients while waiting for the data. The key takeaway here is that if you’re building portal sites with ASP.NET 2.0, you should keep in mind all of the new asynchronous features introduced in this release, and take advantage of them to improve both the responsiveness and scalability of your applications. For more information on asynchronous support in ASP.NET 2.0, see Jeff Prosise’s Wicked Code column in the October 2005 issue of MSDN Magazine.

Send your questions and comments for Fritz to xtrmasp@microsoft.com.

출처 : MSDN (http://msdn.microsoft.com/msdnmag/issues/06/07/ExtremeASPNET/default.aspx)

Posted by 굥쓰
Study/WebService2008. 2. 20. 01:09

Show Me the Code

I created two .NET projects for this article. One is a basic Web Service; the other is a Windows Forms client that calls the Web Service. The Web Service is a simple service that maintains a list of people and their phone numbers. It reads the information from a file and caches the file contents in the ASP.NET application cache for efficiency. The Web Method I call is named FindPhoneNumberForName.

The client is a Windows Forms application in this demonstration, but this isn't universally the case. Quite often, the client could be another service or perhaps some business logic in a large enterprise application. Any .NET consumer of a Web Service can use the techniques I show here. Figure 2 shows the user interface I created.

Figure 2: Demonstration User Interface

The user provides a name for which he or she desires a phone number, clicks "Get", and then waits for the response.

The interesting code is in the button click handler for the "Get" button:

private void cmdGet_Click(object sender, System.EventArgs e)
{
   // Create the Web Service proxy...
   YPServiceClass ws = new YPServiceClass();

   if ( tbName.Text.Length > 0 )
   {
      this.Cursor = Cursors.AppStarting;
      try
      {
         // Make the asynchronous call
         ws.BeginFindPhoneNumberForName(tbName.Text,
            new AsyncCallback(lookupHandler),ws);
      } // try
      catch (Exception ex)
      {
         string strMsg =
            String.Format("Error initiating asynchronous " +
                          "call: '{0}'",ex.Message);
         MessageBox.Show(strMsg, "Web Error",
                         MessageBoxButtons.OK,
                         MessageBoxIcon.Error);
         this.Cursor = Cursors.Arrow;
      } // catch
       // if
   else
   {
      lblPhone.Text = "";
   } // else
}

I italicized the actual call to the Web Service. Note that unlike a synchronous call, which would look like this:

ws.FindPhoneNumberForName(tbName.Text);

it has a very different invocation that looks like this:

ws.BeginFindPhoneNumberForName(tbName.Text,
     new AsyncCallback(lookupHandler),ws);

You know it's an asynchronous call because I use the "begin" form of the method's signature, but other parameters go into the request also. The first is an instance of an AsyncCallback object, and the second is my instance of the Web Service proxy itself. The AsyncCallback object is actually a .NET delegate defined specifically for asynchronous callbacks. It defines the method signature for a method that you provide to handle the returned information from the Web Service. The delegate defines a method signature like the following:

delegate void AsyncCallback(IASyncResult iar);

If you generate a method with the preceding signature and provide that to the constructor of the AsyncCallback delegate (as I did in the Web Service invocation), when the Web Service response arrives from the remote server, .NET will invoke the method you provided to the delegate's constructor. In this case, .NET will invoke the method I named lookupHandler.

The lookupHandler method is then where I actually strip out the results of the Web Service invocation:

private void lookupHandler(IAsyncResult iar)
{
   try
   {
      // Finalize the asynchronous call
      string number = ((YPServiceClass)iar.AsyncState).
         EndFindPhoneNumberForName(iar);

      // Update the user interface
      object[] args = new object[1];
      args[0] = number;
      this.Invoke(new LookupUIUpdateHandler(lookupUIUpdateHandler),
         args);
   } // try
   catch (Exception ex)
   {
      string strMsg = 
         String.Format("Error terminating asynchronous " + 
         "call: '{0}'",ex.Message);
      MessageBox.Show(strMsg,"Web Error",
                      MessageBoxButtons.OK,
                      MessageBoxIcon.Error);
   } // catch
}

I italicized the magic line of code in lookupHandler for extracting the resulting Web Service return parameter information, but I'll repeat it here because it looks a little strange:

string number =
   ((YPServiceClass)iar.AsyncState).EndFindPhoneNumberForName(iar);

As you may recall, when we invoked the Web Service asynchronously, we passed our parameters, an AsyncCallback object, and a reference to the Web Service proxy itself into the "begin" method. We provided the proxy reference to a call implemented by the proxy itself because an object must be responsible for maintaining the state of the asynchronous call. In other words, some object .NET can control must contain the results of the call so that .NET can forward those results to us. This is the job of the proxy: the proxy implements the "end" (call finalization) method. If you write your own asynchronous calls, you may have to provide your own asynchronous state object. But in the case of .NET Web Services and its proxies, you use the proxy objects themselves.

So, the following piece of code does nothing more than return the original proxy object:

((YPServiceClass)iar.AsyncState

And becaise it's our Web Service's proxy, it implements the "end" method, EndFindPhoneNumberForName. So, all we really did was perform a simple cast to obtain our proxy and then finalize the call. The return value for EndFindPhoneNumberForName is simply the return value of the phone number request method, which is a string representing the phone number of the individual requested.

If I were calling this Web Service code and didn't need to display the string in a window, I'd be done. But in the lookupHandler method, some additional code is necessary because I am sending this string to a window for display. Remember, only the thread that created the window can change its state, and when .NET calls back with the result of an asynchronous Web Service call, the callback is executing on a .NET thread pool thread. So, I must marshal the string from the .NET thread pool thread to my application's user interface thread before I can display it. If I don't, the application might not work. The only correct thing to do is marshal the data, even if it is just a simple string.

As I mentioned previously, .NET accomplishes marshaling by its eventing environment and the Control.Invoke method. The following code sets up the call to Invoke:

object[] args = new object[1];
args[0] = number;
this.Invoke(new LookupUIUpdateHandler(lookupUIUpdateHandler), args);

The LookupUIUpdateHandler follows the .NET guidelines for triggering Invoke events. It defines a method that accepts the type and number of parameters contained within args. For this example, that's merely a single string input:

delegate void LookupUIUpdateHandler(string number);

To the delegate's constructor, I passed a reference to the method that implements the delegate, lookupUIUpdateHandler:

private void lookupUIUpdateHandler(string number)
{
   // Update the window
   lblPhone.Text = number != null ? number : "(no number listed)";

   // Reset the cursor (previously set when async call
   // was made).
   this.Cursor = Cursors.Arrow;
}

And, it is here I finally update the user interface because .NET guarantees me that at this point I'll be on the proper thread for managing windows in my application.

Wrap Up

And there you have it: .NET provides a pool of threads that you can use for asynchronous client-side Web Service processing. The way you get to these threads is to use the special "begin" and "end" methods .NET generates when it reads the service description language. You'll find these methods in the resulting proxy file created when adding a Web reference to your project.

By using one of these internal .NET threads, you can free your primary process thread (the one that most likely created your user interface if you're calling the Web Service from a Windows Forms application). The secondary thread blocks while waiting for the Web Request to return, allowing the primary thread to continue performing its main processing tasks (such things as painting, animation, and button/text inputs). This is the essence of asynchronous processing, and it's quite effective. Give it a try!

About the Author

Kenn Scribner is with Wintellect.

출처 : develoter.com (http://www.developer.com/net/net/article.php/11087_3408531_2)

Posted by 굥쓰
Study/WebService2008. 2. 20. 01:07

Working With Asynchronous .NET Web Service Clients
By Kenn Scribner

by Kenn Scribner of Wintellect

While many developers realize that consuming Web Services using the asynchronous call mechanisms built into .NET is useful, they also find it confusing. If you've had trouble trying to use the asynchronous call methods in your generated proxies—or if you're wondering what asynchronous means in the first place—this article helps clarify some things and makes your programming tasks (at least as related to asynchronous processing) a little easier.

The general discussion focuses on consuming .NET Web Services, but because asynchronous processing is built into .NET, the information is applicable to any asynchronous processing you might want to perform. That is, when you create a proxy for your Web Service, the asynchronous methods are baked in. But, you also have similar asynchronous methods built into your .NET delegate classes and therefore also can utilize delegates in an asynchronous fashion (hmm, perhaps fodder for another article: using .NET delegates as the poor man's multithreaded processing platform?).

Web Service Proxies

To be clear about Web Services and their consumption, Figure 1 presents the basic architecture.



Click here for a larger image.

Figure 1: Generalized Web Service Communication Architecture

The ultimate goal of a Web Service is to give the client the illusion that remote resources and processing capabilities are actually present on the client computer. For this to occur, some software on the client must generate and issue the request (as well as interpret the response), and some software on the server must accept requests, translate them into formats the server can work with, and activate the server-side processing itself. The first of these pieces of software is known as the proxy, because it simulates the server on the client. The proxy communicates with some form of stub, whose job is to take the SOAP-based XML and convert it into binary information resident within the server's memory. The stub also initiates the server-side processing.

When you use .NET to consume Web Services, it can create this proxy for you automatically, either with Visual Studio or the tool that ships with .NET itself, Wsdl.exe. Either tool queries the Web Service for its service description, as described by the Web Service Description Language (WSDL), and then generates the proxy source code for you by interpreting the WSDL it finds.

If you open the proxy source code you've been given, you should find all of the Web-based methods exposed by the Web Service. But, you'll also find some curiously named methods beginning with "Begin" and "End" and ending with the Web methods you'd expect. That is, if the Web Service exposed a single method named "CalcPayment," in your proxy you would find executable code for not only CalcPayment itself, but also for BeginCalcPayment and EndCalcPayment. You will use these "begin" and "end" methods for your asynchronous processing.

Asynchronous Processing

What exactly is asynchronous processing and why should you be concerned about using it? To answer that, let me first ask you a question: How do you feel when an application's user interface locks for some extended period of time while it processes your selected action? Most of us tend to dislike that kind of application behavior—and I'm being kind here.

Instead, we prefer our user interfaces responsive, even when working on actions we know to be lengthy. If that action involved a call to a Web Service, especially one over the Internet (versus simply a local one found on our intranet), the request might take quite a bit of time to process. Delays from 200 milliseconds up to full seconds are not uncommon. For example, say a given Web Service takes on average of 600 milliseconds to complete, but the user waits over half a second for each call. That might not seem like much, but to a user it could prove to be a major annoyance. Remember, the user interface locks entirely for this call duration.

The process thread used to make the call to the Web Service causes the locking phenomenon. The user interface usually locks up because the thread servicing the user interface (button clicks, painting behavior, and so forth) is also the very same thread making an extended call somewhere else, waiting for data. It might be a Web Service call, but it also might be some lengthy local financial calculation, database query, or whatever. Anything that takes a significant amount of time to process, if serviced by the same thread that manages the user interface, will cause the user interface to cease taking inputs from the user and appear to be locked. In general, try to avoid this. Such behavior has been known to cause users to throw keyboards, break monitors, and worst of all, no longer purchase your software.

Getting back to asynchronous processing, my dictionary defines asynchronous as "pertaining to a transmission technique that does not require a common clock between the communicating devices." Overlooking the clock reference, you could rewrite this definition in its loosest terms to read something like "asynchronous processing is a form of multithreaded programming where a primary thread activates a lengthy process to be managed by a secondary thread." The primary thread's task is complete when the process is activated, so it can (almost immediately) return to its main function, which in this case might be managing the user interface. The secondary thread then begins the lengthy process and sustains the wait, a procedure known as blocking. It locks and ceases processing, waiting for the lengthy process to terminate. It then (optionally) reports back to the primary thread the results of the process.

A side benefit of this is that you then can easily make multiple such processing calls in parallel. If the primary thread made multiple calls (synchronously, on the same thread), each synchronous call would need to be made sequentially, thus increasing the processing time greatly.

Asynchronous .NET Web Requests

In the case of a Web Service called using the proxy's asynchronous call methods, the secondary thread is actually a thread from the .NET thread pool. What's great about that is you don't need to create and manage your own thread (or pool of threads, which is even more complicated). Instead, when you call the "begin" method found in your proxy, you pass into that a callback function .NET will use to provide the Web Service response to your application. Always remember that when your callback function is executed, the .NET thread executes it, not the primary thread (which is most likely the thread that created your user interface). I'll revisit this a bit later in this discussion.

Although not the only way to process asynchronous Web Service calls, I finalize the call to the Web Service by extracting the resultant parameter values, as well as the method return value, in the callback function. This finalization is the call to the "end" method found in your Web Service proxy. The "end" method retrieves the resulting parameter objects (out and ref parameters), as well as the return value, and provides them to you for further processing. The beauty is that your primary thread doesn't have to block while waiting for the Web Service to complete. Instead, a .NET thread is used, and your user interface is free to continue to accept user inputs and be just as responsive and snappy as it was prior to the Web Service invocation. Later, when the call is complete, you can marshal the data back to your user interface thread and do whatever is necessary at that point.

You could, if you desire, perform some wait processing in your main thread (a technique for this is described in the MSDN article "Communicating with XML Web Services Asynchronously"). Personally, I find this to be of limited value because you're still employing the primary thread to perform the wait processing. But, I would be remiss if I didn't at least mention it.

Asynchronous Behavior Is a Client-Side Phenomenon

No matter how you call your Web Service, synchronously or asynchronously, each call is the same to the Web Service. You're still sending in one SOAP packet, and you'll still (hopefully) receive a single SOAP packet in response.

Marshaling—a Side Note for Windows User Interface Programmers

Marshaling—what an ugly sounding word. It harkens back to the old COM programming days when we had to deal with passing data between threads. Oh, but that's exactly what we're doing here! Uh-oh. Actually, it's not so bad. Marshaling basically is the process of converting information for communication between threads.

Because Windows is a virtual, memory-based operating system, memory addresses are meaningless between processes. If you pass information between processes based upon that information's memory address, it will have no meaning in the other process and this second process likely will crash. Old COM programmers (and I include myself here) know this all too well.

Fortunately, .NET handles marshaling for you without too much effort on your part. The age-old Windows rule that only—and I mean only—the thread that created a window can update that window's state also helps. Therefore, you can't just change window text, color, shape, size, or political affiliation willy-nilly without making sure you're doing so from the thread that created the window. Forget this fact and Windows becomes unpredictable. Sometimes things work, and sometimes they don't. If you follow the basic rule, though, and always let the window's creating thread update the window, your code (and application) won't crash and burn.

Delegates in .NET

If you look into your MSDN documentation under System.Windows.Forms.Control, you'll find a curious method called Invoke(). The first sentence of the associated documentation page reads: "Executes a delegate on the thread that owns the control's underlying window handle." Wow, that sounds a lot like "the thread that created the window." But what's this "executes a delegate" stuff?

We're back into the guts of .NET, but this time we're looking at the delegate and its relationship to events and event handling. If you create a delegate that has as its parameters the very same parameters that the Web Service returned, .NET will marshal those parameters for you and ship them to the particular window's creation thread. There you have it! The parameters are now available to the window's creating thread, so at that point you can update the window to your heart's content. Easy, right?

The truth is it isn't too bad, once you've seen it in action. In .NET terms, a delegate is essentially a type-safe callback function. So, what you need to do utilize it is:

  1. Design a method in your application that accepts the same parameters the Web Service returned;
  2. Designate it as a delegate data type; and
  3. Create one of these delegates for use with Invoke().

I'll show some code for this shortly. But first, back to asynchronous Web Service calls in general.

출처 : developer.com(http://www.developer.com/net/net/article.php/11087_3408531_1)

Posted by 굥쓰
Study/MSDN2008. 2. 20. 00:33

서버 측 비동기 웹 메소드

Matt Powell
Microsoft Corporation

2002년 10월

요약: Matt Powell은 높은 성능의 Microsoft ASP.NET 웹 서비스를 생성하기 위해 서버 쪽에서 비동기 웹 메소드를 사용하는 방법을 보여줍니다.


소개

9월의 3번째 컬럼 (영문) 에서, Microsoft .NET Framework의 서버쪽 특성을 사용해서 HTTP를 통한 비동기 웹서비스 호출에 관해 썼습니다. 이 방법은 많은 백그라운드 쓰레드나 응용 프로그램에 락을 거는 것 없이도 웹 서비스를 호출하는 매우 유용한 방법입니다. 이제 우리는 서버 쪽에서 비슷한 특성을 제공하는 비동기 웹 메소드에 대해 살펴보고자 합니다. 비동기 웹 메소드는 ISAPI extension에서 사용하는 HSE_STATUS_PENDING로써 제공되는 높은 성능의 방식과 비슷하지만, 쓰레드 풀을 직접 관리하는 부담없이 관리되는 코드 안에서 동작되는 모든 이점을 누릴 수 있습니다.

첫째로 일반적인, 동기적 Microsoft ASP.NET의 웹 메소드를 살펴봅시다. 동기적인 웹 메소드에 대한 응답은 메소드로부터 반환이 되었을 때 전송됩니다. 만일 요청을 완료하기에 상대적으로 긴 시간이 걸린다면, 메소드 호출이 끝났을 때까지 요청을 처리하는 쓰레드를 사용하게 됩니다. 불행히도, 오랜 시간이 걸리는 것은 데이터베이스에 질의를 하는 경우와 같은 작업이거나, 다른 웹 서비스를 호출하는 경우입니다. 예를 들면 만일 데이터베이스 호출을 하는 경우, 현재 쓰레드는 데이터베이스에 대한 호출을 완료할 때까지 기다리게 됩니다. 현재 쓰레드는 질의로부터 응답이 올 때까지 아무것도 하지 못하고 기다리게 됩니다. 비슷한 문제들이 TCP 소켓에 대한 호출을 기다리거나 최후위의 웹 서비스를 완료할 때까지 기다리는 일이 일어납니다.

쓰레드들이 대기 중이라는 것을 좋지 않은 일입니다 - 특별히 스트레스를 받는 서버 시나리오에서는 더욱 그러합니다. 대기중인 쓰레드는 다른 요청들을 서비스하는 것과 같은, 생산적인 어떠한 것도 처리할 수 없기 때문입니다. 우리가 서버에서 오랜 시간이 걸리는 백그라운드 프로세스를 시작할 때 필요한 것, 그러나 ASP.NET 프로세스 풀로 현재 쓰레드를 반환하는 것입니다. 그리고 오랜 시간이 걸리는 백그라운드 프로세스가 종료되었을 때, 우리는 ASP.NET에 대한 요청의 완료 신청을 받고 요청에 대한 처리를 마치도록 호출된 콜백 함수를 가지는 것입니다. 이것은 ASP.NET에서 비동기 웹 메소드를 통해 제공되는 특징입니다.

어떻게 비동기 웹 메소드가 동작하는가

웹 메소드를 사용하는 전형적인 ASP.NET 웹 서비스를 우리가 작성할 때, Microsoft Visual Studio .Net은 단순하게 웹 메소드들에 대한 요청을 받았을 때 호출될 수 있는 어셈블리를 생성하도록 코드를 컴파일해 줍니다. 이 어셈블리는 SOAP에 관한 어떤 것도 알지 못합니다. 당신의 응용 프로그램이 처음 실행 될 때, ASMX 핸들러는 어느 웹 메소드가 노출되었는지를 검사하도록 어셈블리를 나타내야 합니다. 일반적인, 동기적 요청에 대해서 이것은 단순히 연관된 WebMethod 속성을 가진 어던 메소드를 발견하는 문제이고, 그리고 SOAPAction HTTP 헤더에 기초해서 올바른 메소드를 호출하도록 로직을 설정하면 됩니다.

비동기 요청에 대해서는, 비동기 적이라고 알 수 있는 특정한 종류의 시그니처를 가진 웹 메소드를 ASMX 핸들러가 리플렉션(Reflection) 동안 찾게 됩니다. 이 경우, 다음의 규칙을 따르는 한 쌍의 메소드들을 찾게 됩니다:

  • BeginXXXEndXXX의 이름을 가지는 웹 메소드가 있으며 여기서 XXX는 노출하기를 원하는 메소드의 이름을 나타냅니다.
  • BeginXXX 함수는 IAsyncResult 인터페이스를 반환하며 마지막 두 개의 입력 파라메터는 AsyncCallback과 객체를 취합니다.
  • The EndXXX 함수는 IAsyncResult인터페이스형 파라미터를 취합니다.
  • 양쪽 모두 WebMethod 속성으로 표시되어야 합니다.

만일 ASMX 핸들러가 이러한 모든 요구 조건에 맞는 2개의 메소드를 발견했다면, 일반 웹 메소드처럼 WSDL에 XXX메소드를 노출시킵니다. 입력으로써 BeginXXX에 대한 시그니처안에 AsyncCallback 파라미터에 정의된 파라미터들을 받게 되며, EndXXX함수에 의해 반환되는 것을 반환하게 됩니다. 만일 우리가 동기적인 선언형태의 웹 메소드를 가졌다면 이와 같이 보일 겁니다:

    [WebMethod]
    public string LengthyProcedure(int milliseconds) {...}

비동기적인 선언의 경우 아래와 같이 보입니다:

    [WebMethod]
    public IAsyncResult BeginLengthyProcedure(
                            int milliseconds, 
                            AsyncCallback cb, 
                            object s) {...}

    [WebMethod]
    public string EndLengthyProcedure(IAsyncResult call) {...}

각각에 대한 WSDL은 동일합니다.

ASMX핸들러가 어셈블리를 리플렉션하고 비동기적인 웹 메소드를 감지한 후에, 반드시 그 메소드에 대한 요청을 동기적인 요청과는 다르게 처리해야 합니다. 단순하게 메소드를 호출하는 것 대신에, BeginXXX 메소드를 호출합니다. 이것은 함수로 전달된 파라미터에서 입력 요청을 직렬해제(deserialize)합니다. 그러나 여기서는 BeginXXX메소드로 여분의 AsyncCallback파라메터로써 내부 콜백 함수에 대한 포인터를 전달합니다.

이러한 접근은 웹 서비스 클라이언트 응용 프로그램에서 .NET Framework에 있는 비동기적 프로그래밍 페러다임과 비슷합니다. 비동기적 웹 서비스 호출에 대한 클라이언트 지원의 경우에, 클라이언트 컴퓨터에 대한 쓰레드를 블록처리하는 것에서 자유로울 수 있으며, 반면에 서버 쪽에서도 서버에서의 쓰레드를 블록처리하지 않고 사용합니다. 여기에는 두 가지 다른 점이 있습니다. 첫 번째로 직접 BeginXXXEndXXX 함수를 호출하는 코드를 작성하는 것 대신에, ASMX핸들러가 대신 이들을 호출해 줍니다. 두 번째로, Visual Studio .NET "웹 참조 추가"마법사나 WSDL.EXE를 사용해서 생성된 코드를 사용하는 것 대신에 BeginXXXEndXXX 함수에 대한 코드를 작성할 수 있습니다. 그러나, 결과적으로 다른 어떤 작업을 수행할 수 있도록 쓰레드를 자유롭게 하는 것은 동일합니다.

ASMX핸들러가 서버의 BeginXXX 함수를 호출한 후에, 받게 되는 어느 다른 요청들을 처리할 수 있도록 프로세스의 쓰레드 풀로 쓰레드를 반환합니다. ASMX 핸들러는 BeginXXX함수로 전달된 콜백 함수가 요청에 대한 처리를 마치고 호출되기까지 대기하게 됩니다.

한번 콜백 함수가 호출되면, ASMX 핸들러는 웹 메소드가 수행하기에 필요한 어떤 처리를 완료할 수 있도록 EndXXX 함수를 호출하게 되며, 그리고 반환 데이터는 SOAP응답 안에 직렬화되도록 제공됩니다. EndXXX함수가 반환된 후 응답이 보내지며 요청에 대한 HttpContext가 해지됩니다.

단순한 비동기 웹 메소드

비동기 웹 메소드를 설명하기 위해서, 필자는 아래에 보여준 코드처럼 LengthyProcedure라고 불리는 단순한 동기 메소드부터 시작합니다. 우리는 어떻게 비동기적으로 동일한 것을 처리하는 지를 볼 것입니다. LengthyProcedure는 수천 분의 몇 초 동안만 블록킹 됩니다.

[WebService]
public class SyncWebService : System.Web.Services.WebService
{
    [WebMethod]
    public string LengthyProcedure(int milliseconds) 
    { 
        System.Threading.Thread.Sleep(milliseconds);
        return "Success"; 
    }
}

이제 우리는 LengthyProcedure를 비동기 웹 메소드로 변환합니다. 우리는 반드시 BeginLengthyProcedure 함수를 생성해야 하며 그리고 앞에서 기술한 것처럼 EndLengthyProcedure 함수를 생성해야 합니다. 우리의 BeginLengthyProcedureIAsyncResult 인터페이스를 반환해야 함을 기억해야 합니다. 이 경우에 필자는 우리의 BeginLengthyProcedure가 위임자(delegate)를 사용하는 비동기적 메소드 호출을 하도록 하며 그 위임자에 대해 BeginInvoke 메소드를 갖도록 하려 합니다. 콜백 함수는 우리의 델리게이트에서 BeginInvoke 메소드를 통해 처리될 수 있도록 BeginLengthyProcedure로 전달되며, BeginInvoke로부터 반환되는 IAsyncResultBeginLenthyProcedure 메소드에서 반환됩니다.

EndLengthyProcedure는 우리의 델리게이트가 완료될 때 호출됩니다. 우리는 IAsyncResult에서 전달된 델리게이트에서 EndInvoke메소드를 호출하며 EndLenghyProcedure호출에 대한 입력으로써 받게 됩니다. 반환된 문자열은 우리의 웹 메소드로부터 반환된 문자열이 됩니다. 여기에 코드가 있습니다:

[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);
    }
}

비동기 웹 메소드가 사용될 때

응용 프로그램에 비동기적 웹 메소드를 사용할지를 결정해야 할 때 고려해야 할 몇 가지 문제가 있습니다. 첫 번째로, 호출에 대해 BeginXXX 함수는 IAsyncResult 인터페이스를 반환해야 합니다. IAsyncResult는 스트림, Microsoft Windows Sockets호출, 파일 I/O를 실행, 다른 하드웨어 디바이스와 교류, 비동기적 메소드의 호출, 그리고 물론 다른 웹 서비스를 호출하는 것과 같은 다양한 비동기적 I/O작업들로부터 반환됩니다. 이러한 종류의 비동기적 작업들 중의 하나로부터 IAsyncResult를 얻기를 원한다면, BeginXXX 함수로부터 반환할 수 있습니다. 다른 옵션은 IAsyncResult 인터페이스를 구현하는 고유의 클래스를 생성하는 것인데, 앞에서 언급한 I/O 구현중의 하나를 포장하는 것 이상을 해야만 합니다.

우리가 언급했던 비동기적 작업들 중 대부분의 경우, 백그라운드 비동기적 호출을 감싸는 비동기적 웹 메소드의 사용을 주의 깊게 만들고 더 효율적인 웹 서비스 코드로 결과를 주게 됩니다. 위임자를 사용하는 비동기적 메소드 호출을 사용할 경우 예외가 있습니다. 위임자는 프로세스의 쓰레드 풀에 있는 쓰레드를 실행해서 비동기적 메소드 호출을 하도록 합니다. 불행히도, 이러한 것들은 입력된 요청들을 서비스하기 위해 ASMX 핸들러에서 사용된 것과 동일한 쓰레드여야 합니다. 하드웨어나 네트워킹 리소스에 대한 실제 I/O 오퍼레이션을 수행하는 호출들과는 달리, 위임자를 사용하는 비동기적 메소드 호출은 실행 중에 프로세스 쓰레드 중의 하나를 여전히 블록킹하게 됩니다. 원래 쓰레드를 블록킹하고 동기적으로 실행되는 웹 메소드를 갖도록 해야 합니다.

다음의 예제는 최후위 웹 서비스를 호출하는 비동기적 웹 메소드를 보여줍니다. 비동기적으로 실행되는 WebMethod 속성으로 BeginGetAgeEndGetAge 메소드를 표시합니다. 이러한 비동기적 웹 메소드에 대한 코드는 반환될 정보를 얻기 위해 UserInfoQuery라 불리는 최후위 웹 메소드를 호출합니다. UserInfoQuery에 대한 호출은 비동기적으로 실행되며 BeginGetAge 메소드로 전달되는 AsyncCallback 함수를 전달합니다. 이것은 최후위 요청이 완료될 때 호출되는 내부 콜백 함수가 실행되도록 합니다. 콜백 함수는 요청을 완료하도록 EndGetAge 메소드를 호출합니다. 이 경우 코드는 앞에서의 예제보다 더 간단해지며, 그리고 우리의 중간 계층의 웹 메소드 요청을 서비스하는 동일한 쓰레드 풀에서 최후위 프로세스을 실행하지 않는 추가적인 이점을 가지게 됩니다.

[WebService]
public class GetMyInfo : System.Web.Services.WebService
{
    [WebMethod]
    public IAsyncResult BeginGetAge(AsyncCallback cb, Object state)
    {
        // 비동기적인 웹 서비스 호출
        localhost.UserInfoQuery proxy 
            = new localhost.UserInfoQuery();
        return proxy.BeginGetUserInfo("User's Name", 
                                      cb, 
                                      proxy);
    }

    [WebMethod]
    public int EndGetAge(IAsyncResult res)
    {
        localhost.UserInfoQuery proxy 
            = (localhost.UserInfoQuery)res.AsyncState;
        int age = proxy.EndGetUserInfo(res).age;
        //  웹 서비스 호출로부터의 결과에 추가적인 프로세싱을 수행합니다. 
        return age;
    }
}

웹 메소드 내부에서 일어나는 가장 공통적인 형태의 I/O작업중의 하는 SQL 데이터베이스에 대한 호출입니다. 불행히도, Microsoft ADO.NET은 2002년 현재 좋은 비동기적인 호출 메카니즘을 가지고 있지 않으며, 단순하게 비동기적인 위임자 호출 안에서 SQL호출을 감싸는 것은 효율성 부분에서 도움이 되지 않습니다. 웹 서비스로 데이터베이스를 노출시키기를 원한다면 Microsoft SQL Server 2000 Web Services Toolkit (영문)을 사용을 고려해 볼 수 있습니다. 데이터베이스를 업데이트하거나 질의하는 비동기적인 웹 서비스를 호출하기 위해서 .NET Framework의 지원을 받을 수 있습니다.

웹 서비스 호출을 통해 SQL을 액세스하는 것은 많은 최후위 리소스들에 사용해야 할 방법 중 하나입니다. 만일 유닉스 서버와 커뮤니케이션을 하기 위해 TCP소켓을 사용한다면, 또는 적절한 데이터베이스 드라이버들을 통해 다른 SQL플랫폼중의 어던 것을 액세스한다면 - 또는 DCOM을 사용해서 액세스하는 리소스를 가지고 있다면 -웹 서비스로써 리소스를 노출시켜주는 현 시점에서 가능한 다양한 웹 서비스 툴킷들의 사용을 고려해 볼 수 있습니다.

이러한 접근법을 취하는 것의 장점 중의 하나는, .NET Framework에서 비동기적 웹 서비스 호출과 같은 클라이언트 쪽 웹 서비스 인프라구조에서의 장점들을 취하는 것입니다. 비용이 들지 않는 비동기적 호출을 얻는다면, 클라이언트 액세스 메커니즘은 비동기적 웹 메소드와 함께 효율적으로 작업할 수 있게 될 겁니다.

비 동기적 웹 메소드를 사용해서 데이터를 집계

오늘날의 많은 웹 서비스들은 최후위 쪽에 있는 다중의 리소스들을 액세스하며 최전위 웹 서비스에 대한 정보들을 집계합니다. 심지어 웹 메소드 모델을 더욱 복잡하게 만드는 여러 개의 최후위 리소스들을 호출하는 것은, 상당한 양의 효율성을 얻게 됩니다.

서비스 A와 서비스 B인, 두 개의 최후위 웹 서비스를 호출하는 웹 메소드가 있다고 합시다. BeginXXX 함수로부터, 비동기적으로 서비스 A를 호출하고 서비스 B를 비동기적으로 호출합니다. 당신은 이러한 비동기적 호출의 각각에 대해서 고유의 콜백 함수를 전달할 수 있습니다. 서비스 A와 서비스 B 양쪽에서 결과를 받은 후에 웹 메소드의 완료를 자동 동작시키기 위해, 당신이 제공한 콜백 함수는 양쪽의 요청이 완료되었는지를 검사하며, 반환된 데이터에 대한 처리를 실행하며, 그리고 BeginXXX 함수에 대해 전달된 콜백 함수를 호출하게 됩니다. 이것은 비동기적 웹 메소드가 완료되도록 반환되는, EndXXX 함수를 호출하도록 자동 동작시킵니다.

결론

비동기적인 웹 메소드는 프로세스 쓰레스 풀 안에 귀중한 쓰레드가 아무것도 하지 않으면서 블록되도록 두지 않고 ASP.NET 웹 서비스에서 최후위 서비스를 호출하는 효율적인 메커니즘을 제공합니다. 최후위 리소스들에 대한 비동기적인 요청들과의 결합하여, 서버는 그들의 웹 메소드들을 최대로 처리할 수 있게 됩니다. 고성능의 웹 서비스 응용 프로그램을 개발하기 위해서는 이러한 접근방법을 고려해 보아야 합니다.


저자에 대해

Matt Powell 은 SOAP Toolkit 1.0을 개발을 도운, MSDN 구조화 샘플 팀의 멤버입입니다. Matt는 MMicrosoft출판사의 Running Microsoft Internet Information Server 의 저자미여, 많은 잡지 기사들을 집필했습니다.

출처 : MSDN - http://ms.inposti.com/korea/msdn/library/ko-kr/dnservice/html/service10012002.aspx

Posted by 굥쓰