.NET applications often depend on a large number of other assemblies. As a consequence, bugs in a .NET application are often caused by bugs in dependent components, not the application itself. Instead of recompiling and redeploying an application when a bug-fixed version of a dependent component is available, you can just redeploy the bug-fixed component and provide a configuration so that the assembly resolver looks for the new version of a strongly named assembly.
Configurations that cause a different version to be loaded are called version redirections. Version redirections can be defined at three different levels as follows:
• The application level
• The machine level
• The assembly level (via publisher policy assemblies)
The following application configuration file shows a typical version redirection at the application level:
<!-- MyApplication.exe.config --> <configuration> <runtime>
<!-- CLR specific settings go here --> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="SampleLib" publicKeyToken="65d6f5d1b5d4890e" /> <bindingRedirect oldVersion="0.0.0.0-184.108.40.206" newVersion="220.127.116.11" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
This configuration file tells the assembly resolver to load the assembly SampleLib, Version =18.104.22.168, Culture=neutral, PublicKeyToken=65d6f5d1b5d4890e when any version between 0.0.0.0 and 22.214.171.124 is requested.
For machine-wide redirections, you must modify a file named machine.config, which can be found in the directory %frameworkdir%\%frameworkversion%\config, where %framewrokdir% and %frameworkversion% should be replaced with the content of the environment variables frameworkdir and frameworkversion.
If you have implemented a .NET assembly that is used by many applications, you will likely prefer version redirections via publisher policy assemblies over application configuration files and machine-wide configurations. Both application and machine configurations would require modifications of configuration files on each of the client machines.
To simplify versioning of libraries, a library developer can deploy two assemblies: the new version of the library, and an assembly that explicitly states that the new version is backward compatible with a set of older versions of the library. Such an assembly is called a publisher policy assembly.
When a publisher policy assembly is installed in the GAC, the assembly resolver will load the new version of the assembly when a compatible older version is requested.
To create a publisher policy assembly, you first have to create a configuration file that specifies the version redirection. This configuration file has the same format as the application configuration file shown previously.
Using this configuration file and the key file that was used to sign your library, a tool called assembly linker (AL.EXE) can produce a publisher policy assembly. Depending on the target platform of the assembly and the compilation model you have used, different command-line options are needed. The following samples assume that the assembly MyLibrary, which was signed with the key file keyfile.snk, should be configured with a configuration file named MyLibrary.dll.config.
For assemblies built with /clr:safe, you should use the following command line:
al /link:MyLibrary.dll.config /platform:anycpu /out:policy.0.0.myLibrary.dll
If the assembly is built with /clr or /clr:pure, the command-line options depend on the target platform. For assemblies that depend on the 32-bit CLR, the command line is as follows:
al /link:MyLibrary.dll.config /platform:x86 /out:policy.0.0.myLibrary.dll /keyfile:keyfile.snk
For assemblies that depend on the AMD64 CLR, you should use the following command line instead:
al /link:MyLibrary.dll.config /platform:x64 /out:policy.0.0.myLibrary.dll /keyfile:keyfile.snk
To apply the publisher policy assembly, you just have to create a deployment package that installs both the new version of the library and the publisher policy assembly. While the publisher policy assembly must be installed in the GAC, the new version can be installed at any location that allows the application to find and load the assembly.
Was this article helpful?