Wednesday, August 10, 2016

Global Assembly Cache

Assemblies
  • A collection of single-file or multiple files (exe or dll), generate when compile successfully.
  • It can create application without disturb other (different version).
  • Composed by manifest, type metadata, MSIL, resources.
  • Strong name assemblies will have public key token.
  • Shared assemblies is GAC, and Private assemblies
Manifest 
  • Contain identification information.
  • Version, Name, Culture, Security Requirements.
GAC
  • Shared Assemblies at 1 location for all multiple .NET application.
  • Using GAC, we no need copy local physical file to every projects, it like system assembly(using System;)
  • It not suitable to xcopy deployment.
  • Why Imporant (Shared Location, Mutiple Versioning, File Security)
Step 1: Generate public key token
sn.exe -k c:\MyStrongKeys.snk (Developer Command Prompt)

Step 2: Add the Strong Key to Class library AssemblyInfo (Class > Properties > AssemblyInfo)
[assembly: AssemblyKeyFile("C:\\MyStrongKeys.snk")]

Step 3: Add the assembly to GAC
cd C:\Users\oby\Documents\visual studio 2015\Projects\Training\GACClassLibrary\bin\Debug
gacutil.exe -i GACClassLibrary.dll

.NET 4.0 > will at C:\Windows\Microsoft.NET\assembly\GAC_MSIL
.NET 4.0 < will at C:\Windows\assembly

Step 4: How to Call GAC

Specific Version and Key in code
Assembly SampleAssembly1 = Assembly.Load("GACClassLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=30e23367dbe9711c");          

Specific Version and Key in WebConfig
Assembly SampleAssembly1 = Assembly.Load("GACClassLibrary");

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="GACClassLibrary" publicKeyToken="30e23367dbe9711c" Culture="neutral" />
        <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

Excecute It
 MethodInfo methodInfo1 = SampleAssembly1.GetTypes()[0].GetMethod("Main");



No comments:

Post a Comment