Today I had a closer look at the "Specific Version" property of assembly references in Visual Studio 2010. After a few experiments with unexpected results I set out to learn as much as possible about how the property works. Even SO, it appears to me, does not have all the answers, so here is my attempt at self-answering the question:
How exactly does the "Specific Version" property of an assembly reference work in Visual Studio?
When you add a reference then Visual Studio records the [AssemblyVersion] of the assembly in the project file. This is important. If you, say, create a bug fix a year later then you want to make sure that you rebuild the project with the exact same version of the reference so it is a true drop-in. You'll get an error if the reference assembly has changed.
But that isn't always desirable. Some programmers let the assembly version automatically increment, generating a new version every single time they rebuild. Even though the public interface of the assembly never changed. Some configure their project by using Nuget to obtain libraries and let it automatically update the library when there's a new release available. They will like to set the Specific Version property to False to suppress the compile error.
Pretty important to understand the consequence, you do need to redeploy the entire build of the program to avoid accidents. Version mismatches at runtime crash the program and can only be suppressed with a
<bindingRedirect>
in the .config file which is risky.It's a compile-time property!
One of the most important things to know is that "Specific Version" is a property that takes effect at compile-time and not at runtime.
What is it all about?
When a project is built, the project's assembly references need to be resolved in order to find the physical assemblies that the build system should use. If the "Specific Version" check is performed (see section "When is "Specific Version" checked?"), it affects the outcome of the assembly resolution process:
Order in which assemblies are resolved
The order in which the assembly resolution process locates potential assemblies appears to be this:
<HintPath>
element in the .csproj fileNote that if several versions of the assembly exist in the GAC, the resolution process first attempts to resolve to the assembly with the highest version. This is important only if the "Specific Version" check is not made.
When is "Specific Version" checked?
Visual Studio bases its decision whether to perform the "Specific Version" check on two pieces of information found in the .csproj file:
<SpecificVersion>
element, and its value (if it is present)This is how a typical assembly reference with version information looks like:
And this is how the assembly reference looks like without version information:
The following table shows when the "Specific Version" check is performed, and when it is not.
The surprising thing here is that no check is performed if both
<SpecificVersion>
and version information are absent (case 6). I would have expected the check to be performed and to always fail (same as case 2) because in my understanding the absence of<SpecificVersion>
implies the default value "True". This may be a quirk of Visual Studio 2010 where I did my tests.When you examine the properties of an assembly reference in the Visual Studio UI (select the reference and hit F4), the value you see for the "Specific Version" property tells you whether or not Visual Studio is going to perform the "Specific Version" check. In case 6 the UI will show "True", although the
<SpecificVersion>
element is not present in the .csproj file.Side-effects on "Copy local"
If the "Copy Local" property is set to "True" but the assembly resolution process fails because of the "Specific Version" check, no assembly is copied.
Reference material