Click here to Skip to main content
Licence CPOL
First Posted 31 Mar 2009
Views 6,059
Bookmarked 5 times

Add Asynchronous Data Methods to the Enterprise Library

By Williarob | 31 Mar 2009 | Technical Blog
C#
A Data Access Layer that does not support Asychronous I/O is not scalable. Period. However, since Microsoft kindly provided the source code along with the Enterprise Library, it is possible to make the Enterprise Library's Data Application Block scalable by adding the Asynchronous methods (Begin
A Technical Blog article. View original blog here.[]

A Data Access Layer that does not support Asychronous I/O is not scalable. Period. However, since Microsoft kindly provided the source code along with the Enterprise Library, it is possible to make the Enterprise Library's Data Application Block scalable by adding the Asynchronous methods (BeginExecuteNonQuery, BeginExecuteReader, etc.) to the SQL Database Object. I took the code from the 3.x library and modified the file SqlDatabase.cs found here c:\EntLib3Src\App Blocks\Src\Data\Sql (assuming you installed the source files to the root of your C drive). The following download includes the modified binaries and my SqlDatabase.cs code file:

AsyncEL.zip (348.39 kb)

I don't think I implemented every overload, and again this is for the 3.0 library, but you should be able to modify the 4.x libraries and add more overloads by following the patterns I'm using. To use the binaries included in the zip file above, simply drop them into the bin folder of your web site or web application project, add references if necessary and don't forget to add the async=true attribute to both your <% page %>  tag and your connection string! Here is a simple example of how you might use it, to get you started:

    1 using System; 
    2 using System.Collections; 
    3 using System.Configuration; 
    4 using System.Data; 
    5 using System.Data.SqlClient; 
    6 using System.Linq; 
    7 using System.Web; 
    8 using System.Web.Configuration; 
    9 using System.Web.Security; 
   10 using System.Web.UI; 
   11 using System.Web.UI.HtmlControls; 
   12 using System.Web.UI.WebControls; 
   13 using System.Web.UI.WebControls.WebParts; 
   14 using System.Xml.Linq; 
   15  
   16 public partial class temp : System.Web.UI.Page 
   17 { 
   18     protected void Page_Load(object sender, EventArgs e) 
   19     { 
   20         if (!IsPostBack) 
   21         { 
   22             RegisterAsyncTask(new PageAsyncTask(new BeginEventHandler(
                      BeginUpdateByAsyncEL), new EndEventHandler(EndUpdateByAsyncEL),
                      new EndEventHandler(AsyncUpdateTimeout), null, true)); 
   23         } 
   24     } 
   25  
   26     /// <summary> 
   27     /// Creates a SqlDatabase Object and stores it in HttpContext for the
          /// duration of the request. 
   28     /// </summary> 
   29     /// <value></value> 
   30     /// <returns>A SqlDatabase Object</returns> 
   31     /// <remarks></remarks> 
   32     public static Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase AsyncNorthWindDB 
   33     { 
   34         get 
   35         { 
   36             if (HttpContext.Current == null) 
   37             { 
   38                 return new Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase(
                          WebConfigurationManager.ConnectionStrings[
                          "AsyncNorthwind"].ConnectionString); 
   39             } 
   40             if (HttpContext.Current.Items["AsyncNorthWindDB"] == null) 
   41             { 
   42                 HttpContext.Current.Items.Add("AsyncNorthWindDB",
                         new Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase(
                         WebConfigurationManager.ConnectionStrings[
                         "AsyncNorthwind"].ConnectionString)); 
   43             } 
   44             return HttpContext.Current.Items["AsyncNorthWindDB"]; 
   45         } 
   46     } 
   47  
   48     /// <summary> 
   49     /// Begins an asynchronous call the the Northwind Database  
   50     /// </summary> 
   51     /// <param name="sender"></param> 
   52     /// <param name="e"></param> 
   53     /// <param name="cb"></param> 
   54     /// <param name="state"></param> 
   55     /// <returns>An IAsyncResult Interface</returns> 
   56     /// <remarks>These methods do not exist in the standard
          ///          Enterprise Library</remarks> 
   57     public IAsyncResult BeginUpdateByAsyncEL(object sender, EventArgs e,
              AsyncCallback cb, object state) 
   58     { 
   59         SqlCommand cmd = new SqlCommand(
                  "Update Products SET UnitPrice = 79.0 WHERE productID = 20"); 
   60         return AsyncNorthWindDB.BeginExecuteNonQuery(cmd, cb, state); 
   61     } 
   62  
   63     public void EndUpdateByAsyncEL(IAsyncResult ar) 
   64     { 
   65         AsyncNorthWindDB.EndExecuteNonQuery(ar); 
   66     } 
   67  
   68     /// <summary> 
   69     /// Async Timeout method 
   70     /// </summary> 
   71     /// <param name="ar"></param> 
   72     /// <remarks></remarks> 
   73     public void AsyncUpdateTimeout(IAsyncResult ar) 
   74     { 
   75         Label1.Text = "Connection Timeout"; 
   76     } 
   77 }

Download the Full Enterprise Library (including the complete source code) from http://www.codeplex.com/entlib.

kick it on DotNetKicks.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Williarob

Software Developer (Senior)
Salem Web Network
United States United States

Member
Robert Williams has been programming web sites since 1996 and employed as .NET developer since its release in 2002.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 31 Mar 2009
Article Copyright 2009 by Williarob
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid