As mentioned in Gary's blog Dependent Assembly Binding Redirect (by version), it is possible to use a configuration file to change which version of an assembly to use. This can be very helpful in "tight versioning" scenarios (see blog Proxy Versioning Options for Project Templates ) where a specific version of a proxy is used. Here are some considerations for this:
In order to use a binding redirect the reference must have the SpecificVersion property set to True.
< Reference Include = "ShapeAppCSharpProxy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b2943f7cfe902115, processorArchitecture=MSIL" >
< SpecificVersion > True</SpecificVersion >
</ Reference >
Generally the binding redirect can be specified in the application level configuration file AppName.exe.config (see Gary's blog ); however, there is a known bug with this for VSTA v 1 add-ins after Visual Studio 2008 SP1 is installed. The workaround is to specify the binding redirect in the machine level configuration file Machine.config. This file can be updated through the Microsoft .NET Framework 2.0 Configuration utility or programmatically with admin privileges- see code below.
private void EnsureMachineConfig()
{
//open the config file
Configuration MachineConfig = ConfigurationManager.OpenMachineConfiguration();
//Ge thte runtime section
ConfigurationSection runtimeSection = MachineConfig.Sections["runtime"]; //case sensitive
string content = runtimeSection.SectionInformation.GetRawXml();
//check if the proxy is already being re-directed
if (!content.Contains("ShapeAppCSharpProxy"))
{
//add the entry
content = content.Replace(
"</assemblyBinding>",
"<dependentAssembly>" +
"<assemblyIdentity name=\"ShapeAppCSharpProxy\" culture=\"Neutral\" publicKeyToken=\"b2943f7cfe902115\"/>" +
"<bindingRedirect oldVersion=\"1.0.0.0\" newVersion=\"2.0.0.0\"/>"+
"</dependentAssembly>"+
"</assemblyBinding>");
//save the changes
runtimeSection.SectionInformation.SetRawXml(content);
MachineConfig.Save();
}
}
For VSTA v 2 Visual Basic add-ins only one version of a proxy can be registered on a machine at any time due to the unique name requirment(see blog Proxy Assembly Registration- Unique Name Required ). For C# add-ins multiple versions of the same proxy can coexist.
Posted
Jul 27 2010, 11:21 AM
by
Melody