成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

關(guān)于C#中動態(tài)加載AppDomain的問題

開發(fā) 后端
本文介紹了一些關(guān)于C#中動態(tài)加載AppDomain的問題的解決辦法。在.NET中出現(xiàn)了一個新的概念:AppDomain——應(yīng)用程序域,所有.NET應(yīng)用程序都需要運行在托管環(huán)境中。

在操作系統(tǒng)中,利用進程可以對正在運行的應(yīng)用程序進行隔離,每個應(yīng)用程序被加載到單獨的進程中,并為其分配虛擬內(nèi)存,進程無法直接訪問物理內(nèi)存,只能通過操作系統(tǒng)將虛擬內(nèi)存映射到物理內(nèi)存中,并保證進程之間的物理內(nèi)存不會重疊,但是進程***的缺點就是效率問題,尤其是進程的切換開銷很大,而進程間不能共享內(nèi)存,所以不可能從一個進程通過傳遞指針給另一個進程。

在.NET中出現(xiàn)了一個新的概念:AppDomain——應(yīng)用程序域,所有.NET應(yīng)用程序都需要運行在托管環(huán)境中,操作系統(tǒng)能提供的只有進程,因此.NET程序需要通過AppDomain這個媒介來運行在進程中,同時使用該incheng提供的內(nèi)存空間,只要是.NET的應(yīng)用都會運行在某個AppDomain中。

當(dāng)我們運行一個.NET應(yīng)用程序或者運行庫宿主時,OS會首先建立一個進程,然后會在進程中加載CLR(這個加載一般是通過調(diào)用_CorExeMain或者_(dá)CorBindToRuntimeEx方法來實現(xiàn)),在加載CLR時會創(chuàng)建一個默認(rèn)的AppDomain,它是CLR的運行單元,程序的Main方法就是在這里執(zhí)行,這個默認(rèn)的AppDomain是唯一且不能被卸載的,當(dāng)該進程消滅時,默認(rèn)AppDomain才會隨之消失。

一個進程中可以有多個AppDomain,且它們直接是相互隔離的,我們的Assembly是不能單獨執(zhí)行的,它必須被加載到某個AppDomain中,要想卸載一個Assembly就只能卸載其AppDomain。

最近在我所參加的一個項目中要實現(xiàn)這樣一個模塊:定制一個作業(yè)管理器,它可以定時的以不同頻率執(zhí)行某些.Net應(yīng)用程序或者存儲過程,這里的頻率可以是僅一次、每天、每周還是每月進行執(zhí)行計劃的實施,對于調(diào)用存儲過程沒什么好說的,但是調(diào)用.Net應(yīng)用程序的時候就需要考慮如下問題:

一旦Assembly被作業(yè)管理器的服務(wù)器調(diào)用,(比如某個執(zhí)行計劃正好要被執(zhí)行了),在調(diào)用之前會將程序集加載到默認(rèn)AppDomain,然后執(zhí)行,這就有個問題,如果我需要做替換或者刪除Assembly等這些操作的時候,由于Assembly已經(jīng)被默認(rèn)AppDomain加載,那么對它的更改肯定是不允許的,它會彈出這樣的錯誤:

錯誤提示

除非你關(guān)掉作業(yè)管理服務(wù)器,然后再操作,顯然這樣做是很不合理的。

并且默認(rèn)AppDomain是不能被卸載的,那么我們該怎么辦呢,我想到的方法是動態(tài)的加載Assembly,新建一個AppDomain,讓Assembly加載到這個新AppDomain中然后執(zhí)行,當(dāng)執(zhí)行完后卸載這個新的AppDomain即可,方法如下:

