程序集强命名与GAC
1、新建項目Other,在其中創(chuàng)建如下類:
using System;
using System.Collections.Generic;
using System.Text;
namespace Other.全局應(yīng)用緩存
{
??? public class GAC
???? {
???????? public string CallGAC()
???????? {
???????????? return "GAC end";
???????? }
???? }
}
2、強(qiáng)命名程序集:
在項目Other上,擊右鍵,簽名,為程序集簽名,新建或者瀏覽密鑰文件
也可以在SDK 命令提示中創(chuàng)建密鑰,sn -k D:\CompanyA.keys,在此引用。vs2003:[assembly: AssemblyKeyFile
("D:\CompanyA.keys")]
編譯,此時Other.dll已經(jīng)是強(qiáng)命名程序集
3、共享程序集Other.dll:
將程序集(如:E:\個人文件夾\Code\企業(yè)級應(yīng)用解決方案\Other\bin\Debug\Other.dll)直接拖入全局應(yīng)用程序緩存(如:
C:\WINDOWS\assembly)
也可是在SDK 命令提示中完成,如:GACUtil /i E:\個人文件夾\Code\企業(yè)級應(yīng)用解決方案\Other\bin\Debug\Other.dll
4、調(diào)用共享程序集Other.dll
新建Web項目MyWebProject,添加引用剛才創(chuàng)建的程序集Other.dll,比如:E:\個人文件夾\Code\企業(yè)級應(yīng)用解決方案
\Other\bin\Debug\Other.dll,此時MyWebProject的web.config中有了:
?? <compilation debug="true">
??? <assemblies>
???? <add assembly="Other, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=43FC64574884C304"/></assemblies></compilation></system.web>
調(diào)用代碼如下:
public partial class _Default : System.Web.UI.Page
{
???? protected void Page_Load(object sender, EventArgs e)
???? {
???? }
???? protected void Button1_Click(object sender, EventArgs e)
???? {
???????? //使用反射,不需要引用
???????? System.Reflection.Assembly assembly = System.Reflection.Assembly.Load("Other, Version=1.0.0.0, Culture=neutral, PublicKeyToken=43FC64574884C304");
???????? this.Response.Write("利用反射,從GAC中載入程序集" + assembly.GlobalAssemblyCache);
???? }
???? protected void Button2_Click(object sender, EventArgs e)
???? {
???????? //必須引用,網(wǎng)站MyWebProject部署之后,可以刪除調(diào)E:\個人文件夾\Code\企業(yè)級應(yīng)用解決方案
\Other\bin\Debug\Other.dll
???????? Other.全局應(yīng)用緩存.GAC gac = new Other.全局應(yīng)用緩存.GAC();
???????? string result = gac.CallGAC();
???????? this.Response.Write(result);
???? }
}
總結(jié)
以上是生活随笔為你收集整理的程序集强命名与GAC的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeArt WSS3.0(MOSS)
- 下一篇: 允许交互式登陆