无法获取.NET Policy DLL将应用程序重定向到较新的程序集

时间:2012-01-31 18:47:17

标签: .net binding gac policyfiles

我在技术支持方面的背景可能导致我在这篇文章中提供了太多的信息,导致没有人有兴趣阅读它,但恕我直言,更多的信息要好于更少。

我缺少一些关于装配绑定和重定向的基本和基本的东西。我正在尝试创建一个策略DLL,以将针对我的程序集的旧版本编译的应用程序重定向到最新版本。我所做的一切似乎都没有用。我已经概述了我在下面采取的步骤。我不使用Visual Studio,而只使用.NET Framework和Windows SDK附带的命令行工具。任何人都可以告诉我为什么,在第11步,当我运行针对我的程序集的v1.0.0版本编译的应用程序时,应用程序没有重定向到在GAC中注册的v2.0.0程序集?

当你按照我所做的去做时,你经常需要注意我执行命令的目录。

我对RTFM答案感到非常满意,如果你只是想指出TFM我应该阅读。

提前致谢, 注册

第一步:创建一个密钥对来强烈命名我的程序集。

c:\stackexchangetest> sn -k stackTest.snk
Microsoft (R) .NET Framework Strong Name Utility  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Key pair written to stackTest.snk

c:\stackexchangetest> sn -p stackTest.snk publicStackTest.snk
Microsoft (R) .NET Framework Strong Name Utility  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Public key written to publicStackTest.snk

c:\stackexchangetest> sn -tp publicStackTest.snk
Microsoft (R) .NET Framework Strong Name Utility  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Public key is
002400000480000094000000060200000024000052534131000400000100010079ba0ab991ceda
d7ba4a767d55889a6cd0eafc9bc15c394620b61c02f4c1112dd9e348cbb4826775c72164ac8552
f9b751b1a1dd71874c04b24221f00f4ee8c2971f3ac8b6b381b26d39cbd3347e773bf08e9af838
6d7f317592951a4985e2c187288d037d5af2f62cce2e4dc700d0d00feb4c095df5d12c9a0908dc
8606cdad

Public key token is 6dfd37060fccec62

第二步:创建版本为v1.0.0

的程序集
c:\stackexchangetest\1.0.0> type StackTest.cs
using System;

namespace stack.exchange.rdomarat
{
    public class stackTest
    {
        public static String getVersion()
        {
            return( "1.0.0" );
        }
    }
}

c:\stackexchangetest\1.0.0> type AssemblyInfo.cs
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("stack.exchange.rdomarat")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("StackExchange")]
[assembly: AssemblyProduct("rdomarat")]
[assembly: AssemblyCopyright("Copyright 2012")]
[assembly: AssemblyTrademark("rdomarat")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("a3d17815-97f5-4e56-803c-06f652a23369")]
[assembly: AssemblyVersion( "1.0.0" )]

c:\stackexchangetest\1.0.0> csc /out:stack.exchange.rdomarat.dll /keyfile:../stackTest.snk /debug /w:4 /t:library AssemblyInfo.cs StackTest.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.5420
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

第三步:为v1.0.0程序集

创建策略DLL
c:\stackexchangetest\1.0.0> type stack.exchange.rdomarat.config
<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="stack.exchange.rdomarat"
                        publicKeyToken="6dfd37060fccec62"
                        culture=neutral
                    />
                <bindingRedirect oldVersion="1.0.0-1.0.0"
                        newVersion="1.0.0"
                    />
            </dependentAssembly>
        </assemblyBinding>
    </runtime> 
</configuration>

c:\stackexchangetest\1.0.0> al.exe /version:1.0.0 /link:stack.exchange.rdomarat.config /out:policy.1.0.stack.exchange.rdomarat.dll /keyfile:../stackTest.snk /description:"Policy file" /title:"Policy file" /company:"StackExchange" /copyright:"Copyright 2012" /trademark:"rdomarat" /product:"rdomarat"
Microsoft (R) Assembly Linker version 9.0.30729.1
Copyright (C) Microsoft Corporation. All rights reserved.

第四步:将两个程序集注册到GAC

c:\stackexchangetest\1.0.0> gacutil -u stack.exchange.rdomarat
Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

No assemblies found matching: stack.exchange.rdomarat
Number of assemblies uninstalled = 0
Number of failures = 0

c:\stackexchangetest\1.0.0> gacutil -u policy.1.0.stack.exchange.rdomarat
Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

No assemblies found matching: policy.1.0.stack.exchange.rdomarat
Number of assemblies uninstalled = 0
Number of failures = 0

c:\stackexchangetest\1.0.0> gacutil -i stack.exchange.rdomarat.dll
Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Assembly successfully added to the cache

c:\stackexchangetest\1.0.0> gacutil -i policy.1.0.stack.exchange.rdomarat.dll
Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Assembly successfully added to the cache

第五步:创建使用新v1.0.0程序集

的应用程序
c:\stackexchangetest\stackapp\1.0.0> type stackApp.cs
using System;
using stack.exchange.rdomarat;

