By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,314 Members | 1,721 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,314 IT Pros & Developers. It's quick & easy.

Asynchronous Problem in web application

P: n/a
Hi,

I have a asp.net 2.0 web application.
I want to implement the asynchronous model through http handler in
web.config

-------------------------------------------------------------------------------------------------------------------------

My web.config file:
---------------------
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings>
<add name="AdvWorks" connectionString="Data Source=(local); Integrated
Security=SSPI; Initial Catalog=AdventureWorks; Asynchronous
Processing=true"/>
</connectionStrings>
<system.web>
<httpHandlers>
<add verb="*" path="*.myasync" type="ProductAsyncHandler"/>
</httpHandlers>
<compilation debug="true"/>
</system.web>
</configuration>

-------------------------------------------------------------------------------------------------------------------------

ProductAsyncHandler class
-----------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public class ProductAsyncHandler : IHttpAsyncHandler
{
#region IHttpHandler-related members (if you want to perform a synchronous
operation in the HTTP handler)

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
}

public bool IsReusable
{
get
{
return false;
}
}

#endregion

#region IHttpAsyncHandler-related members (if you want to perform an
asynchronous operation in the HTTP handler)

// The command object that will be performed asynchronously.
private SqlCommand _command;

// The HttpContext object that provides access to HTTP-related resources
(such as HTTP request and response objects).
private HttpContext _context;

/// <summary>
/// ASP.NET calls this method, to begin an asynchronous operation.
/// This method starts an asynchronous data access operation, and then
returns immediately to ASP.NET.
/// </summary>
/// <param name="context">Provides access to HTTP resources such as the
HTTP request and response</param>
/// <param name="cb">References the callback method to be called when the
asynchronous operation is complete.</param>
/// <param name="extraData">Additional state that will be passed into the
callnback method</param>
/// <returns>Represents the status of the asynchronous operation.</returns>
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback
cb, object extraData)
{
// Save the context parameter in an instance variable, so we can use it in
the callback method.
_context = context;

// Set up the method return variable.
IAsyncResult result = null;

// Create a connection object.
string connectionString =
ConfigurationManager.ConnectionStrings["AdvWorks"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);

// Set up the command object, using the list price specified in the query
string.
decimal listPrice = decimal.Parse(context.Request["ListPrice"]);
_command = new SqlCommand("uspGetProductsBelowListPrice", connection);
_command.CommandType = CommandType.StoredProcedure;
_command.Parameters.AddWithValue("@ListPrice", listPrice);

try
{
// Open the database connection.
connection.Open();

// Perform the command asynchronously.
result = _command.BeginExecuteReader(cb, listPrice);
}
catch (Exception)
{
// Only close the connection if an exception occurs.
// Otherwise, keep it open for the asynchronous command.
// The callback method will close the connection when the command is
complete.
connection.Close();

// Rethrow the exception, so ASP.NET can handle it.
throw;
}

// Return status information to ASP.NET.
return result;
}

/// <summary>
/// ASP.NET calls this method when the asynchronous data-access command is
complete.
/// This method retrieves the result of the command, and then closes the
connection.
/// </summary>
/// <param name="result">Represents the status of the asynchronous
operation.</param>
public void EndProcessRequest(IAsyncResult result)
{
try
{
// Get the data reader returned by the query.
SqlDataReader reader = _command.EndExecuteReader(result);

// Iterate through the product rows returned by the query.
_context.Response.Write("<h1>Products that cost less than $" +
result.AsyncState.ToString() + "</h1>");
while (reader.Read())
{
string productDetails = String.Format("[{0}] {1}, ${2}<br/>",
reader["ProductID"],
reader["Name"],
reader["ListPrice"]);

_context.Response.Write(productDetails);
}
}
finally
{
// Close the connection and wrap up.
_command.Connection.Close();
_command.Dispose();
_command = null;
}
}

#endregion
}

-------------------------------------------------------------------------------------------------------------------------

Default Page:
-------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Asynchronous Callback Test</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<a href="anything.myasync?ListPrice=1000">Get Products that cost
less than $1000</a>
<br />
<a href="anything.myasync?ListPrice=2000">Get Products that cost
less than $2000</a>
<br />
<a href="anything.myasync?ListPrice=3000">Get Products that cost
less than $3000</a>
<br />

</div>
</form>
</body>
</html>
---------------------------
No Code Behind
----------------------------

Thank you.
Bishoy
Oct 26 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.