2

I'm trying to implement a (conditional) binding redirect. I'd like binding redirect to use a particular version of an assembly depending if another, newer version already exists.

For clarity, the project currently references version 1.1.0.0, but if that does not exist, I want it to reference version 1.0.0.0.

This redirect currently redirects successfully to 1.0.0.0, but environments with only 1.1.0.0 do not function properly (assembly name and publicKeyToken have been changed for this post).

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="MyAssembly" culture="neutral" publicKeyToken="blahblahblah"/>
            <bindingRedirect oldVersion="1.1.0.0" newVersion="1.0.0.0"/>
            <codeBase version="1.0.0.0" href="file://C:\Windows\assembly\GAC_MSIL\MyAssembly"/>
    </dependentAssembly>
</assemblyBinding>

How can I cancel/ignore this redirect in the case that it is not needed?

tnw
  • 13,521
  • 15
  • 70
  • 111
  • @vcsjones Would you mind clarifying? Maybe I'm misunderstanding the point of binding redirects. – tnw Dec 05 '12 at 19:48
  • 1
    Lets say I have a 3rd party assembly, Y, that depends on version 1.0 of another 3rd party assembly, X; and both are strongly named. Now I wanted to use version 1.1 of X. If I upgrade X, I will have broken Y because it expects 1.0. With a binding redirect, I could tell it to use 1.1 even though it was compiled against 1.0. – vcsjones Dec 05 '12 at 19:51
  • @vcsjones Okay that makes much more sense. Thank you for the simple explanation. Is there a way I can dynamically change a project reference at runtime? – tnw Dec 05 '12 at 19:59
  • Not to my knowledge (it may be possible, but if it is, I don't know it) – vcsjones Dec 05 '12 at 20:03
  • @vcsjones I pulled up [this post](http://stackoverflow.com/questions/4410258/c-sharp-visual-studio-adding-references-programmatically). I'll try it and see how it pans out. – tnw Dec 05 '12 at 20:04

1 Answers1

0

I'm afraid it can't be done at runtime, but you can do it on build if it's environment-specific. On build pipeline, you can check if the assembly x with version 1.1 exists, and if it does, update the configuration file to use that version.

Andrew Chaa
  • 6,120
  • 2
  • 45
  • 33