Tuesday, June 3, 2008

Adding references - WebSite versus WebApplication

On my current project I’m creating an ASP.NET site in Visual Studio 2005 SP1.

NOTE: By default, Visual Studio 2005 only allows you to create ASP.NET sites by using the WebSite template but after installing SP1, you can also use the WebApplication template.

For this project we are using the WebSite template since this is the customer’s standard. While developing the application, I noticed that when using the WebSite template, the Add Reference works differently then when using the WebApplication template.

When adding a reference to dll A using the WebSite template, Visual Studio automatically adds a reference to all dll’s that dll A references. This is done to ease the deployment but it can give some unwanted side effects.

To make this clearer, I’ve created a simple solution structure that shows this issue. In the image on the left, you can see 4 projects:


  • DalLib

  • BllLib: contains a link to DalLib

  • WebSite: project using the WebSite template; contains a link to BllLib

  • WebApplication: project using the WebApplication template; contains a link to BllLib

When you compile this solution, you will notice that the Visual Studio will add the DalLib to the bin folder of the WebSite project. Because of this, you gain access to all the public classes within DalLib from the WebSite. Since I want to avoid direct access from UI to Dal, this is not what I want. When using the WebApplication template, you don’t have this problem.

A possible solution is to make the classes within the DalLib internal and set an InternalsVisible attribute in the AssemblyInfo.cs file of the DalLib (thanks Gert for the tip).

InternalsVisible is a new attribute from.NET 2.0 and is defined in the System.Runtime.CompilerServices namespace.