Beispiel #1
0
        public void UnloadExtension(string fileName, bool autoInitialize = false)
        {
            string          assemblyName         = Path.GetFileName(fileName);
            ExtensionDomain existExtensionDomain = _extensionDomainList.Find(ed => ed.AssemblyName == assemblyName);

            if (existExtensionDomain == null)
            {
                return;
            }
            existExtensionDomain.Proxy.Uninitialize();
            if (!_isUseSeparateAppdomain)
            {
                existExtensionDomain.IsAbandoned = true;
                Console.WriteLine(
                    $"[Warning - {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}/ExtensionManager]:You can not unload any extension from default appdomain,you may need to enable useSeparateAppdomain to unload extension at runtime.");
            }
            else
            {
                _extensionDomainList.Remove(existExtensionDomain);
                AppDomain.Unload(existExtensionDomain.Domain);
            }
        }
Beispiel #2
0
        public void LoadExtension(string fileName, bool autoInitialize = false)
        {
            if (!File.Exists(fileName))
            {
                return;
            }

            string assemblyName = Path.GetFileName(fileName);

            if (!_isUseSeparateAppdomain && Array.Find(AppDomain.CurrentDomain.GetAssemblies(),
                                                       assembly => assembly.GetName().Name == Path.GetFileNameWithoutExtension(fileName)) != null)
            {
                Console.WriteLine(
                    $"[Warning - {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}/ExtensionManager]:An existing extension has been loaded in default appdomain.");
                return;
            }

            ExtensionDomain existExtensionDomain = _extensionDomainList.Find(ed => ed.AssemblyName == assemblyName);

            if (existExtensionDomain != null && _isUseSeparateAppdomain)
            {
                existExtensionDomain.Proxy.Uninitialize();
                _extensionDomainList.Remove(existExtensionDomain);
            }

            ExtensionProxy proxy;
            AppDomain      ownerDomain;

            if (_isUseSeparateAppdomain)
            {
                Evidence       evidence       = new Evidence();
                AppDomainSetup appDomainSetup = new AppDomainSetup();
                appDomainSetup.ShadowCopyFiles = "true";
                appDomainSetup.ApplicationBase = Environment.CurrentDirectory;
                appDomainSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
                ownerDomain = AppDomain.CreateDomain(fileName, evidence, appDomainSetup);

                Type type = typeof(ExtensionProxy);
                proxy =
                    (ExtensionProxy)ownerDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);
            }
            else
            {
                ownerDomain = AppDomain.CurrentDomain;
                proxy       = new ExtensionProxy();
            }

            proxy.SetEnvironment(ExtensionManager.GetInstance(), EventRouter.GetInstance(), CronTab.GetInstance(),
                                 IOManager.GetDefaultDataStorage(), IOManager.GetRootPath());
            proxy.LoadExtension(fileName);
            _extensionDomainList.Add(new ExtensionDomain()
            {
                Domain       = ownerDomain,
                Proxy        = proxy,
                AssemblyName = assemblyName,
                IsAbandoned  = false
            });
            if (autoInitialize)
            {
                proxy.Initialize();
            }
            if (existExtensionDomain != null && _isUseSeparateAppdomain)
            {
                AppDomain.Unload(existExtensionDomain.Domain);
            }
        }