zoukankan      html  css  js  c++  java
  • AppDomainManager

    namespace Practices.Services.AppDomainManagers
    {
        public class AppDomainManager:ServiceBase
        {
            #region Private Members
            private readonly Dictionary<string,AppDomain> m_AppDomains = new Dictionary<string, AppDomain>();
            private readonly Dictionary<string, AppDomainEventNotify> m_AppDomainEventNotifys = new Dictionary<string, AppDomainEventNotify>();
            private readonly Dictionary<string, AppDomainEventListener> m_AppDomainEventListeners = new Dictionary<string, AppDomainEventListener>();
            #endregion

            #region Public Members
            public AppDomain CreateAppDomain(string appDomainName, string baseDirectory)
            {
                if (!m_AppDomains.ContainsKey(appDomainName))
                {
                    AppDomainSetup appDomainSetup = new AppDomainSetup
                                                        {
                                                            ApplicationName = appDomainName,
                                                            ApplicationBase = baseDirectory,
                                                            ShadowCopyFiles = "true",
                                                            ShadowCopyDirectories = baseDirectory,
                                                            ConfigurationFile =
                                                                AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
                                                        };
                    AppDomain workerDomain = AppDomain.CreateDomain(appDomainName,null,appDomainSetup);
     
                    const string typeString = "Practices.Service.AppDomainManager.AppDomainEventNotify,Practices.Service.AppDomainManager";
                    AppDomainEventNotify appDomainEventNotify =
                        TypeResolver.CreateInstance("", typeString, workerDomain, null) as AppDomainEventNotify;

                    AppDomainEventListener appDomainEventListener = new AppDomainEventListener(this, appDomainEventNotify);

                    m_AppDomains.Add(appDomainName, workerDomain);
                    m_AppDomainEventNotifys.Add(appDomainName, appDomainEventNotify);
                    m_AppDomainEventListeners.Add(appDomainName, appDomainEventListener);
                }
                return m_AppDomains[appDomainName];
            }

            public void UnloadAppDomain(string appDomainName)
            {
                if (m_AppDomains.ContainsKey(appDomainName))
                {
                    AppDomain.Unload(m_AppDomains[appDomainName]);
                    m_AppDomains.Remove(appDomainName);
                    m_AppDomainEventNotifys.Remove(appDomainName);
                    m_AppDomainEventListeners.Remove(appDomainName);
                }
            }
            #endregion

            #region Internal Methods
            internal void OnDomainUnload(object sender, EventArgs e)
            {
                AppDomain appDomain = sender as AppDomain;
                if (appDomain != null)
                    Logger.WriteLog("Test1", "DomainUnload in " + appDomain.Id + ", " + appDomain.FriendlyName);
            }

            internal void OnAssemblyLoad(object sender, AssemblyLoadEventArgs args)
            {
                AppDomain appDomain = sender as AppDomain;
                if (appDomain != null)
                    Logger.WriteLog("Test1", "AssemblyLoad in " + appDomain.Id + ", " + appDomain.FriendlyName);
            }

            internal void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                AppDomain appDomain = sender as AppDomain;
                if (appDomain != null)
                    Logger.WriteLog("Test1", "UnhandledException in " + appDomain.Id + ", " + appDomain.FriendlyName);
            }

            #endregion
        }
    }

    Helper:

    namespace Practices.Services.AppDomainManagers
    {
        public class AppDomainEventListener:ServiceBase
        {
            #region Private Members
            private readonly AppDomainManager m_AppDomainManager;
            private readonly AppDomainEventNotify m_AppDomainEventNotify;
            #endregion

            #region Public Constructor
            public AppDomainEventListener(AppDomainManager appDomainManager,AppDomainEventNotify appDomainEventNotify)
            {
                m_AppDomainManager = appDomainManager;
                m_AppDomainEventNotify = appDomainEventNotify;
                m_AppDomainEventNotify.RegisterEventListener(this);
            }
            #endregion

            #region Public Methods
            public void OnDomainUnload(object sender, EventArgs e)
            {
                m_AppDomainManager.OnDomainUnload(sender,e);
            }

            public void OnAssemblyLoad(object sender, AssemblyLoadEventArgs e)
            {
                m_AppDomainManager.OnAssemblyLoad(sender, e);
            }

            public void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                m_AppDomainManager.OnUnhandledException(sender, e);
            }
            #endregion
        }
    }

    namespace Practices.Services.AppDomainManagers
    {
        public class AppDomainEventNotify:ServiceBase
        {
            #region Private Members
            private readonly List<AppDomainEventListener> m_AppDomainEventListeners = new List<AppDomainEventListener>();
            #endregion

            #region Public Constructor
            public AppDomainEventNotify()
            {
                AppDomain.CurrentDomain.DomainUnload += CurrentDomain_DomainUnload;
                AppDomain.CurrentDomain.AssemblyLoad += CurrentDomain_AssemblyLoad;
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            }
            #endregion

            #region Public Method
            public void RegisterEventListener(AppDomainEventListener appDomainEventListener)
            {
                m_AppDomainEventListeners.Add(appDomainEventListener);
            }
            #endregion

            #region Private Events
            private void CurrentDomain_DomainUnload(object sender, EventArgs e)
            {
                foreach (AppDomainEventListener appDomainEventListener in m_AppDomainEventListeners)
                {
                    appDomainEventListener.OnDomainUnload(sender,e);
                }
            }

            private void CurrentDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs e)
            {
                foreach (AppDomainEventListener appDomainEventListener in m_AppDomainEventListeners)
                {
                    appDomainEventListener.OnAssemblyLoad(sender, e);
                }
            }

            private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                foreach (AppDomainEventListener appDomainEventListener in m_AppDomainEventListeners)
                {
                    appDomainEventListener.OnUnhandledException(sender, e);
                }
            }

            #endregion
        }
    }

  • 相关阅读:
    Ubuntu 16.04 安装 Wireshark分析tcpdump的pcap包——sudo apt install wireshark-qt
    tcpdump dns流量监控
    ubuntu dig timeout解决方法,dnscat执行失败也是这个原因
    dns tunnel C&C
    “借贷宝”到底是不是坑?——“借贷宝”注册送现金营销模式分析?【已亲测可以无条件提现成功】
    借贷宝注册提现详细攻略:注册送20元,邀请好友再各送20元,亲测可无条件提现(附提现、到账截图)
    滴滴专车司机苹果手机ios客户端可以下载了
    优步UBER司机全国各地奖励政策汇总:北京、上海、广州、深圳、佛山、天津、南京、武汉、成都、重庆、济南、西安、宁波、青岛、长沙、苏州
    北京优步UBER司机B组最新奖励政策、高峰翻倍奖励、行程奖励、金牌司机奖励【每周更新】
    优步UBER司机奖励政策:含高峰、翻倍、行程、金牌司机、保底奖励(持续更新...)
  • 原文地址:https://www.cnblogs.com/zanxiaofeng/p/1809031.html
Copyright © 2011-2022 走看看