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.execommand-line utility to create a public key pair for use in signing your control:sn.exe -k Blog.snk
Add the
AssemblyKeyFileAttributeto 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.exeutility, as follows:gacutil -i Blog.dll
Tip
Note that as with the
csc.exeandvbc.exe ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access