GAC程序集版本基于Web.Config使用

时间:2011-05-12 08:15:09

标签: .net asp.net gac

美好的一天

我有一个在GAC中使用自定义程序集的项目: 为了能够使用它,我在

中添加了对我的项目的引用
C:\WINDOWS\Microsoft.NET\assembly\GAC_MSIL\JOHN.CommonLib\v4.0_1.0.0.0__9cd884563ebafb62\JOHN.CommonLib.dll

(CopyLocal = False; SpecificVersion = False) 另外,我在Web.Config文件中添加了这个

<compilation debug="false" strict="true" explicit="true" targetFramework="4.0" >
  <assemblies>
    <add assembly="JOHN.CommonLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9cd884563ebafb62"/>        
  </assemblies >
</compilation >

它按预期运行。问题是当我安装新的版本时 我在GAC上安装了新版本,并相应地更改了Web.Config

<add assembly="JOHN.CommonLib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=9cd884563ebafb62"/>

JOHN.CommonLib是一个测试人员类库,根据版本返回“1.0”或“2.0”。

问题:如果我使用1.0编译,使用它的webapps总是显示“1.0”,即使我在1.0和2.0之间更改Web.Config 我希望我的网络应用程序使用我在Web.Config中编写的版本

有什么想法吗? 我也在更改Web.Config之间停止并启动AppPool。

1 个答案:

答案 0 :(得分:3)

对于强命名程序集 - 应用程序将始终绑定(如果可能)到已构建的版本。要覆盖此绑定,您需要为程序集指定绑定重定向。有多种方法可以做到这一点 - 请参阅此link。因此,使用app / web配置文件的方法之一 - 例如,

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="JOHN.CommonLib"
          publicKeyToken="9cd884563ebafb62"
          culture="en-us" />
        <!-- Assembly versions can be redirected in application, 
          publisher policy, or machine configuration files. -->
        <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>