Redirecting Assembly versions to a different CLR/G

2019-06-09 20:13发布

问题:

I have a question regarding the 2 CLR versions, i.e. version 2 and version 4 of the .NET framework, which use different GAC locations. I have a client application built which references an assembly “X” from the v2 GAC (C:\Windows\Assembly). I am now updating the assembly “X” to run on v4 of the .NET framework (C:\Windows\Microsoft.NET\assembly), however, I do not want to recompile the client application. Note, assembly "X" is removed from the v2 GAC before installing the to v4 GAC.

Is it possible to create a publisher policy file which redirects an assembly which used to reside in version 2 of the CLR to version 4 of the CLR? If so, how is this achieved?

I have searched the MSDN, and understand there is an appliesTo field on the assemblyBinding element where you can specify the version of the .NET framework, but this seems to encompass the whole bind.

What I would like is something like:

<bindingRedirect oldVersion="1.0.0.0" .Net 2 newVersion="2.0.0.0" .Net 4/>

I have read here http://msdn.microsoft.com/en-us/magazine/dd727509.aspx that CLR v2.0 applications now cannot see CLR v4.0 assemblies in the GAC. However, you can force an app to use the updated CLR using:

<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" />

So would a mixture of this plus the publisher policy suffice, or is there another way?

回答1:

The determining factor here is which version of the CLR your client app is running under. If it's v2 then there's nothing you can do in order to make v4 assemblies usable or even visible to it. If it does run under the v4 CLR then it would automatically try to resolve assembly references in the v4 GAC first.

The good news is, there is not need for you to recompile your client app in order to achieve that. You can simply force it to run under .Net 4 by adding this to the app's config file:

<configuration>
  <startup>
    <supportedRuntime version="v4.0" />
  </startup>
</configuration>

This should give you what you wanted.