public class StackApp
{
   public static void Main()
   {
       String s1;
       s1 = stackTest.getVersion();
       Console.WriteLine("Hello " + s1);
   }
}

c:\stackexchangetest\stackapp\1.0.0> csc /out:StackApp.exe /target:exe /reference:..\..\1.0.0\stack.exchange.rdomarat.dll StackApp.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.5420
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

c:\stackexchangetest\stackapp\1.0.0> stackapp
Hello 1.0.0

第六步:创建v2.0.0 stack.exchange.rdomarat.dll程序集

c:\stackexchangetest\2.0.0> type StackTest.cs
using System;

namespace stack.exchange.rdomarat
{
    public class stackTest
    {
        public static String getVersion()
        {
            return( "2.0.0" );
        }
    }
}

c:\stackexchangetest\2.0.0> type AssemblyInfo.cs
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("stack.exchange.rdomarat")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("StackExchange")]
[assembly: AssemblyProduct("rdomarat")]
[assembly: AssemblyCopyright("Copyright 2012")]
[assembly: AssemblyTrademark("rdomarat")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("a3d17815-97f5-4e56-803c-06f652a23369")]
[assembly: AssemblyVersion( "2.0.0" )]

c:\stackexchangetest\2.0.0> csc /out:stack.exchange.rdomarat.dll /keyfile:../stackTest.snk /debug /w:4 /t:library AssemblyInfo.cs StackTest.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.5420
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

第七步:为v2.0.0程序集

创建策略DLL
c:\stackexchangetest\2.0.0> type stack.exchange.rdomarat.config
<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="stack.exchange.rdomarat"
                        publicKeyToken="6dfd37060fccec62"
                        culture=neutral
                    />
                <bindingRedirect oldVersion="1.0.0-2.0.0"
                        newVersion="2.0.0"
                    />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

c:\stackexchangetest\2.0.0> al.exe /version:2.0.0 /link:stack.exchange.rdomarat.config /out:policy.2.0.stack.exchange.rdomarat.dll /keyfile:../stackTest.snk /description:"Policy file" /title:"Policy file" /company:"StackExchange" /copyright:"Copyright 2012" /trademark:"rdomarat" /product:"rdomarat"
Microsoft (R) Assembly Linker version 9.0.30729.1
Copyright (C) Microsoft Corporation. All rights reserved.

第八步:将两个v2.0.0程序集注册到GAC

c:\stackexchangetest\2.0.0> gacutil -u policy.2.0.stack.exchange.rdomarat
Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

No assemblies found matching: policy.2.0.stack.exchange.rdomarat
Number of assemblies uninstalled = 0
Number of failures = 0

c:\stackexchangetest\2.0.0> gacutil -i stack.exchange.rdomarat.dll
Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Assembly successfully added to the cache

c:\stackexchangetest\2.0.0> gacutil -i policy.2.0.stack.exchange.rdomarat.dll
Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Assembly successfully added to the cache

第九步:创建一个使用新v2.0.0程序集的应用程序

c:\stackexchangetest\stackapp\2.0.0> type stackApp.cs
using System;
using stack.exchange.rdomarat;

public class StackApp
{
   public static void Main()
   {
       String s1;
       s1 = stackTest.getVersion();
       Console.WriteLine("Hello " + s1);
   }
}

c:\stackexchangetest\stackapp\2.0.0> csc /out:StackApp.exe /target:exe /reference:..\..\2.0.0\stack.exchange.rdomarat.dll StackApp.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.5420
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

c:\stackexchangetest\stackapp\2.0.0> StackApp.exe
Hello 2.0.0

第十步:取消注册GAC中的所有内容,并确认两个应用程序均无法正常运行

c:\stackexchangetest\stackapp\2.0.0> gacutil -u policy.2.0.stack.exchange.rdomarat
Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Assembly: policy.2.0.stack.exchange.rdomarat, Version=2.0.0.0, Culture=neutral,PublicKeyToken=6dfd37060fccec62, processorArchitecture=MSIL
Uninstalled: policy.2.0.stack.exchange.rdomarat, Version=2.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62, processorArchitecture=MSIL
Number of assemblies uninstalled = 1
Number of failures = 0

c:\stackexchangetest\stackapp\2.0.0> gacutil -u policy.1.0.stack.exchange.rdomarat
Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Assembly: policy.1.0.stack.exchange.rdomarat, Version=1.0.0.0, Culture=neutral,PublicKeyToken=6dfd37060fccec62, processorArchitecture=MSIL
Uninstalled: policy.1.0.stack.exchange.rdomarat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62, processorArchitecture=MSIL
Number of assemblies uninstalled = 1
Number of failures = 0

c:\stackexchangetest\stackapp\2.0.0> gacutil -u stack.exchange.rdomarat
Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Assembly: stack.exchange.rdomarat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62, processorArchitecture=MSIL
Uninstalled: stack.exchange.rdomarat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62, processorArchitecture=MSIL

