代码之家  ›  专栏  ›  技术社区  ›  roundcrisis

目标程序集的非特定版本

  •  4
  • roundcrisis  · 技术社区  · 16 年前

    FileLoadException: Could not load file or assembly 
    

    当引用的dll的版本与当前版本不完全匹配时,就会发生这种情况。 我认为,问题在于如何提及大会。

    4 回复  |  直到 16 年前
        1
  •  3
  •   dcp    16 年前

    一般来说,如果您试图使用程序集的特定版本,下面的说明实际上并不适用,您应该只使用所需的版本。

    然而,有时你会遇到这样的情况:

    AssemblyX-引用AssemblyZ的版本1.2.1

    但是你的项目需要两者 装配。

    那你怎么解决这个问题呢?您可以将AssemblyZ的1.2.1和1.2.2放在GAC中,或者,如果您确定没有任何兼容性问题,可以使用程序集重新绑定。下面是一个例子(在你的 Web.config App.config 文件):

    <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>
    

    这基本上是说,如果解决方案中的任何程序集引用myAssembly的1.0.0.0,那么它们应该真正使用版本2.0.0.0。您希望路径中有版本2.0.0.0。

    当您总是希望他们使用程序集的特定版本时,可以使用的一种方法是指定版本范围,如下所示:

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

    这将强制MyAssembly的1.2.1.0版本用于0.0.0.0和3.0.0.0之间的任何MyAssembly版本引用。

        2
  •  0
  •   roundcrisis    16 年前

    到目前为止,我发现解决这个问题的唯一方法是:

    2) 提供依赖项(在本例中为dll)

        3
  •  0
  •   somesh    12 年前

    您可以拥有这两个DLL,也可以使用bindingRedirect,如下链接所示

    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>
    
        4
  •  0
  •   Ken    12 年前

    虽然下面App.Config的代码示例是正确的,但您可能希望通过在assemblyBinding树中添加以下publisherPolicy apply=“no”(这将导致所有程序集忽略GAC publisher策略)或将publisherPolicy apply=“no”/>在dependentAssembly树中,这将只传递该程序集的publisherPolicy。

       { <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>}
    

    有关更多信息,请参阅: MSDN reference