This example will use the following command line utilities and versions:
- csc - C# compiler (Version 3.5.30729.4926)
- sn - .Strong Name Utility (Version 3.5.30729.1)
- gacutil - Global Assembly Cache Tool (Version 3.5.30729.1)
- al - assembly linker (Version 8.00.50727.42)
- fuslogvw - Assembly Binding Log Viewer for viewing the CLR binding process.
- Assembly Cache Viewer - For viewing assemblies installed in the GAC.
To get started lets create a program calling a method in another assembly which outputs the callee's assembly version. These steps also create assemblies for use in the later examples.
- Open a Visual Studio Command Prompt and navigate to a working directory where some files can be created.
- Create and save a file named demo.cs containing the following text:
using System;using System.Reflection;
[assembly: AssemblyVersionAttribute("2.0.0.0")]
public static class Demo{public static void Main(){Console.WriteLine("Version " + Assembly.GetExecutingAssembly().GetName().Version.ToString());}} - from a command prompt type "sn -k key.snk"
- type "csc /target:library /keyfile:key.snk demo.cs"
- type "echo f | xcopy demo.dll .\bin\v2\demo.dll"
- Change the AssemblyVersionAttribute in demo.cs replacing "2.0" with "3.0"
- csc /target:library /keyfile:key.snk demo.cs
- echo f | xcopy demo.dll .\bin\v3\demo.dll
- Change the AssemblyVersionAttribute in demo.cs replacing "3.0" with "4.0"
- csc /target:library /keyfile:key.snk demo.cs
- echo f | xcopy demo.dll .\bin\v4\demo.dll
- Change the AssemblyVersionAttribute in demo.cs replacing "4.0" with "1.0"
- csc /target:libarary /keyfile:key.snk demo.cs
- echo f | xcopy demo.dll .\bin\v1\demo.dll
- Create and save a file named demo.cs containing the following text:
public static class Program{public static void Main(){Demo.Main();}} - csc /reference:demo.dll program.cs
- program.exe
- The output should read: Version 1.0.0.0
Redirecting 1.0 to 2.0 version in the GAC.
- gacutil /i .\bin\v2\demo.dll
- sn -T demo.dll
- Note the Public key token as we'll need that in the assemblyIdentity publicKeyToken attribute in the next step.
- Create program.exe.config with the following text:
<?xml version="1.0"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="demo" publicKeyToken="keyFromSn-T" />
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration> - program.exe
- The output should read: Version 2.0.0.0
Redirecting 1.0 to Version 3.0 using a URL:
- Replace the program.exe.config text with the following:
<?xml version="1.0"?><configuration><runtime><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="demo" publicKeyToken="keyFromSn-T" /><bindingRedirect oldVersion="1.0.0.0" newVersion="3.0.0.0"/><!--to use an absolute filepath use href="file://D:/Projects/...." or it can point to a web resource: href="http://www.fake.com/demo.dll"--><codeBase version="3.0.0.0" href="./bin/v3/demo.dll" /></dependentAssembly></assemblyBinding></runtime></configuration> - program.exe
- The output should read: Version 3.0.0.0
Redirecting 1.0 to Version 4.0 in the GAC using a publisher policy.
- Create a policy.config file in the working directory with this text:
<?xml version="1.0"?><configuration><runtime><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="demo" publicKeyToken="keyFromSn-T" /><bindingRedirect oldVersion="1.0.0.0" newVersion="4.0.0.0" /></dependentAssembly></assemblyBinding></runtime></configuration> - al /link:policy.config /out:policy.1.0.demo.dll /keyfile:key.snk
- gacutil /i policy.1.0.demo.dll
- gacutil /i .\bin\v4\demo.dll
- delete the program.exe.config in the working directory or erase the bindingredirect element. The bindingredirects seem to be applied first and will break this example.
- program.exe
- The output should read: Version 4.0.0.0