In the .NET framework, shared assemblies are used by multiple applications, and they are typically stored in a centralized location known as the Global Assembly Cache (GAC). Shared assemblies are particularly useful for system-wide or cross-application functionality, as they allow multiple applications to share a single instance of the assembly, ensuring consistency and efficient memory usage.
A shared assembly is an assembly that is designed to be used by more than one application. Unlike private assemblies, which are used by a single application and stored in the application's directory, shared assemblies are installed in the Global Assembly Cache (GAC) or a shared location, where multiple applications can reference and use them.
The key characteristics of a shared assembly include:
Shared assemblies offer several benefits, particularly when multiple applications need to access the same functionality or code. These benefits include:
The Global Assembly Cache (GAC) is a central repository used to store shared assemblies. It is located on the file system at the path C:\Windows\assembly or C:\Program Files\Assembly in older versions of .NET. The GAC enables assemblies to be shared across multiple applications, and assemblies in the GAC must be strongly named.
To deploy an assembly to the GAC, it must be strongly named. This means the assembly must have a cryptographic signature, which gives it a unique identity. A strongly named assembly includes:
Strong naming ensures that the assembly is unique and can be verified, which is especially important in shared scenarios to prevent malicious or accidental replacement of assemblies.
Generate a Key Pair: Use the sn.exe (Strong Name) tool to generate a public/private key pair.
sn -k MyKeyPair.snk
Sign the Assembly: Add the key pair to your assembly by specifying it in the AssemblyInfo.cs file.
[assembly: AssemblyKeyFile("MyKeyPair.snk")]
Build the Assembly: After signing the assembly, build it as usual. The resulting assembly will have a strong name.
Install the Assembly in the GAC: To install the strongly named assembly in the GAC, use the gacutil.exe tool.
gacutil -i MyAssembly.dll
This command installs the assembly in the GAC, making it available for shared use.
When deploying shared assemblies, the typical approach is to install them into the GAC, ensuring that they are available to multiple applications across the system.
Compile the Assembly:
MyLibrary.dll) and ensure it is strongly named.Install the Assembly in the GAC:
Example command:
gacutil -i MyLibrary.dll
This command installs the MyLibrary.dll assembly in the GAC.
Configure Your Application to Use the Assembly:
You can add a reference to the assembly in Visual Studio by:
Use the Shared Assembly in Your Application:
using MyLibrary;
class Program
{
static void Main()
{
var myClass = new MyClass();
myClass.DoSomething();
}
}
One of the key features of the GAC is that it allows multiple versions of the same assembly to coexist. This ensures that applications that depend on older versions of an assembly do not break when newer versions are installed.
app.config or web.config) to direct the application to use a particular version of an assembly, even if it was compiled with a different version.app.config:<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="MyLibrary" publicKeyToken="xxxxxxxxxxxxxxxx" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
This tells the application to always use version 1.0.0.0 of MyLibrary.dll, even if it was compiled with an older version.
To remove an assembly from the GAC, you can use the gacutil tool with the /u (uninstall) option.
gacutil -u MyLibrary
This command removes the MyLibrary assembly from the GAC.
Assembly Not Found: If an application cannot find a shared assembly in the GAC, ensure the assembly is installed properly. Verify that the correct version is installed, and check the application's configuration to ensure the correct version is being referenced.
Version Mismatches: When different applications rely on different versions of the same shared assembly, it’s important to manage versioning carefully using binding redirects in the app.config file.
GAC Cache Issues: Sometimes, changes to assemblies in the GAC may not be reflected immediately. Restarting the application or the machine may resolve such issues.
Shared assembly deployment in C# allows multiple applications to access the same code, which simplifies maintenance and improves memory usage. By installing shared assemblies in the Global Assembly Cache (GAC), you ensure that assemblies are centrally managed and that multiple versions can coexist.
Shared assembly deployment is a powerful tool for managing common libraries in a system, and understanding how to deploy and manage shared assemblies is crucial for large-scale applications.
Open this section to load past papers