Gacutil和环境中的次要版本差异引起了严重的问题

4

这是情况:

我有一个编译在 .NET 2 中的 DLL,放置在全局程序集缓存中。我有一个名为 myDLL.dll.config 的配置文件,它包含以下内容:

<configuration>
   <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="myDLL" publicKeyToken="c426c33bab532523" />
        <bindingRedirect oldVersion="1.2.2.0-1.2.2.22" newVersion="1.2.2.23" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

我使用汇编链接器创建策略文件:
al /link:myDLL.dll.config /out:policy.1.2.myDLL.dll /keyfile:myDLL.snk

但是当我尝试使用以下方法将策略文件加载到全局程序集缓存中时:

gacutil -i policy.1.2.myDLL.dll

I get the error:

Failure adding assembly to the cache:  This assembly is built by a 
runtime newer than the currently loaded runtime and cannot be loaded.  

.NET 全局程序集缓存实用工具版本为 2.0.50727.42,但我通过创建一个新的 C# .NET 2 控制台项目并执行以下命令来检查构建环境中的版本:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MessageBox.Show(Environment.Version.ToString());
        }
    }
}

我的Environment.Version是2.0.50727.5466。

这一切都发生在同一台计算机上,具体来说就是我的开发工作站。

我可以将myDLL.dll从\bin\release文件夹复制到c:\Windows\assembly文件夹中,没有问题。但是尝试将policy.1.2.myDLL.dll文件从\bin\release复制粘贴到\assembly中,或者使用gacutil都失败了。


请点击以下链接查看有关编程的内容:http://social.msdn.microsoft.com/Forums/en-US/biztalkgeneral/thread/e76f5c46-e679-4923-bf9d-1f883074eb2e/ - Justin Pihony
你确定你正在使用al的.NET 2.x版本吗? - Tim B
@TimB 我不确定。我打开了一个新的Visual Studio 2010命令提示符,输入了“al”,然后得到了Assembly Linker version 10.0.30319.1。当涉及到在全局程序集缓存中使用dll时,我有点新手。 - CurtisHx
2
你应该在%PROGRAMFILES(X86)%\Microsoft SDKs\Windows\v7.0A\Bin和%PROGRAMFILES(X86)%\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools中都有一个al.exe。它们都具有相同的版本号,但前者适用于.NET 2.x,后者适用于.NET 4.x。我建议指定完整路径以确保使用正确的版本。 - Tim B
1
@TimB,你解决了我的问题。现在我得去向老板解释为什么我浪费了半天时间在这个该死的东西上。 - CurtisHx
1个回答

6
问题在于策略程序集是为.NET 4.x编译的。
在%PROGRAMFILES(X86)%\Microsoft SDKs\Windows\v7.0A\Bin中有一个al.exe,另一个在%PROGRAMFILES(X86)%\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools中也有一个。它们的版本号相同,但前者适用于.NET 2.x,后者适用于.NET 4.x。您应该指定完整路径以确保使用正确的版本。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接