在同个应用程序中不关闭程序实现动态加载同个DLL多次,且DLL内容有更新,程序不会认为是同个DLL

在同个应用程序中不关闭程序实现动态加载同个DLL多次,且DLL内容有更新,程序不会认为是同个DLL

最近做个项目,需要挂载DLL,在DLL更新后不能立刻关闭旧的DLL,因为DLL是一系列的WCF服务。在这种情况下就需要实现动态挂载和卸载DLL,但是一个应用程序在不关闭的情况下只能加载同个DLL一次。

入正题:

private void GetSevList(string dllPath, string sevUri)
{
  //创建程序域
  AppDomainSetup setup = new AppDomainSetup();   ProxyObject po = new ProxyObject();   setup.LoaderOptimization = LoaderOptimization.SingleDomain;   setup.ApplicationName = "Test";   setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;   setup.PrivateBinPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "private");   setup.CachePath = setup.ApplicationBase;   setup.ShadowCopyFiles = "true";   setup.ShadowCopyDirectories = setup.ApplicationBase;
  //使用指定的名称、证据和应用程序域设置信息创建新的应用程序域。(sevUri 是一个文本值 不重复)   AppDomain domain = AppDomain.CreateDomain(sevUri.Split('/')[sevUri.Split('/').Length - 1], null, setup);
  string name = Assembly.GetExecutingAssembly().GetName().FullName;
  //加载DLL(这个就是需要多次加载的那个DLL)   Assembly MyAssembly = po.LoadAssembly(dllPath);   po = (ProxyObject)domain.CreateInstanceAndUnwrap(name, typeof(ProxyObject).FullName);
  
  
  Assembly[] abs = AppDomain.CurrentDomain.GetAssemblies();
}

class ProxyObject : MarshalByRefObject
{
  Assembly assembly = null;
  public Assembly LoadAssembly(string dllPath)
  {
    assembly = Assembly.LoadFile(dllPath);
    return assembly;
  }
 
  public string FullName
  {
    get { return assembly.FullName; }
  }   
}
------------付截图--------------


转载说明出处
原文地址:https://www.cnblogs.com/weivyuan/p/2738919.html