Target non specific version of an assembly

两盒软妹~` 提交于 2019-11-28 13:53:36

In general, if you are trying to use a specific version of an assembly the below doesn't really apply, you should just use the version you need.

However, sometimes you can run into a situation where you have this:

AssemblyX - references version 1.2.1 of AssemblyZ
AssemblyY - references version 1.2.2 of AssemblyZ

But your project needs both AssemblyX and AssemblyY.

So how do you resolve this? You can either put 1.2.1 and 1.2.2 of AssemblyZ in the GAC, or, if you're sure there aren't any compatibility issues, you can use assembly rebinding. Here's an example (this goes in your Web.config or App.config file):

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly>
            <assemblyIdentity name="myAssembly"
                              publicKeyToken="32ab4ba45e0a69a1"
                              culture="neutral" />
            <bindingRedirect oldVersion="1.0.0.0"
                             newVersion="2.0.0.0"/>
         </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

What this basically says is that if any assemblies in your solution reference 1.0.0.0 of myAssembly, then they should really use version 2.0.0.0. And you're expected to have version 2.0.0.0 present in the path.

A hack you can use when you always want them to use a specific version of the assembly is to specify a version range, like this:

        <dependentAssembly>
            <assemblyIdentity name="MyAssembly" publicKeyToken="B7567367622062C6" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="1.2.1.0" />
        </dependentAssembly>

This will force version 1.2.1.0 of MyAssembly to be used for any version reference of MyAssembly between 0.0.0.0 and 3.0.0.0.

the only ways around this issue I found so far are:

1) ILMerge the dll with the dependency I have 2) provide the the dependency (in this case a dll) 3) not signong the assembly , tho this would be quite hard, cause i ll need to "unsign" the hierachy

You can have both dlls and can use bindingRedirect as in below link

dll versioning in dotnet

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly>
            <assemblyIdentity name="AssemblyName"
                              publicKeyToken="32ab4ba45e0a69a1"
                              culture="neutral" />
            <bindingRedirect oldVersion="1.0.0.0"
                             newVersion="2.0.0.0"/>
         </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

While the code example for App.Config below is correct you might wish to keep GAC policy from overriding your desire by adding the following publisherPolicy apply="no" in either the assemblyBinding tree which will cause all assemblys to ignore the GAC publisher policy or you may place the publisherPolicy apply="no" /> in the dependentAssembly tree and this will by pass publisherPolicy for that assembly only.

   { <configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <publisherPolicy apply="no" /> // This applies to the whole application - all references
         <dependentAssembly>
         <publisherPolicy apply="no" /> // This applies only to this assembly
            <assemblyIdentity name="AssemblyName"
                              publicKeyToken="32ab4ba45e0a69a1"
                              culture="neutral" />
            <bindingRedirect oldVersion="1.0.0.0"
                             newVersion="2.0.0.0"/>
         </dependentAssembly>
      </assemblyBinding>}

For more info see: MSDN reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!