|
Related Reading
ASP.NET in a Nutshell |
In this final installment in a series of excerpts from ASP.NET in a Nutshell, learn how to share ASP.NET controls across applications.
The architecture of the .NET Framework makes using a custom server control or other assembly as simple as copying that assembly to the bin subdirectory of your application and adding the appropriate directives and tags to your page. However, there may be times when you would like multiple applications on the same machine to be able to use the same control, without having multiple local copies of the control's assembly floating around.
Fortunately, .NET addresses this need with the Global Assembly Cache (GAC), a repository of shared assemblies that are accessible to all .NET applications on a given machine. Adding your own control assemblies to the GAC is a relatively straightforward process that requires four steps:
sn.exe -k Blog.snk
AssemblyKeyFileAttribute to the file containing the
control code, passing the path to the keyfile created in Step 1 as an argument.
(This is an assembly-level attribute, so it should be placed outside of any
namespace or class definitions.) When compiled, this attribute will result in a
strongly named assembly that can be placed in the GAC:
[assembly: AssemblyKeyFileAttribute("Blog.snk")]
gacutil -i Blog.dll
TIP: Note that as with the csc.exe and vbc.exe command-line compilers, using the sn.exe and gacutil.exe utilities without a fully qualified path requires that you have the path to these utilities registered as part of your
PATHenvironment variable. The sn.exe and gacutil.exe utilities are typically located in the \FrameworkSDK\bin directory, which is installed either under ProgramFiles ->Microsoft.NET or ProgramFiles ->MicrosoftVisualStudio.NET, depending on whether you've installed just the .NET Framework SDK or Visual Studio .NET.
|
In This Series
User Controls and Custom Server Controls, Part 3
User Controls and Custom Server Controls, Part 2
User Controls and Custom Server Controls, Part 1 |
Once you've added the control assembly to the GAC, you can now
use it from any application on the machine. One caveat: to use custom controls
that are installed in the GAC, you must supply the version, culture, and public
key information for the assembly when adding the @
Register directive for the control, as shown in the following snippet
(which should appear on a single line):
<%@ Register TagPrefix="aspnetian"
Namespace="aspnetian" Assembly="Blog,
Version=0.0.0.0, Culture=neutral,
PublicKeyToken=6bd31f35fc9a113b" %>
If you've added your control to the Visual Studio .NET toolbox,
when you use the control from the toolbox, the correct @
Register directive will be generated for you automatically.
The following sites provide additional information on the topics discussed in this chapter:
Matthew MacDonald is a developer, author, and educator in all things Visual Basic and .NET. He's worked with Visual Basic and ASP since their initial versions, and written over a dozen books on the subject, including The Book of VB .NET (No Starch Press) and Visual Basic 2005: A Developer's Notebook (O'Reilly). His web site is http://www.prosetech.com/.
G. Andrew Duthie is the founder and principal of Graymad Enterprises, Inc., which provides training and consulting in Microsoft Web development technologies.
Back to: ASP.NET in a Nutshell
Copyright © 2009 O'Reilly Media, Inc.