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

關于C#中動態加載AppDomain的問題

開發 后端
本文介紹了一些關于C#中動態加載AppDomain的問題的解決辦法。在.NET中出現了一個新的概念:AppDomain——應用程序域,所有.NET應用程序都需要運行在托管環境中。

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

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

當我們運行一個.NET應用程序或者運行庫宿主時,OS會首先建立一個進程,然后會在進程中加載CLR(這個加載一般是通過調用_CorExeMain或者_CorBindToRuntimeEx方法來實現),在加載CLR時會創建一個默認的AppDomain,它是CLR的運行單元,程序的Main方法就是在這里執行,這個默認的AppDomain是唯一且不能被卸載的,當該進程消滅時,默認AppDomain才會隨之消失。

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

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

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

錯誤提示

除非你關掉作業管理服務器,然后再操作,顯然這樣做是很不合理的。

并且默認AppDomain是不能被卸載的,那么我們該怎么辦呢,我想到的方法是動態的加載Assembly,新建一個AppDomain,讓Assembly加載到這個新AppDomain中然后執行,當執行完后卸載這個新的AppDomain即可,方法如下:

1、創建程序集加載類AssemblyDynamicLoader,該類用來創建新的AppDomain,并生成用來執行.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、創建RemoteLoader類,它可以在AppDomain中自由穿越,這就需要繼承System.MarshalByRefObject這個抽象類,這里RemoteLoader如果不繼承MarshalByRefObject類則一定會報錯(在不同AppDomain間傳遞對象,該對象必須是可序列化的)。以RemoteLoader類做為代理來調用待執行的.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的全名、類的全名、待執行方法的輸入參數和其全名就可以執行該方法,該方法可以是帶參數或不帶參數,靜態的或者不是靜態的。

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

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

【編輯推薦】

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

2009-08-28 16:14:26

C#實現加載動態庫

2009-07-31 14:47:22

JavaScript函C#

2009-02-05 15:32:23

接口委托

2009-02-03 09:33:26

動態類型動態編程C# 4.0

2009-08-11 14:26:56

C#動態調用WebSe

2021-02-06 10:27:45

C#函數參數

2022-01-14 07:56:39

C#動態查詢

2009-09-02 10:58:02

C#動態數組

2009-08-24 16:11:35

C#項目開發

2009-08-12 16:01:32

C#動態改變數據

2010-06-01 13:32:15

Visual Stud

2011-06-09 09:08:00

C#循環結構

2009-09-17 18:07:22

C#動態數組

2009-09-02 11:02:57

C#動態數組

2012-12-26 09:31:44

C#Winform

2009-08-17 17:08:47

C#轉義

2009-08-27 16:29:18

C#動態編譯

2009-09-17 18:14:05

C#動態數組

2009-09-17 17:40:36

C#動態數組

2009-09-17 17:44:51

C#動態數組
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 成人欧美一区二区三区黑人孕妇 | 国产欧美一区二区精品久导航 | 国产精品自拍视频 | 午夜合集 | 国产观看 | 国产1区2区3区 | 伊人伊成久久人综合网站 | 国产在线不卡 | 亚洲午夜在线 | 日韩精品一区二区三区中文在线 | 成人精品毛片国产亚洲av十九禁 | 成人午夜免费视频 | 成人精品鲁一区一区二区 | 国产男女视频网站 | 欧美成人激情 | 在线观看黄色大片 | 伦理二区 | 精品一区二区电影 | 99精品一级欧美片免费播放 | 一区二区三区免费网站 | 美女久久 | 草久久免费视频 | 日韩中文字幕在线 | 日韩免费视频 | 欧美成人久久 | 黄色毛片免费看 | 天天搞天天操 | 91麻豆精品一区二区三区 | 久久久久久综合 | 日韩在线免费播放 | 亚洲一区二区三区福利 | 亚州毛片 | 亚洲国产精品久久久 | 精品av| 精品伦精品一区二区三区视频 | 久久精品国产亚洲a | 成年人免费看的视频 | 色眯眯视频在线观看 | 国产一区二区三区四区五区加勒比 | 国产免费高清 | 高清av在线|