ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Advanced Programming
    CSI-415
    Progress0 / 55 topics
    Topics
    1. Visual Programming Basics2. Introduction to Events3. Fundamentals of Event-Driven Programming4. Message Handling5. User Interfaces6. Graphics Device Interface7. Painting and Drawing8. Windows Management9. Input Devices10. Resources11. String and Menu Resource12. Dialogs and Windows Controls13. Common Controls14. Dynamic Link Libraries (DLLs)15. Threads and Synchronization16. Network Programming17. Building Class Libraries at the Command Line18. Class Libraries19. Using References20. Assemblies21. Private Assembly Deployment22. Shared Assembly Deployment23. Configuration Overview24. Configuration Files25. Programmatic Access to Configuration26. Using SDK Tools for Signing and Deployment27. Metadata28. Reflection29. Late Binding30. Directories and Files31. Serialization32. Attributes33. Memory Management and Garbage Collection34. Threading and Synchronization35. Asynchronous Delegates36. Application Domains37. Marshal by Value38. Marshal by Reference39. Authentication and Authorization40. Configuring Security41. Code Access Security42. Code Groups43. Evidence44. Permissions45. Role-Based Security46. Principals and Identities47. Using Data Readers48. Using Data Sets49. Interacting with XML Data50. Tracing Event Logs51. Using the Boolean Switch and Trace Switch Classes52. Print Debugging Information with the Debug Class53. Instrumenting Release Builds with the Trace Class54. Using Listeners55. Implementing Custom Listeners
    CSI-415›Shared Assembly Deployment
    Advanced ProgrammingTopic 22 of 55

    Shared Assembly Deployment

    8 minread
    1,325words
    Intermediatelevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time8 min
      Word count1,325
      Code examples0
      DifficultyIntermediate