8

Context

I have 2 different versions of an assembly installed in GAC, version 1.0 and version 2.0. I made an application that is referencing 1.0 as a specific version.

Issue

When I run my application, it will always load version 2.0 whereas the application is specifically referencing version 1.0.

Question

Why is this happening? How can I force my application to load the version it has been compiled for?

It does not seem to me that this has anything to do with a binding redirect as my application was not even aware of version 2.0 when I built it and that the reference "Specific Version" metadata is set to true.

Thanks.


Edit:

The assembly I am referencing is actually Oracle.DataAccess from the ODAC package. I noticed that other assemblies named Policy.x.xxx.Oracle.DataAccess where published in GAC.


Edit 2:

After looking into the Oracle.DataAccess policy I found the configuration defining the binding redirect:

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="Oracle.DataAccess" publicKeyToken="89B483F429C47342"/>
            <bindingRedirect oldVersion="4.112.0.0-4.112.3.0" newVersion="4.112.3.0"/>
        </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

Even though I added the reversed binding redirect into my application configuration, the policy in GAC seems to have the priority. I found an MSDN article treating the subject and suggesting to ignore policy with this configuration:

<publisherPolicy apply="no" />

But it still does not work...


Edit 3:

I tried to remove the policy from the GAC and rebooted my machine. It finally worked. It does not feel like a confortable solution development but this policy does broke one of my application which means disabling the policy is the right thing to do in my case.


Final Edit:

Igor gave me the right answer. All I had to do to workaround those policies was to use the publisherPolicy setting in the right configruation section:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="Oracle.DataAccess" publicKeyToken="89B483F429C47342"/>
      <publisherPolicy apply="no"/>
    </dependentAssembly>
  </assemblyBinding>
</runtime>
Ucodia
  • 7,410
  • 11
  • 47
  • 81
  • Did you check that there was no publisher policy for your referenced assembly? – Igor Korkhov May 22 '12 at 11:23
  • @IgorKorkhov: Yes I found something called Policy.x.xxx.MyAssemblyName in the GAC. I was not aware of such a mechanism. I edit my question and you can submit a reply. Thanks. – Ucodia May 22 '12 at 11:44
  • Tangential question: What is the problem that makes you want to use V1? I use Oracle occasionally, so forewarned is forearmed :) – Ian May 22 '12 at 12:25
  • @Ian: The application was built with version 1.0 and version 2.0 breaks the applicaton. – Ucodia May 22 '12 at 12:29

1 Answers1

7

After you have edited your question it becomes clear that this is the policy file which affects assembly binding.

In case of Oracle there's a file called Policy.X.Y.Oracle.DataAccess.config with the contents similar to this:

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="Oracle.DataAccess" publicKeyToken="89B483F429C47342"/>
            <bindingRedirect oldVersion="10.1.0.000-10.2.0.100" newVersion="10.2.0.100"/>
        </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

The policy is installed by the Oracle Installer and redirects Oracle.DataAccess.dll to the latest version, as Oracle believes the library is backward compatible.

EDIT: If you don't want publisher policy to be applied for a particular assembly, put the element in the element:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
        <assemblyIdentity name="myAssembly" publicKeyToken="..."  culture="en-us" />
        <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
            <publisherPolicy apply="no" />
    </dependentAssembly>
</assemblyBinding>
Igor Korkhov
  • 8,283
  • 1
  • 26
  • 31
  • I tried with `` but it did not resolved the issue. I read [here](http://msdn.microsoft.com/en-us/library/7wd6ex19.aspx) that I could use `` to ignore policies but still the same issue... – Ucodia May 22 '12 at 12:05
  • @Ucodia: that's strange. Are you referencing v4.112.3.0 in your app? Maybe you should try including a range of versions (say, `0.0.0.0-4.65535.65535.65535`) to see if this will work? – Igor Korkhov May 22 '12 at 12:17
  • I specifically reference 4.112.2.1. I updated my question with some new findings. It looks like the policy published in GAC overrides any application configuration. – Ucodia May 22 '12 at 12:22
  • @Ucodia: indeed, the policy does override application configuration, but `` inside `` should still work. – Igor Korkhov May 22 '12 at 13:06
  • It worked with policy enabled! I was actually defining this setting in the wrong configuration section, thus the setting was not used. Thank you very much for your valuable help. You should update your answer then :) – Ucodia May 22 '12 at 13:48
  • @Ucodia: I updated my answer and am glad it finally works without deleting the policy file (which is overkill in your scenario, I think). – Igor Korkhov May 22 '12 at 14:03