Beispiel #1
0
        internal PluginDomain(string name, AppDomainSetup appDomainSetup)
        {
            if (appDomainSetup == null)
            {
                throw new ArgumentNullException("appDomainSetup");
            }
            _identity = (name + _count++);
            appDomainSetup.ApplicationName = appDomainSetup.ApplicationName ?? "PluginDomain" + _identity;

            _appDomain = AppDomain.CreateDomain(_identity, null, appDomainSetup);

            _resolver = new PluginAssemblyResolver();
            _resolver.AddPath(appDomainSetup.ApplicationBase);
            _resolver.AddPath(appDomainSetup.PrivateBinPath);
            _resolver.SetAlternatePathResolver(ResolveFromThisDomain);
            AppDomain.CurrentDomain.AssemblyResolve += _resolver.Resolve;

            _proxyResolver = new Proxy <PluginAssemblyResolver>(this);

            ((PluginAssemblyResolver)_proxyResolver).AddPath(appDomainSetup.ApplicationBase);
            ((PluginAssemblyResolver)_proxyResolver).AddPath(appDomainSetup.PrivateBinPath);
            foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
            {
                if (!a.IsDynamic && File.Exists(a.Location))
                {
                    var folder = Path.GetDirectoryName(a.Location);
                    if (folder != null && Directory.Exists(folder))
                    {
                        ((PluginAssemblyResolver)_proxyResolver).AddPath(folder);
                    }
                }
            }

            // push this assembly into the other domain.
            LoadFileWithReferences(Assembly.GetExecutingAssembly().Location);

            // setup the assembly resolver
            Invoke(resolver => { _appDomain.AssemblyResolve += resolver.Resolve; }, ((PluginAssemblyResolver)_proxyResolver));

            // if that can't find an assembly, ask this domain.
            Invoke(resolver => { resolver.SetAlternatePathResolver(ResolveFromThisDomain); }, ((PluginAssemblyResolver)_proxyResolver));

            // Give the plugin domain a way of telling us what Dynamic Assemblies they are creating.
            _appDomain.SetData("RegisterDynamicAssembly", new Action <string, string>(RegisterDynamicAssembly));
        }
Beispiel #2
0
        public virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_proxyResolver != null)
                {
                    _proxyResolver.Dispose();
                    _proxyResolver = null;
                }

                _resolver = null;
                if (_appDomain != null)
                {
                    try {
                        AppDomain.Unload(_appDomain);
                    } catch (Exception e) {
                        e.Dump();
                    }
                }
                _appDomain = null;
            }
        }