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
        }
    }

  • 相关阅读:
    zoj-3433-Gu Jian Qi Tan
    优先队列详解(转载)
    HDU-3661-Assignments
    hdu-1052-Tian Ji -- The Horse Racing(经典)
    POJ-1456-Supermarket
    UVA-11292Dragon of Loowater
    UVA-11729-Commando War
    循环日程表 问题(递归分治)
    八数码问题
    POJ-3273 Monthly Expense (最大值最小化问题)
  • 原文地址:https://www.cnblogs.com/zanxiaofeng/p/1809031.html
Copyright © 2011-2022 走看看