You want to create a business service class that can be used by any page in your site, and you want it to have access to the HTTP-specific information available in web pages—that is, all the server objects used by the application.
Add a reference to the System.Web
assembly in your
business service project and a companion Imports
statement (or using
statement in C#) to your
class, and then use the Current
property of the
HttpContext
object to access the desired server
objects.
In the business service class, use the .NET language of your choice to:
Add a reference to System.Web
.
Import the System.Web
namespace.
Reference the current HTTP context when accessing server objects, as
in HTTPContext.Current.Session
.
Example 18-1 and Example 18-2 show the VB and C# class files for an example business service that implements this solution.
By referencing the Current
property of the
HttpContext
object in the business class, your
code has full access to all the server objects used in web
applications. This includes the ability to access all information
about the request being made, the response being returned, session
data, and application data. For more information, refer to the
HttpContext
class in the MSDN documentation.
When you create an ASP.NET application with Visual Studio .NET, all
the pages and classes of the web project have access to the
HTTP-specific information. This is because Visual Studio
automatically adds a reference to System.Web
when
you create the project. When you create a Class Library project, the
technique commonly used to create reusable assemblies, Visual Studio
does not automatically add the reference to
System.Web
. You can easily do so manually,
however, by right-clicking the project in the Solution Explorer and
then selecting Add Reference from the context menu. When the Add
Reference dialog box appears, choose the
System.Web.dll
component from the .NET tab.
If you are not using Visual Studio for your project, you can add the reference as part of the compile command. The following fragments show how we do this for our example:
vbc /target:library/reference:System.Web.dll
/out:BusinessService.dll BusinessService.vb csc /target:library/reference:System.Web.dll
/out:BusinessService.dll BusinessService.cs
In the class requiring access to the HTTP-specific information, add
an Imports
statement (or using
statement in C#) for the System.Web
assembly.
Adding this statement imports the namespace and provides access to
the HTTP objects without having to fully qualify each reference to
the object, making the code more readable and easier to maintain. The
difference in access methods is shown here:
'access to the Session object when Imports statement is included value = CStr(HttpContext.Current.Session
("someData")) 'access to the Session object when the Imports statement is NOT included value = CStr(System.Web.HttpContext.Current.Session
("someData")) // access to the Session object when Imports statement is included value = (string)(HttpContext.Current.Session
["someData"]); // access to the Session object when the Imports statement is NOT included value = (string)(System.Web.HttpContext.Current.Session
["someData"]);
By adding the reference and Imports
(or
using
) statement to your business service
projects, your code now has full access to all the server objects
used in web applications.
Providing access to the server objects in your business classes is useful and necessary for some services, but it should not be universally applied. When your business service requires access to the HTTP-specific information, it can no longer be used in non-web applications, reducing its reusability.
HttpContext
class documentation in the MSDN Library