1、創(chuàng)建程序集加載類AssemblyDynamicLoader,該類用來創(chuàng)建新的AppDomain,并生成用來執(zhí)行.Net程序的RemoteLoader類:

  1.  using System;  
  2.  
  3.     using System.Collections.Generic;  
  4.     using System.Globalization;  
  5.     using System.IO;  
  6.     using System.Reflection;  
  7.     using System.Text;  
  8.     using Ark.Log;  
  9.  
  10.     /// < summary>  
  11.     /// The local loader.  
  12.     /// < /summary>  
  13.     public class AssemblyDynamicLoader  
  14.     {  
  15.   /// < summary>  
  16.   /// The log util.  
  17.   /// < /summary>  
  18.   private static ILog log = LogManager.GetLogger(typeof(AssemblyDynamicLoader));  
  19.  
  20.   /// < summary>  
  21.   /// The new appdomain.  
  22.   /// < /summary>  
  23.   private AppDomain appDomain;  
  24.  
  25.   /// < summary>  
  26.   /// The remote loader.  
  27.   /// < /summary>  
  28.   private RemoteLoader remoteLoader;  
  29.  
  30.   /// < summary>  
  31.   /// Initializes a new instance of the < see cref="LocalLoader"/> class.  
  32.   /// < /summary>  
  33.   public AssemblyDynamicLoader()  
  34.   {  
  35. AppDomainSetup setup = new AppDomainSetup();  
  36. setup.ApplicationName = "ApplicationLoader";  
  37. setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;  
  38. setup.PrivateBinPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "private");  
  39. setup.CachePath = setup.ApplicationBase;  
  40. setup.ShadowCopyFiles = "true";  
  41. setup.ShadowCopyDirectories = setup.ApplicationBase;  
  42.  
  43. this.appDomain = AppDomain.CreateDomain("ApplicationLoaderDomain"null, setup);  
  44. String name = Assembly.GetExecutingAssembly().GetName().FullName;  
  45.  
  46. this.remoteLoader = (RemoteLoader)this.appDomain.CreateInstanceAndUnwrap(name, typeof(RemoteLoader).FullName);  
  47.   }  
  48.  
  49.   /// < summary>  
  50.   /// Invokes the method.  
  51.   /// < /summary>  
  52.   /// < param name="fullName">The full name.< /param>  
  53.   /// < param name="className">Name of the class.< /param>  
  54.   /// < param name="argsInput">The args input.< /param>  
  55.   /// < param name="programName">Name of the program.< /param>  
  56.   /// < returns>The output of excuting.< /returns>  
  57.   public String InvokeMethod(String fullName, String className, String argsInput, String programName)  
  58.   {  
  59. this.remoteLoader.InvokeMethod(fullName, className, argsInput, programName);  
  60. return this.remoteLoader.Output;  
  61.   }  
  62.  
  63.   /// < summary>  
  64.   /// Unloads this instance.  
  65.   /// < /summary>  
  66.   public void Unload()  
  67.   {  
  68. try 
  69. {  
  70.     AppDomain.Unload(this.appDomain);  
  71.     this.appDomain = null;  
  72. }  
  73. catch (CannotUnloadAppDomainException ex)  
  74. {  
  75.     log.Error("To unload assembly error!", ex);  
  76. }  
  77. }  
  78. }  

