Visual Studio Web Applications—Adding Assembly References
|Visual C# Tutorials|
|Visual Studio .NET Tutorials|
|© 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
|Includes the core set of .NET data types, common exception types, and numerous other fundamental building blocks.|
| Includes classes for reading and writing configuration information in the |
|Includes the data container classes for ADO.NET, along with the SQL Server data provider.|
|Includes classes representing colors, fonts, and shapes. Also includes the GDI+ drawing logic you need to build graphics on the fly.|
|Includes the core ASP.NET classes, including classes for building web forms, managing state, handling security, and much more.|
|Includes classes for building web services—units of code that can be remotely invoked over HTTP.|
|Includes .NET classes for reading, writing, searching, transforming, and validating XML.|
|Includes .NET classes for COM+ services such as transactions.|
|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).
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
<?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=22.214.171.124, ..."/> </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