Visual Studio Web Applications—Adding Assembly References

Jump to: navigation, search
Visual C# Tutorials
Visual Studio .NET Tutorials

Visual Studio Web Apps

© 2006 M. MacDonald, M. Szpuszta

Adding Assembly References

By default, ASP.NET makes a small set of commonly used .NET assemblies available to all web pages. These assemblies (listed in Table 2-4) are configured through a special machine-wide configuration file. You don’t need to take any extra steps to use the classes in these assemblies.

Table 2-4. Core Assemblies for ASP.NET Pages

Assembly Description
mscorlib.dll and System.dll Includes the core set of .NET data types, common exception types, and numerous other fundamental building blocks.
System.Configuration.dll Includes classes for reading and writing configuration information in the web.config file, including your custom settings.
System.Data.dll Includes the data container classes for ADO.NET, along with the SQL Server data provider.
System.Drawing.dll Includes classes representing colors, fonts, and shapes. Also includes the GDI+ drawing logic you need to build graphics on the fly.
System.Web.dll Includes the core ASP.NET classes, including classes for building web forms, managing state, handling security, and much more.
System.Web.Services.dll Includes classes for building web services—units of code that can be remotely invoked over HTTP.
System.Xml.dll Includes .NET classes for reading, writing, searching, transforming, and validating XML.
System.EnterpriseServices.dll Includes .NET classes for COM+ services such as transactions.
System.Web.Mobile.dll Includes .NET classes for the mobile web controls, which are targeted for small devices such as web-enabled cell phones.

If you want to use additional features or a third-party component, you may need to import more assemblies. For example, if you want to use an Oracle database, you need to add a reference to the System.Data.OracleClient.dll assembly. To add a reference, select Website > Add Reference. The Add Reference dialog box will appear, with a list of registered .NET assemblies (see Figure 2-16).


Image:7680f0216.jpg
Figure 2-16. Adding a reference


In the Add Reference dialog box, select the component you want to use. If you want to use a component that isn’t listed here, you’ll need to click the Browse tab and select the DLL file from the appropriate directory.

When you add a reference, Visual Studio modifies the web.config file to indicate that you use this assembly. Here’s an example of what you might see after you add a reference to the System.Data.OracleClient.dll file:

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <system.web>
    <compilation debug="true">
      <assemblies>
        <add assembly="System.Data.OracleClient, Version=2.0.0.0, ..."/>
      </assemblies>
    </compilation>
    <!-- Other settings omitted. -->
  </system.web>
</configuration>

If you add a reference to an assembly that isn’t stored in the GAC (global assembly cache), Visual Studio will create a Bin subdirectory in your web application and copy the DLL into that directory. This step isn’t required for assemblies in the GAC because they are shared with all the .NET applications on the computer.


Note Unlike previous versions of Visual Studio, in Visual Studio 2005 you won’t see a list of assembly references in the Solution Explorer. Instead, you need to crack open the web.config file to get that information.


If you look at the code for a web-page class, you’ll notice that Visual Studio imports a lengthy number of core .NET namespaces. Here’s the code you’ll see:

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;

Adding a reference isn’t the same as importing the namespace with the using statement. The using statement allows you to use the classes in a namespace without typing the long, fully qualified class names. However, if you’re missing a reference, it doesn’t matter what using statements you include—the classes won’t be available. For example, if you import the System.Web.UI namespace, you can write Page instead of System.Web.UI.Page in your code. But if you haven’t added a reference to the System.Web.dll assembly that contains these classes, you still won’t be able to access the classes in the System.Web.UI namespace.


prevpp.png  nextpp.png
C# Online.NET