蝈蝈俊.net

-- 用随笔来记录自己的技术感触
随笔 - 673, 评论 - 4416, 引用 - 276

导航

关于

记录自己的技术心得

标签

每月存档

最新留言

  • Syffkefi
    Gloomy tales <a href=" http://www.wikio.com/user/ybaquqeun/bio ">pic...
    by Syffkefi(匿名) on 2010/3/21 20:31:00
  • Ymipnkir
    i'm fine good work <a href=" http://www.wikio.com/user/irucilury/bio "&...
    by Ymipnkir(匿名) on 2010/3/21 20:30:44
  • Cnmevoyw
    Excellent work, Nice Design <a href=" http://www.wikio.com/user/qelytoiniq/bio &...
    by Cnmevoyw(匿名) on 2010/3/21 20:30:31
  • Gncuydae
    Thanks funny site <a href=" http://www.wikio.com/user/myaedet/bio ">...
    by Gncuydae(匿名) on 2010/3/21 19:43:40
  • Inmtkkwr
    perfect design thanks <a href=" http://www.wikio.com/user/arutybany/bio "&a...
    by Inmtkkwr(匿名) on 2010/3/21 19:43:30
  • Iumrwzpe
    this post is fantastic <a href=" http://www.wikio.com/user/riuneefyy/bio "&...
    by Iumrwzpe(匿名) on 2010/3/21 19:43:19
  • carbozyne
    uRTyRW How are you? and <a href="http://stress-relief-fda.viviti.com"&g...
    by carbozyne(匿名) on 2010/3/21 19:13:37
  • Murrimhv
    Excellent work, Nice Design <a href=" http://www.wikio.com/user/arutybany/bio &q...
    by Murrimhv(匿名) on 2010/3/21 18:23:56
  • Ujyntzwf
    Hello good day <a href=" http://www.wikio.com/user/fogoido/bio ">ear...
    by Ujyntzwf(匿名) on 2010/3/21 18:23:51
  • Mthziksa
    This site is crazy :) <a href=" http://www.wikio.com/user/pebyhylaga/bio "&...
    by Mthziksa(匿名) on 2010/3/21 18:23:46

广告

 

如果你自己的项目用到了基于.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

留言反馈

#回复: 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.



2007-08-08 09:51:00 | [匿名:ghj1976]
#re: Enterprise Library 强签名的问题 编辑

D:\>type keyfile.snk
...rsa....

看出什么了没有...
2006-07-09 18:49:00 | [匿名:chating]
#re: Enterprise Library 强签名的问题 编辑
...
2006-05-28 20:04:00 | [匿名:rtyw4et]
#re: Enterprise Library 强签名的问题 编辑
...
2006-05-20 18:22:00 | [匿名:e]
#re: Enterprise Library 强签名的问题 编辑
..
2006-05-20 18:22:00 | [匿名:e]
#re: Enterprise Library 强签名的问题 编辑
...
2006-05-19 13:23:00 | [匿名:e]
#re: Enterprise Library 强签名的问题 编辑
好技巧,一直在找一个方法,让自己写出的程序集中的类只有自己能用,别人不能用。这次有了。每次其它程序集调用只能把方法写成public,别人也能用了。
2006-05-17 13:18:00 | [匿名:笑望人生]
#re: Enterprise Library 强签名的问题 编辑
原来也遇到过同样的问题,我的解决办法是修改源码文件,而不修改Test工程文件,然后编译一下,生成强名称的程序集
2006-05-16 19:31:00 | [匿名:阿不]
对不起,目前本随笔不允许发表新评论.

Powered by: Joycode.MVC引擎 0.5.2.0