/// <summary>
 /// Called when an exception occurs in the isolated part.
 /// </summary>
 /// <param name="host">The host.</param>
 /// <param name="exception">The exception.</param>
 internal static void OnFaulted(PartActivationHostBase host, Exception exception)
 {
     if (Faulted != null)
     {
         Faulted(host, new ActivationHostEventArgs(host.Description, exception));
     }
 }
        private static IPartActivationHost CreateActivationHost(Type type, string configFileBaseName)
        {
            // use two variables to satisfy CA disposable object exception path....
            PartActivationHostBase selectedHost     = null;
            PartActivationHostBase tempSelectedHost = null;

            try
            {
                lock (_hosts)
                {
                    // find an appropriate existing host if any...
                    var query = from host in _hosts
                                let workDirectory = Path.GetDirectoryName(type.Assembly.Location)
                                                    where host.ActivatedTypes.Contains(type) || host.Description.HostWorkingDirectory == workDirectory
                                                    select host;

                    tempSelectedHost = query.FirstOrDefault();

                    // if a host was found, return it...
                    if (tempSelectedHost != null)
                    {
                        selectedHost     = tempSelectedHost;
                        tempSelectedHost = null;
                        return(selectedHost);
                    }

                    // if no host found, create a new one...

                    string location    = Path.GetDirectoryName(type.Assembly.Location);
                    var    description = new ActivationHostDescription(location, configFileBaseName);

                    tempSelectedHost = new PartAppDomainHost(description);

                    _hosts.Add(tempSelectedHost);
                }

                // start the host...
                tempSelectedHost.Start();

                selectedHost     = tempSelectedHost;
                tempSelectedHost = null;
            }
            finally
            {
                if (tempSelectedHost != null)
                {
                    tempSelectedHost.Dispose();
                }
            }

            return(selectedHost);
        }