白天有雨

此处应该有掌声

导航

程序集强命名与GAC

1、新建项目Other,在其中创建如下类:
using System;
using System.Collections.Generic;
using System.Text;

namespace Other.全局应用缓存
{
    public class GAC
     {
         public string CallGAC()
         {
             return "GAC end";
         }
     }
}

2、强命名程序集:
在项目Other上,击右键,签名,为程序集签名,新建或者浏览密钥文件
也可以在SDK 命令提示中创建密钥,sn -k D:\CompanyA.keys,在此引用。vs2003:[assembly: AssemblyKeyFile

("D:\CompanyA.keys")]
编译,此时Other.dll已经是强命名程序集

3、共享程序集Other.dll:
将程序集(如:E:\个人文件夹\Code\企业级应用解决方案\Other\bin\Debug\Other.dll)直接拖入全局应用程序缓存(如:

C:\WINDOWS\assembly)
也可是在SDK 命令提示中完成,如:GACUtil /i E:\个人文件夹\Code\企业级应用解决方案\Other\bin\Debug\Other.dll

4、调用共享程序集Other.dll
新建Web项目MyWebProject,添加引用刚才创建的程序集Other.dll,比如:E:\个人文件夹\Code\企业级应用解决方案

\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>
调用代码如下:

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)
     {
         //必须引用,网站MyWebProject部署之后,可以删除调E:\个人文件夹\Code\企业级应用解决方案

\Other\bin\Debug\Other.dll
         Other.全局应用缓存.GAC gac = new Other.全局应用缓存.GAC();
         string result = gac.CallGAC();
         this.Response.Write(result);
     }
}

posted on 2008-01-26 17:26  卓如  阅读(637)  评论(0编辑  收藏  举报