2、創(chuàng)建RemoteLoader類,它可以在AppDomain中自由穿越,這就需要繼承System.MarshalByRefObject這個抽象類,這里RemoteLoader如果不繼承MarshalByRefObject類則一定會報錯(在不同AppDomain間傳遞對象,該對象必須是可序列化的)。以RemoteLoader類做為代理來調(diào)用待執(zhí)行的.Net程序。

  1. using System;  
  2.  using System.Collections.Generic;  
  3.  using System.Globalization;  
  4.  using System.IO;  
  5.  using System.Reflection;  
  6.  using System.Text;  
  7.  
  8.  /// < summary>  
  9.  /// The Remote loader.  
  10.  /// < /summary>  
  11.  public class RemoteLoader : MarshalByRefObject  
  12.  {  
  13.   /// < summary>  
  14.   /// The assembly we need.  
  15.   /// < /summary>  
  16.   private Assembly assembly = null;  
  17.  
  18.   /// < summary>  
  19.   /// The output.  
  20.   /// < /summary>  
  21.   private String output = String.Empty;  
  22.  
  23.   /// < summary>  
  24.   /// Gets the output.  
  25.   /// < /summary>  
  26.   /// < value>The output.< /value>  
  27.   public String Output  
  28.   {  
  29. get 
  30. {  
  31.  return this.output;  
  32. }  
  33.   }  
  34.  
  35.   /// < summary>  
  36.   /// Invokes the method.  
  37.   /// < /summary>  
  38.   /// < param name="fullName">The full name.< /param>  
  39.   /// < param name="className">Name of the class.< /param>  
  40.   /// < param name="argsInput">The args input.< /param>  
  41.   /// < param name="programName">Name of the program.< /param>  
  42.   public void InvokeMethod(String fullName, String className, String argsInput, String programName)  
  43.   {  
  44. this.assembly = null;  
  45. this.output = String.Empty;  
  46.  
  47. try 
  48. {  
  49.  this.assembly = Assembly.LoadFrom(fullName);  
  50.  
  51.  Type pgmType = null;  
  52.  if (this.assembly != null)  
  53.  {  
  54.   pgmType = this.assembly.GetType(className, truetrue);  
  55.  }  
  56.  else 
  57.  {  
  58.   pgmType = Type.GetType(className, truetrue);  
  59.  }  
  60.  
  61.  Object[] args = RunJob.GetArgs(argsInput);  
  62.  
  63.  BindingFlags defaultBinding = BindingFlags.DeclaredOnly | BindingFlags.Public  
  64. | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase  
  65. | BindingFlags.InvokeMethod | BindingFlags.Static;  
  66.  
  67.  CultureInfo cultureInfo = new CultureInfo("es-ES"false);  
  68.  
  69.  try 
  70.  {  
  71.   MethodInfo methisInfo = RunJob.GetItsMethodInfo(pgmType, defaultBinding, programName);  
  72.   if (methisInfo == null)  
  73.   {  
  74. this.output = "EMethod does not exist!";  
  75.   }  
  76.  
  77.   if (methisInfo.IsStatic)  
  78.   {  
  79. if (methisInfo.GetParameters().Length == 0)  
  80. {  
  81.  if (methisInfo.ReturnType == typeof(void))  
  82.  {  
  83.   pgmType.InvokeMember(programName, defaultBinding, nullnullnull, cultureInfo);  
  84.   this.output = "STo call a method without return value successful.";  
  85.  }  
  86.  else 
  87.  {  
  88.   this.output = (String)pgmType.InvokeMember(programName, defaultBinding, nullnullnull, cultureInfo);  
  89.  }  
  90. }  
  91. else 
  92. {  
  93.  if (methisInfo.ReturnType == typeof(void))  
  94.  {  
  95.   pgmType.InvokeMember(programName, defaultBinding, nullnull, args, cultureInfo);  
  96.   this.output = "STo call a method without return value successful.";  
  97.  }  
  98.  else 
  99.  {  
  100.   this.output = (String)pgmType.InvokeMember(programName, defaultBinding, nullnull, args, cultureInfo);  
  101.  }  
  102. }  
  103.   }  
  104.   else 
  105.   {  
  106. if (methisInfo.GetParameters().Length == 0)  
  107. {  
  108.  object pgmClass = Activator.CreateInstance(pgmType);  
  109.  
  110.  if (methisInfo.ReturnType == typeof(void))  
  111.  {  
  112.   pgmType.InvokeMember(programName, defaultBinding, null, pgmClass, null, cultureInfo);  
  113.   this.output = "STo call a method without return value successful.";  
  114.  }  
  115.  else 
  116.  {  
  117.   this.output = (String)pgmType.InvokeMember(programName, defaultBinding, null, pgmClass, null, cultureInfo);//'ymtpgm' is program's name and the return value of it must be started with 'O'.  
  118.  }  
  119. }  
  120. else 
  121. {  
  122.  object pgmClass = Activator.CreateInstance(pgmType);  
  123.  
  124.  if (methisInfo.ReturnType == typeof(void))  
  125.  {  
  126.   pgmType.InvokeMember(programName, defaultBinding, null, pgmClass, args, cultureInfo);  
  127.   this.output = "STo call a method without return value successful.";  
  128.  }  
  129.  else 
  130.  {  
  131.   this.output = (String)pgmType.InvokeMember(programName, defaultBinding, null, pgmClass, args, cultureInfo);//'ymtpgm' is program's name and the return value of it must be started with 'O'.  
  132.  }  
  133. }  
  134.   }  
  135.  }  
  136.  catch 
  137.  {  
  138.   this.output = (String)pgmType.InvokeMember(programName, defaultBinding, nullnullnull, cultureInfo);  
  139.  }  
  140. }  
  141. catch (Exception e)  
  142. {  
  143.  this.output = "E" + e.Message;  
  144. }  
  145. }  
  146. }   

