如果你自己的项目用到了基于.net2.0的 Enterprise Library - January 2006,并且你的项目是需要强签名的。这时候你就需要对企业库的代码作少量的修改,让企业库涉及到的项目组件也支持强签名。
修改企业库,让它支持强签名,除了需要把每个项目指定签名文件外,还需要修改每个项目配置的友元程序集(Friend Assemblies)。
一个Assembly中所有的internal类型和internal成员,可以被它的友元程序集(Friend Assemblies)访问。
即如果AssemblyA中声明AssemblyB是它的Friend Assemblies,
则AssemblyB可以像访问自身一样访问AssemblyA的internal类型和internal成员。
但 AssemblyA 中的private类型和private成员仍然不可被AssemblyB访问。
上述设置AssemblyA 是不可以访问AssemblyB的internal类型。因为这个声明是单向的,不是双向的。
以下是对Enterprise Library - January 2006的具体修改步骤:
1、打开VS2005
的Visual Studio 2005 Command Prompt。
2、使用sn.exe 工具,生成一个 snk 文件 [sn -k keyfile.snk]
-k [keysize] outfile
生成一个指定大小的新 RSACryptoServiceProvider 密钥并将其写入指定的文件。公钥和私钥都写入该文件。
如果不指定密钥大小,并且已安装了 Microsoft Enhanced Cryptographic Provider,则默认情况下生成 1,024 位的密钥;否则,生成 512 位的密钥。
3、提取公钥到一个文件
[sn -p keyfile.snk publickey.pk]
-p infile outfile
从 infile 中的密钥对提取公钥并将其存储在 outfile 中。
4、 显示出公钥,这个一定要记住,后面用。
[sn -tp publickey.pk]
-t[p] infile
显示存储在 infile 中的公钥的标记。infile 的内容必须是以前使用 -p 从密钥对文件生成的公钥。不要使用 -t[p] 选项直接从密钥对文件提取该标记。
5、给企业库中,你需要到的项目进行签名,注意项目之间的互相引用,如果搞不清楚这些关系,那就给所有项目配置签名。
对于每个项目,都通过下面步骤来指定签名
a)在Solution Explorer中选择每一个项目,然后在右键中选择属性(项目的属性)
b)属性页中选择“Signing”页,然后选中“Sign the assembly” 选择框。然后在下拉列表框中选择“Browse…”,选择第二步创建的snk文件。
c) 保存并关闭属性页,
整个企业库 54 个项目,如果你一个个的都这么设置的话,比较累。
网上已经有人开发了一个工具,可以来配置多个项目。
这个工具在下述地址下载。
http://spaces.msn.com/misopiniones/blog/cns!2737DC89A4AAB26B!623.entry
6、在整个解决方案中搜索 InternalsVisibleTo,然后把第4步获得的公钥Copy出来。
修改如下代码成类似后面这样的:
[assembly: InternalsVisibleTo("Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Tests")}
[assembly: InternalsVisibleTo("Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Tests,
PublicKey=00240000048000009400000006024*********3D7B62BCB24A160C64F8")]
注意:不是PublicKeyToken,是PublicKey
如果你用PublicKeyToken的话,会报如下错误:
Friend assembly reference 'Microsoft.Practices.EnterpriseLibrary.Common.Tests,PublicKeyToken=b34627b8a52c2536' is invalid. Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations.
7、保存整个解决方案。
8、编译整个解决方案。
参考:
强名称工具 (Sn.exe) 参数介绍
http://msdn2.microsoft.com/zh-cn/library/k5b5tt23.aspx
InternalsVisibleToAttribute,友元程序集访问属性
http://vitoriatang.cnblogs.com/archive/2005/11/22/282201.aspx
http://spaces.msn.com/misopiniones/blog/cns!2737DC89A4AAB26B!624.entry
http://blogs.msdn.com/tomholl/archive/2005/12/02/499529.aspx
打印 | 张贴于 2006-05-16 13:24:00 | Tag:.net 编程心得 Enterprise Library
留言反馈
获得 InternalsVisibleTo 的
PublicKey 的方法如下:
http://blog.csdn.net/ChengZi/archive/2007/05/14/1608159.aspx
One thing you will notice with the InternalsVisibleToAttribute in Visual Studio 2005 is that an assembly's full public key must be now specified instead of the just the token (The help in MDN is wrong.).
That means that the following:
[assembly: InternalsVisibleTo("MyAssembly, PublicKeyToken=B20BF637EDDC8C19")]
must be specified like so:
[assembly: InternalsVisibleTo("MyAssembly, PublicKey=0024000004800000940000000602000000240000525341310004000001000100898f6490fdd62e5fa...")]
Way to retrieve the public key for an assembly.
Simply open up Visual Studio command prompt via the Start menu and type the following (replace MyAssembly.dll with your own assembly):
sn -Tp MyAssembly.dll
This will output the public key of your assembly to the command prompt, ready for pasting into the above attribute.
D:\>type keyfile.snk
...rsa....
看出什么了没有...