ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCSI-415Shared Assembly Deploy…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CSI-415

    Advanced Programming

    Progress0 / 55 done
    Topics55
    1Visual Programming Basics2Introduction to Events3Fundamentals of Event-Driven Programming4Message Handling5User Interfaces6Graphics Device Interface7Painting and Drawing8Windows Management9Input Devices10Resources11String and Menu Resource12Dialogs and Windows Controls13Common Controls14Dynamic Link Libraries (DLLs)15Threads and Synchronization16Network Programming17Building Class Libraries at the Command Line18Class Libraries19Using References20Assemblies21Private Assembly Deployment22Shared Assembly Deployment23Configuration Overview24Configuration Files25Programmatic Access to Configuration26Using SDK Tools for Signing and Deployment27Metadata28Reflection29Late Binding30Directories and Files31Serialization32Attributes33Memory Management and Garbage Collection34Threading and Synchronization35Asynchronous Delegates36Application Domains37Marshal by Value38Marshal by Reference39Authentication and Authorization40Configuring Security41Code Access Security42Code Groups43Evidence44Permissions45Role-Based Security46Principals and Identities47Using Data Readers48Using Data Sets49Interacting with XML Data50Tracing Event Logs51Using the Boolean Switch and Trace Switch Classes52Print Debugging Information with the Debug Class53Instrumenting Release Builds with the Trace Class54Using Listeners55Implementing Custom Listeners
    8 min read
    Topic 22 of 55
    Topic Study Note Illustration
    22

    Shared Assembly Deployment

    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 Assembly Deployment in C#

    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.

    1. What is a Shared Assembly?

    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:

    • Global Accessibility: Shared assemblies are available system-wide and can be used by multiple applications.
    • Strongly Named: To be stored in the GAC, an assembly must be strongly named (i.e., signed with a cryptographic key), which ensures that the assembly has a unique identity.
    • Versioning: Shared assemblies allow for versioning, where different applications can reference different versions of the same assembly.

    2. Why Use Shared Assemblies?

    Shared assemblies offer several benefits, particularly when multiple applications need to access the same functionality or code. These benefits include:

    • Code Reusability: Shared assemblies allow multiple applications to reuse the same code, avoiding duplication and reducing the overall size of applications.
    • Memory Efficiency: Only one copy of the shared assembly is loaded into memory, even if multiple applications are using it. This helps optimize system resource usage.
    • Centralized Management: Shared assemblies are managed centrally, making it easier to update or patch common libraries used by multiple applications.
    • Versioning: The GAC allows multiple versions of the same assembly to coexist, ensuring backward compatibility while enabling the use of newer versions when needed.

    3. Global Assembly Cache (GAC)

    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.

    Characteristics of Assemblies in the GAC:

    • Strong Naming: The assembly must be signed with a private key, which gives it a unique identity. This ensures that the assembly can be differentiated from other assemblies with the same name but different versions.
    • Versioning: The GAC allows multiple versions of the same assembly to coexist, ensuring that applications can use specific versions without conflicts.
    • Security: Assemblies in the GAC are typically trusted and validated, providing a layer of security and ensuring that only authenticated assemblies are installed.

    4. Strongly Named Assemblies

    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:

    • Assembly Name
    • Version
    • Culture
    • Public Key Token (used to identify the key used to sign the assembly)

    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.

    How to Strongly Name an Assembly:

    1. Generate a Key Pair: Use the sn.exe (Strong Name) tool to generate a public/private key pair.

      sn -k MyKeyPair.snk
      
    2. Sign the Assembly: Add the key pair to your assembly by specifying it in the AssemblyInfo.cs file.

      [assembly: AssemblyKeyFile("MyKeyPair.snk")]
      
    3. Build the Assembly: After signing the assembly, build it as usual. The resulting assembly will have a strong name.

    4. 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.


    5. Deploying Shared Assemblies to the GAC

    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.

    Steps for Deploying a Shared Assembly:

    1. Compile the Assembly:

      • First, build your assembly (e.g., MyLibrary.dll) and ensure it is strongly named.
    2. Install the Assembly in the GAC:

      • You can use the GacUtil tool to install the assembly into the GAC.

      Example command:

      gacutil -i MyLibrary.dll
      

      This command installs the MyLibrary.dll assembly in the GAC.

    3. Configure Your Application to Use the Assembly:

      • In your application, you can reference the shared assembly just like you would reference any other assembly. However, this time the assembly is not located in the application's directory but in the GAC.

      You can add a reference to the assembly in Visual Studio by:

      • Right-clicking References in Solution Explorer.
      • Selecting Add Reference.
      • Browsing to the Assemblies tab and selecting the assembly from the list of GAC assemblies.
    4. Use the Shared Assembly in Your Application:

      using MyLibrary;
      
      class Program
      {
          static void Main()
          {
              var myClass = new MyClass();
              myClass.DoSomething();
          }
      }
      

    6. Versioning of Shared Assemblies

    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.

    Versioning in the GAC:

    • Each shared assembly in the GAC has a version number, and the version number must be included when referencing the assembly.
    • Applications can specify which version of the assembly they require. If the required version is not available, the application will fail to run.
    • You can use binding redirects in the application's configuration file (e.g., 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.

    Example of Binding Redirect in 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.


    7. Uninstalling Assemblies from the GAC

    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.


    8. Common Issues and Troubleshooting

    • 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.


    9. Summary

    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.

    Key Points:

    • Shared assemblies are stored in the GAC and can be used by multiple applications.
    • Assemblies must be strongly named to be added to the GAC.
    • The GAC supports versioning, allowing multiple versions of the same assembly to exist without conflicts.
    • Binding redirects can be used to manage assembly versioning in an application.
    • The GAC helps with memory efficiency, code reusability, and centralized management.

    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.

    ← Previous topic 21
    Private Assembly Deployment
    Next topic 23 →
    Configuration Overview

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    8mremaining
    1.3kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Advanced Programming