其中的InvokeMethod方法只要提供Assembly的全名、類的全名、待執(zhí)行方法的輸入?yún)?shù)和其全名就可以執(zhí)行該方法,該方法可以是帶參數(shù)或不帶參數(shù),靜態(tài)的或者不是靜態(tài)的。

***這樣使用這兩個類:

  1. AssemblyDynamicLoader loader = new AssemblyDynamicLoader();  
  2. String output = loader.InvokeMethod("fileName""ymtcla""yjoinp""ymtpgm");  
  3.  loader.Unload();  

【編輯推薦】

  1. 淺談C#泛型的用處
  2. 淺談C#如何實現(xiàn)多繼承
  3. C#語言與Java語言程序的比較
  4. 利用C#指針進行圖像操作
  5. C#中用鼠標(biāo)移動頁面功能的實現(xiàn)
責(zé)任編輯:yangsai 來源: 博客園
相關(guān)推薦

2009-08-28 16:14:26

C#實現(xiàn)加載動態(tài)庫

2009-07-31 14:47:22

JavaScript函C#

2009-02-05 15:32:23

接口委托

2009-02-03 09:33:26

動態(tài)類型動態(tài)編程C# 4.0

2009-08-11 14:26:56

C#動態(tài)調(diào)用WebSe

2021-02-06 10:27:45

C#函數(shù)參數(shù)

2022-01-14 07:56:39

C#動態(tài)查詢

2009-09-02 10:58:02

C#動態(tài)數(shù)組

2009-08-12 16:01:32

C#動態(tài)改變數(shù)據(jù)

2009-08-24 16:11:35

C#項目開發(fā)

2010-06-01 13:32:15

Visual Stud

2011-06-09 09:08:00

C#循環(huán)結(jié)構(gòu)

2009-09-17 18:07:22

C#動態(tài)數(shù)組

2009-09-02 11:02:57

C#動態(tài)數(shù)組

2012-12-26 09:31:44

C#Winform

2009-08-17 17:08:47

C#轉(zhuǎn)義

2009-09-17 17:40:36

C#動態(tài)數(shù)組

2009-09-17 17:44:51

C#動態(tài)數(shù)組

2009-09-02 11:18:10

C#動態(tài)數(shù)組

2009-08-27 16:29:18

C#動態(tài)編譯
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 国产一区二区在线91 | 香蕉大人久久国产成人av | 国产精品一区二区在线 | 二区精品 | 国产在视频一区二区三区吞精 | 午夜在线视频 | 91性高湖久久久久久久久_久久99 | 91免费看片| 国产女人精品视频 | 亚洲午夜网 | 毛片毛片毛片毛片 | 激情小说综合网 | 波多野结衣二区 | 69热视频在线观看 | 干干干操操操 | 日韩欧美亚洲综合 | 欧美阿v | 国产精品视频一二三区 | 久久91| 久草在线免费资源 | 日韩三级视频 | 亚洲性人人天天夜夜摸 | 夜夜操天天操 | 亚洲国产精品久久 | 美日韩一区二区 | 亚洲精品久久久9婷婷中文字幕 | 毛片网在线观看 | 一区二区三区四区不卡视频 | 精品久久99| 在线免费观看成人 | 久久成人免费 | 国产精品污www在线观看 | 中文字幕专区 | 成人午夜精品 | 91精品久久久久久久久中文字幕 | 成人激情视频在线播放 | 成人av鲁丝片一区二区小说 | 国产成人aⅴ | 免费观看毛片 | 男女爱爱福利视频 | 日韩在线视频一区二区三区 |