public IRegisteredObject CreateObject(string appId, Type type, string virtualPath, string physicalPath, bool failIfExists, bool throwOnError)
 {
     if (appId == null)
         throw new ArgumentNullException("appId");
     SimpleApplicationHost appHost = new SimpleApplicationHost(VirtualPath.CreateAbsolute(virtualPath), physicalPath);
     HostingEnvironmentParameters hostingParameters = null;
     if (throwOnError)
     {
         hostingParameters = new HostingEnvironmentParameters();
         hostingParameters.HostingFlags = HostingEnvironmentFlags.ThrowHostingInitErrors;
     }
     return CreateObjectInternal(appId, type, appHost, failIfExists, hostingParameters);
 }
        internal ObjectHandle CreateInstanceInNewWorkerAppDomain(Type type, string appId, VirtualPath virtualPath, string physicalPath)
        {
            IApplicationHost             appHost           = new SimpleApplicationHost(virtualPath, physicalPath);
            HostingEnvironmentParameters hostingParameters = new HostingEnvironmentParameters();

            hostingParameters.HostingFlags = HostingEnvironmentFlags.HideFromAppManager;
            return(CreateAppDomainWithHostingEnvironmentAndReportErrors(appId, appHost, hostingParameters).CreateInstance(type));
        }
        public IRegisteredObject CreateObject(string appId, Type type, string virtualPath, string physicalPath, bool failIfExists, bool throwOnError)
        {
            if (appId == null)
            {
                throw new ArgumentNullException("appId");
            }
            SimpleApplicationHost        appHost           = new SimpleApplicationHost(VirtualPath.CreateAbsolute(virtualPath), physicalPath);
            HostingEnvironmentParameters hostingParameters = null;

            if (throwOnError)
            {
                hostingParameters = new HostingEnvironmentParameters();
                hostingParameters.HostingFlags = HostingEnvironmentFlags.ThrowHostingInitErrors;
            }
            return(CreateObjectInternal(appId, type, appHost, failIfExists, hostingParameters));
        }
		internal ObjectHandle CreateInstanceInNewWorkerAppDomain(Type type, string appId, VirtualPath virtualPath, string physicalPath)
		{
			IApplicationHost appHost = new SimpleApplicationHost(virtualPath, physicalPath);
            HostingEnvironmentParameters hostingParameters = new HostingEnvironmentParameters();
            hostingParameters.HostingFlags = HostingEnvironmentFlags.HideFromAppManager;
            return CreateAppDomainWithHostingEnvironmentAndReportErrors(appId, appHost, hostingParameters).CreateInstance(type);
		}
 private HostingEnvironment GetAppDomainWithHostingEnvironment(string appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters)
 {
     HostingEnvironment environment;
     lock (this)
     {
         _appDomains.TryGetValue(appId, out environment);
         if (environment != null)
         {
             try
             {
                 environment.IsUnloaded();
             }
             catch (AppDomainUnloadedException)
             {
                 environment = null;
                 //_appDomainsShutdowdIds.Append("Un:" + appId + ":" + DateTime.UtcNow.ToShortTimeString() + ";");
             }
         }
         if (environment == null)
         {
             environment = CreateAppDomainWithHostingEnvironmentAndReportErrors(appId, appHost, hostingParameters);
             _appDomains[appId] = environment;
         }
     }
     return environment;
 }
        private HostingEnvironment CreateAppDomainWithHostingEnvironment(string appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters)
		{
			string physicalPath = appHost.GetPhysicalPath();
			if(!StringUtil.StringEndsWith(physicalPath, Path.DirectorySeparatorChar))
				physicalPath = physicalPath + Path.DirectorySeparatorChar;

			string domainId = ConstructAppDomainId(appId);
			string tmp = appId.ToLower(CultureInfo.InvariantCulture) + physicalPath.ToLower(CultureInfo.InvariantCulture);
			string appName = tmp;
			VirtualPath appVPath = VirtualPath.Create(appHost.GetVirtualPath());
			Dictionary<string, string> dict = new Dictionary<string, string>(20);
			AppDomainSetup setup = new AppDomainSetup();
			PopulateDomainBindings(domainId, appId, appName, physicalPath, appVPath, setup, dict);
			AppDomain domain = null;
			Exception innerException = null;

			_log.Debug("DomainBindings " + domainId);
			_log.Debug("PhysicalPath = " + physicalPath);
			_log.Debug("VirtualPath = " + appVPath.VirtualPathString);
			try
			{
				domain = AppDomain.CreateDomain(domainId, GetDefaultDomainIdentity(), setup);
				foreach (KeyValuePair<string, string> entry in dict)
				{
					domain.SetData(entry.Key, entry.Value);
				}
			}
			catch(Exception exception)
			{
				innerException = exception;
			}
			if (domain == null)
				throw new SystemException("Cannot create AppDomain", innerException);

			Type type = typeof(HostingEnvironment);
			string fullName = type.Module.Assembly.FullName;
			string typeName = type.FullName;
			ObjectHandle handle = null;
			try
			{
				handle = domain.CreateInstance(fullName, typeName);
			}
			finally
			{
				if (handle == null)
				{
					_log.Debug("Unloading Domain " + domain.FriendlyName);
					AppDomain.Unload(domain);
				}
			}
			HostingEnvironment environment = (handle != null) ? (handle.Unwrap() as HostingEnvironment) : null;
			if (environment == null)
				throw new SystemException("Cannot create HostingEnvironment");

			environment.Initialize(this, appHost);
			_appDomains[appId] = environment;
			return environment;
		}
        private HostingEnvironment CreateAppDomainWithHostingEnvironmentAndReportErrors(string appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters)
		{
			HostingEnvironment environment;
			try
			{
                environment = CreateAppDomainWithHostingEnvironment(appId, appHost, hostingParameters);
			}
			catch(Exception exception)
			{
				if( _log.IsErrorEnabled )
					_log.Error(string.Format("Failed to initialize AppDomain {0}", appId), exception );
				throw;
			}
			return environment;
		}
 internal IRegisteredObject CreateObjectInternal(string appId, Type type, IApplicationHost appHost, bool failIfExists, HostingEnvironmentParameters hostingParameters)
 {
     if (!typeof(IRegisteredObject).IsAssignableFrom(type))
     {
         throw new ArgumentException(SR.GetString(SR.NotIRegisteredObject, new object[] { type.FullName }), "type");
     }
     ObjectHandle handle = GetAppDomainWithHostingEnvironment(appId, appHost, hostingParameters).CreateWellKnownObjectInstance(type, failIfExists);
     if (handle == null)
         return null;
     return handle.Unwrap() as IRegisteredObject;
 }
        private HostingEnvironment GetAppDomainWithHostingEnvironment(string appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters)
        {
            HostingEnvironment environment;

            lock (this)
            {
                _appDomains.TryGetValue(appId, out environment);
                if (environment != null)
                {
                    try
                    {
                        environment.IsUnloaded();
                    }
                    catch (AppDomainUnloadedException)
                    {
                        environment = null;
                        //_appDomainsShutdowdIds.Append("Un:" + appId + ":" + DateTime.UtcNow.ToShortTimeString() + ";");
                    }
                }
                if (environment == null)
                {
                    environment        = CreateAppDomainWithHostingEnvironmentAndReportErrors(appId, appHost, hostingParameters);
                    _appDomains[appId] = environment;
                }
            }
            return(environment);
        }
        private HostingEnvironment CreateAppDomainWithHostingEnvironment(string appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters)
        {
            string physicalPath = appHost.GetPhysicalPath();

            if (!StringUtil.StringEndsWith(physicalPath, Path.DirectorySeparatorChar))
            {
                physicalPath = physicalPath + Path.DirectorySeparatorChar;
            }

            string      domainId             = ConstructAppDomainId(appId);
            string      tmp                  = appId.ToLower(CultureInfo.InvariantCulture) + physicalPath.ToLower(CultureInfo.InvariantCulture);
            string      appName              = tmp;
            VirtualPath appVPath             = VirtualPath.Create(appHost.GetVirtualPath());
            Dictionary <string, string> dict = new Dictionary <string, string>(20);
            AppDomainSetup setup             = new AppDomainSetup();

            PopulateDomainBindings(domainId, appId, appName, physicalPath, appVPath, setup, dict);
            AppDomain domain         = null;
            Exception innerException = null;

            _log.Debug("DomainBindings " + domainId);
            _log.Debug("PhysicalPath = " + physicalPath);
            _log.Debug("VirtualPath = " + appVPath.VirtualPathString);
            try
            {
                domain = AppDomain.CreateDomain(domainId, GetDefaultDomainIdentity(), setup);
                foreach (KeyValuePair <string, string> entry in dict)
                {
                    domain.SetData(entry.Key, entry.Value);
                }
            }
            catch (Exception exception)
            {
                innerException = exception;
            }
            if (domain == null)
            {
                throw new SystemException("Cannot create AppDomain", innerException);
            }

            Type         type     = typeof(HostingEnvironment);
            string       fullName = type.Module.Assembly.FullName;
            string       typeName = type.FullName;
            ObjectHandle handle   = null;

            try
            {
                handle = domain.CreateInstance(fullName, typeName);
            }
            finally
            {
                if (handle == null)
                {
                    _log.Debug("Unloading Domain " + domain.FriendlyName);
                    AppDomain.Unload(domain);
                }
            }
            HostingEnvironment environment = (handle != null) ? (handle.Unwrap() as HostingEnvironment) : null;

            if (environment == null)
            {
                throw new SystemException("Cannot create HostingEnvironment");
            }

            environment.Initialize(this, appHost);
            _appDomains[appId] = environment;
            return(environment);
        }
        private HostingEnvironment CreateAppDomainWithHostingEnvironmentAndReportErrors(string appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters)
        {
            HostingEnvironment environment;

            try
            {
                environment = CreateAppDomainWithHostingEnvironment(appId, appHost, hostingParameters);
            }
            catch (Exception exception)
            {
                if (_log.IsErrorEnabled)
                {
                    _log.Error(string.Format("Failed to initialize AppDomain {0}", appId), exception);
                }
                throw;
            }
            return(environment);
        }
        internal IRegisteredObject CreateObjectInternal(string appId, Type type, IApplicationHost appHost, bool failIfExists, HostingEnvironmentParameters hostingParameters)
        {
            if (!typeof(IRegisteredObject).IsAssignableFrom(type))
            {
                throw new ArgumentException(SR.GetString(SR.NotIRegisteredObject, new object[] { type.FullName }), "type");
            }
            ObjectHandle handle = GetAppDomainWithHostingEnvironment(appId, appHost, hostingParameters).CreateWellKnownObjectInstance(type, failIfExists);

            if (handle == null)
            {
                return(null);
            }
            return(handle.Unwrap() as IRegisteredObject);
        }