6.3 Sharing 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:
Use the sn.exe command-line utility to create a
public key pair for use in signing your control: sn.exe -k Blog.snk Add the 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")] Recompile the control. Add the control to the GAC, either by dragging and dropping the
assembly in Windows Explorer or by using the
gacutil.exe utility, as follows: gacutil -i Blog.dll |
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
PATH environment 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\Microsoft Visual Studio .NET
2003\SDK\v1.1\Bin, depending on whether
you've installed just the .NET Framework SDK or
Visual Studio .NET.
|
|
Once you've added the
control assembly to the GAC, you can 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.
|