public IRegisteredObject CreateObject(IApplicationHost appHost, Type type)
        {
            if (appHost == null)
            {
                throw new ArgumentNullException("appHost");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            string appId = CreateSimpleAppId(VirtualPath.Create(appHost.GetVirtualPath()), appHost.GetPhysicalPath(), appHost.GetSiteName());

            return(CreateObjectInternal(appId, type, appHost, false));
        }
Example #2
0
        public void StartApplication(string path)
        {
            if (_log != null && _log.IsDebugEnabled)
            {
                _log.Debug(string.Format("Starting application from {0}", path));
            }

            lock (SyncRoot)
            {
                foreach (ApplicationInfo applicationInfo in ApplicationManager.GetApplicationManager().GetRunningApplications())
                {
                    if (applicationInfo.PhysicalPath == path)
                    {
                        throw new InvalidOperationException("Application already started");
                    }
                }
                //if (_hosts.ContainsKey(path))
                //    throw new InvalidOperationException("Application already started");
                string physicalPath = path;
                string virtualPath  = string.Empty;
                try
                {
                    DirectoryInfo directoryInfo = new DirectoryInfo(path);
                    string        appName       = directoryInfo.Name;
                    virtualPath = "/" + directoryInfo.Name;
                    if (_log != null)
                    {
                        _log.Debug("Starting application " + appName);
                    }
                    //_log.Debug("PhysicalPath = " + physicalPath);
                    //_log.Debug("VirtualPath = " + virtualPath);
                    string shadowCopyAssemblyList = string.Empty;

                    FluorineRuntimeProxy host = FluorineRuntimeProxy.Start(this, physicalPath, virtualPath, null, null, shadowCopyAssemblyList, _singleDomain);
                    host.Start();
                    //_hosts.Add(physicalPath, host);
                }
                catch (Exception exception)
                {
                    if (_log != null && _log.IsErrorEnabled)
                    {
                        _log.Error("Error starting application " + virtualPath, exception);
                    }
                    if (ApplicationError != null)
                    {
                        ApplicationError(this, new ApplicationErrorEventArgs(new ApplicationInfo(null, VirtualPath.Create(virtualPath), physicalPath), exception));
                    }
                    //throw;
                }
            }
        }
Example #3
0
        public void Start(bool singleDomain)
        {
            if (_log != null && _log.IsDebugEnabled)
            {
                _log.Debug("Starting HostManager.");
            }

            _singleDomain = singleDomain;
            lock (SyncRoot)
            {
                try
                {
                    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

                    string shadowCopyAssemblyList = string.Empty;

                    /*
                     * string assemblyFolder = Path.Combine("net", Environment.Version.Major + "." + Environment.Version.Minor);
                     * string[] shadowCopyAssemblies = new string[]{"FluorineFx.Hosting.dll", "FluorineFx.dll", "log4net.dll", "ICSharpCode.SharpZipLib.dll"};
                     * for( int i = 0; i < shadowCopyAssemblies.Length; i++)
                     * {
                     *  if( i > 0 )
                     *      shadowCopyAssemblyList += ";";
                     *  shadowCopyAssemblyList += Path.Combine(assemblyFolder, shadowCopyAssemblies[i]);
                     * }
                     */
                    //string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "applications");
                    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ApplicationRoot);
                    if (Directory.Exists(path))
                    {
                        foreach (string dir in Directory.GetDirectories(path))
                        {
                            string physicalPath = dir;
                            string virtualPath  = string.Empty;
                            try
                            {
                                DirectoryInfo directoryInfo = new DirectoryInfo(dir);
                                string        appName       = directoryInfo.Name;
                                virtualPath = "/" + directoryInfo.Name;
                                if (_log != null)
                                {
                                    _log.Debug("Starting application " + appName);
                                }
                                //_log.Debug("PhysicalPath = " + physicalPath);
                                //_log.Debug("VirtualPath = " + virtualPath);

                                FluorineRuntimeProxy host = FluorineRuntimeProxy.Start(this, physicalPath, virtualPath, null, null, shadowCopyAssemblyList, singleDomain);
                                host.Start();
                                //_hosts.Add(physicalPath, host);
                            }
                            catch (Exception exception)
                            {
                                if (_log != null && _log.IsErrorEnabled)
                                {
                                    _log.Error("Error starting application " + virtualPath, exception);
                                }
                                if (ApplicationError != null)
                                {
                                    ApplicationError(this, new ApplicationErrorEventArgs(new ApplicationInfo(null, VirtualPath.Create(virtualPath), physicalPath), exception));
                                }
                            }
                        }
                    }
                    else
                    {
                        if (_log != null)
                        {
                            _log.Debug(string.Format("Base directory {0} was not found.", path));
                        }
                    }
                    _serverTimer.Enabled = true;
                    if (_log != null)
                    {
                        _log.Debug("HostManager started.");
                    }
                }
                catch (Exception ex)
                {
                    if (_log != null)
                    {
                        _log.Fatal("Failed to start HostManager.", ex);
                    }
                }
            }
        }
        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);
        }