Assembly: stack.exchange.rdomarat, Version=2.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62, processorArchitecture=MSIL
Uninstalled: stack.exchange.rdomarat, Version=2.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62, processorArchitecture=MSIL
Number of assemblies uninstalled = 2
Number of failures = 0

c:\stackexchangetest\stackapp\1.0.0> StackApp.exe
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'stack.exchange.rdomarat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62' or one of its dependencies. The system cannot find the file specified.
File name: 'stack.exchange.rdomarat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62' at StackApp.Main()

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v2.0.50727\mscorwks.dll
Running under executable  c:\StackExchangeTest\StackApp\1.0.0\StackApp.exe
--- A detailed error log follows.

=== Pre-bind state information ===
LOG: User = SYBASE\rdomarat
LOG: DisplayName = stack.exchange.rdomarat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62 (Fully-specified)
LOG: Appbase = file:///C:/Users/rdomarat/Documents/Issues/111205_mlclient_policy/StackExchangeTest/StackApp/1.0.0/
LOG: Initial PrivatePath = NULL 
Calling assembly : StackApp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v2.0.50727\config\machine.config.
LOG: Post-policy reference: stack.exchange.rdomarat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62
LOG: The same bind was seen before, and was failed with hr = 0x80070002.

c:\stackexchangetest\stackapp\2.0.0> StackApp.exe
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'stack.exchange.rdomarat, Version=2.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62' or one of its dependencies. The system cannot find the file specified.
File name: 'stack.exchange.rdomarat, Version=2.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62' at StackApp.Main()

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v2.0.50727\mscorwks.dll
Running under executable  c:\StackExchangeTest\StackApp\2.0.0\StackApp.exe
--- A detailed error log follows.

=== Pre-bind state information ===
LOG: User = SYBASE\rdomarat
LOG: DisplayName = stack.exchange.rdomarat, Version=2.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62 (Fully-specified)
LOG: Appbase = file:///C:/Users/rdomarat/Documents/Issues/111205_mlclient_policy/StackExchangeTest/StackApp/2.0.0/
LOG: Initial PrivatePath = NULL
Calling assembly : StackApp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v2.0.50727\config\machine.config.
LOG: Post-policy reference: stack.exchange.rdomarat, Version=2.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62
LOG: The same bind was seen before, and was failed with hr = 0x80070002.

第十一步:重新注册v2.0.0程序集,两个应用程序现在都应该正常工作

c:\stackexchangetest\2.0.0> gacutil -i stack.exchange.rdomarat.dll
Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Assembly successfully added to the cache

c:\stackexchangetest\2.0.0> gacutil -i policy.2.0.stack.exchange.rdomarat.dll
Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Assembly successfully added to the cache

c:\stackexchangetest\stackapp\2.0.0> StackApp.exe
Hello 2.0.0

c:\stackexchangetest\stackapp\1.0.0> StackApp.exe
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'stack.exchange.rdomarat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62' or one of its dependencies. The system cannot find the file specified.
File name: 'stack.exchange.rdomarat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62' at StackApp.Main()

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v2.0.50727\mscorwks.dll
Running under executable  c:\StackExchangeTest\StackApp\1.0.0\StackApp.exe
--- A detailed error log follows.

=== Pre-bind state information ===
LOG: User = SYBASE\rdomarat
LOG: DisplayName = stack.exchange.rdomarat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62 (Fully-specified)
LOG: Appbase = file:///C:/Users/rdomarat/Documents/Issues/111205_mlclient_policy/StackExchangeTest/StackApp/1.0.0/
LOG: Initial PrivatePath = NULL
Calling assembly : StackApp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v2.0.50727\config\machine.config.
LOG: Post-policy reference: stack.exchange.rdomarat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6dfd37060fccec62
LOG: The same bind was seen before, and was failed with hr = 0x80070002.

2 个答案:

答案 0 :(得分:1)

从第一天开始我的整个问题就是缺少双引号:

培养=中性

我将.config文件更改为

culture =“中立”

一切都像魅力一样。程序集绑定调试outlog日志中的错误表明配置文件的XML中的解析错误本来很方便,可能已经节省了两天。哦,好吧。

答案 1 :(得分:0)

我认为这个问题可能要短得多。

在针对强名称程序集进行编译时,您需要在GAC中注册程序集的确切版本。针对V1.0.0.0程序集编译的应用程序需要注册/安装程序集V1.0.0.0。针对V2.0.0.0程序集编译的应用程序需要注册/安装V2.0.0.0。如果你想运行这两个应用程序,那么你需要安装和注册这两个版本 - 据我所知,没有强烈命名的程序集的例外或替换。

这一点的重点是,为一个版本编写的代码可以继续运行而不用担心如果在将来某个时候更新程序集版本(即:dll hell) - 它将继续运行使用旧版本的程序集,直到开发人员更新基本应用程序以针对较新版本运行。

重定向程序集:

http://msdn.microsoft.com/en-us/library/7wd6ex19%28v=vs.85%29.aspx