Beispiel #1
0
        public async Task ProcessRequest(HttpContext httpContext)
        {
#if DEBUG
            var stopWatch = new Stopwatch();
            stopWatch.Start();
#endif
            var matchedSite = _sites.FirstOrDefault(site => site.Match(httpContext));
#if DEBUG
            stopWatch.Stop();
            Logger.LogDebug($"Router took {stopWatch.ElapsedMilliseconds}ms to match location.");
#endif
            if (matchedSite == null)
            {
            }
            else
            {
#if DEBUG
                stopWatch.Reset();
                stopWatch.Start();
#endif
                var htcContext = new HtcHttpContext(httpContext, matchedSite);
                await matchedSite.ProcessRequest(htcContext);

#if DEBUG
                stopWatch.Stop();
                Logger.LogDebug($"Router took {stopWatch.ElapsedMilliseconds}ms to process request.");
#endif
            }
        }
Beispiel #2
0
        public void CreateApplicationSite(string applicationSiteFile)
        {
            applicationSiteFilePath = applicationSiteFile;
            ApplicationSiteModel applicationSite = ApplicationSiteParseConfig(applicationSiteFilePath);

            if (applicationSite != null)
            {
                try {
                    string applicationName = applicationSite.ApplicationName;

                    ServerManager             serverManager             = new ServerManager();
                    SiteCollection            siteCollection            = serverManager.Sites;
                    ApplicationPoolCollection applicationPoolCollection = serverManager.ApplicationPools;
                    Site            site            = null;
                    ApplicationPool applicationPool = null;

                    _logger.InfoFormat("[{0}] - =============================================", MethodInfo.GetCurrentMethod().Name);
                    _logger.InfoFormat("[{0}] - applicationName: {1}", MethodInfo.GetCurrentMethod().Name, applicationName);

                    if ((siteCollection != null) && (siteCollection.Any()))
                    {
                        site = siteCollection.FirstOrDefault(p => p.Name == applicationName);

                        if ((applicationPoolCollection != null) && (applicationPoolCollection.Any()))
                        {
                            applicationPool = applicationPoolCollection.FirstOrDefault(p => p.Name == applicationName);
                        }

                        if ((site != null) || (applicationPool != null))
                        {
                            _logger.InfoFormat("[{0}] - ApplicationSite already exists: {1} please use different name", MethodInfo.GetCurrentMethod().Name, applicationName);

                            return;
                        }
                    }

                    if ((!Directory.Exists(applicationSite.PhysicalPath.Source)) && (!File.Exists(applicationSite.PhysicalPath.SourcePackage)) && (!Directory.Exists(applicationSite.PhysicalPath.Destination)))
                    {
                        _logger.InfoFormat("[{0}] - ApplicationSite PhysicalPath doesn't exists, can't create ApplicationSite", MethodInfo.GetCurrentMethod().Name);
                        _logger.InfoFormat("[{0}] - =============================================", MethodInfo.GetCurrentMethod().Name);

                        return;
                    }
                    else if (Directory.Exists(applicationSite.PhysicalPath.Destination))
                    {
                        _logger.InfoFormat("[{0}] - ApplicationSite PhysicalPath Destination already exists, can't create ApplicationSite", MethodInfo.GetCurrentMethod().Name);
                        _logger.InfoFormat("[{0}] - =============================================", MethodInfo.GetCurrentMethod().Name);

                        return;
                    }
                    else if ((Directory.Exists(applicationSite.PhysicalPath.Source)) && (!Directory.Exists(applicationSite.PhysicalPath.Destination)))
                    {
                        try {
                            Directory.CreateDirectory(applicationSite.PhysicalPath.Destination);

                            if (Directory.Exists(applicationSite.PhysicalPath.Destination))
                            {
                                CopyApplicationSite(applicationSite.PhysicalPath.Source, applicationSite.PhysicalPath.Destination);
                            }
                            else
                            {
                                _logger.InfoFormat("[{0}] - Failed to create directory {1}, can't create ApplicationSite", MethodInfo.GetCurrentMethod().Name, applicationSite.PhysicalPath.Destination);
                                _logger.InfoFormat("[{0}] - =============================================", MethodInfo.GetCurrentMethod().Name);
                            }
                        } catch (Exception ex) {
                            _logger.Error(string.Format("[{0}] - Problem on create Directory: {1}. Message: {2}", MethodInfo.GetCurrentMethod().Name, applicationSite.PhysicalPath.Destination, ex.Message), ex);
                            _logger.InfoFormat("[{0}] - =============================================", MethodInfo.GetCurrentMethod().Name);
                        }
                    }
                    else if ((File.Exists(applicationSite.PhysicalPath.SourcePackage)) && (!Directory.Exists(applicationSite.PhysicalPath.Destination)))
                    {
                        try {
                            ZipFile.ExtractToDirectory(applicationSite.PhysicalPath.SourcePackage, applicationSite.PhysicalPath.Destination);

                            if (Directory.Exists(applicationSite.PhysicalPath.Destination))
                            {
                                _logger.InfoFormat("[{0}] - Package extracted into {1}, continue to configure ApplicationSite", MethodInfo.GetCurrentMethod().Name, applicationSite.PhysicalPath.Destination);

                                ConfigureApplicationSite(applicationSite);
                            }
                            else
                            {
                                _logger.InfoFormat("[{0}] - Failed to extract package into {1}, can't create ApplicationSite", MethodInfo.GetCurrentMethod().Name, applicationSite.PhysicalPath.Destination);
                                _logger.InfoFormat("[{0}] - =============================================", MethodInfo.GetCurrentMethod().Name);
                            }
                        } catch (Exception ex) {
                            _logger.Error(string.Format("[{0}] - Problem on extract package: {1} to destination: {2}. Message: {3}", MethodInfo.GetCurrentMethod().Name, applicationSite.PhysicalPath.SourcePackage,
                                                        applicationSite.PhysicalPath.Destination, ex.Message), ex);
                            _logger.InfoFormat("[{0}] - =============================================", MethodInfo.GetCurrentMethod().Name);
                        }
                    }
                } catch (Exception ex) {
                    _logger.Error(string.Format("[{0}] - Message: {1}", MethodInfo.GetCurrentMethod().Name, ex.Message), ex);
                }
            }
            else
            {
                _logger.InfoFormat("[{0}] - ApplicationSite file config not found, can't create ApplicationSite", MethodInfo.GetCurrentMethod().Name);
            }
        }
        public void GetApplicationSiteDetail(string applicationName)
        {
            ServerManager  serverManager  = new ServerManager();
            SiteCollection siteCollection = serverManager.Sites;

            _logger.InfoFormat("[{0}] - =============================================", MethodInfo.GetCurrentMethod().Name);
            if ((siteCollection != null) && (siteCollection.Any()))
            {
                ApplicationPoolCollection applicationPoolCollection = serverManager.ApplicationPools;
                string previousApplicationPoolName = string.Empty;
                string currentApplicationPoolName  = string.Empty;

                _logger.InfoFormat("[{0}] - applicationName: {1}", MethodInfo.GetCurrentMethod().Name, applicationName);

                Site site = siteCollection.FirstOrDefault(p => p.Name == applicationName);
                if (site != null)
                {
                    _logger.InfoFormat("[{0}] - site.Id: {1}", MethodInfo.GetCurrentMethod().Name, site.Id);
                    _logger.InfoFormat("[{0}] - site.Name: {1}", MethodInfo.GetCurrentMethod().Name, site.Name);
                    _logger.InfoFormat("[{0}] - site.ServerAutoStart: {1}", MethodInfo.GetCurrentMethod().Name, site.ServerAutoStart);
                    _logger.InfoFormat("[{0}] - site.State: {1}", MethodInfo.GetCurrentMethod().Name, (int)site.State);
                    _logger.InfoFormat("[{0}] - site.State[string]: {1}", MethodInfo.GetCurrentMethod().Name, site.State);

                    BindingCollection bindings = site.Bindings;
                    _logger.InfoFormat("[{0}] - ======================", MethodInfo.GetCurrentMethod().Name);
                    foreach (Binding binding in bindings)
                    {
                        _logger.InfoFormat("[{0}] - binding.Protocol: {1}", MethodInfo.GetCurrentMethod().Name, binding.Protocol);
                        _logger.InfoFormat("[{0}] - binding.BindingInformation: {1}", MethodInfo.GetCurrentMethod().Name, binding.BindingInformation);
                        _logger.InfoFormat("[{0}] - ===========", MethodInfo.GetCurrentMethod().Name);
                    }
                    _logger.InfoFormat("[{0}] - ======================", MethodInfo.GetCurrentMethod().Name);

                    ApplicationCollection applications = site.Applications;
                    foreach (Application itemApplication in site.Applications)
                    {
                        currentApplicationPoolName = itemApplication.ApplicationPoolName;

                        _logger.InfoFormat("[{0}] - itemApplication.ApplicationPoolName: {1}", MethodInfo.GetCurrentMethod().Name, currentApplicationPoolName);
                        _logger.InfoFormat("[{0}] - itemApplication.EnabledProtocols: {1}", MethodInfo.GetCurrentMethod().Name, itemApplication.EnabledProtocols);
                        _logger.InfoFormat("[{0}] - itemApplication.Path: {1}", MethodInfo.GetCurrentMethod().Name, itemApplication.Path);

                        if ((string.IsNullOrWhiteSpace(previousApplicationPoolName) || (currentApplicationPoolName != previousApplicationPoolName)))
                        {
                            previousApplicationPoolName = currentApplicationPoolName;
                            GetApplicationPoolDetail(applicationPoolCollection, currentApplicationPoolName);
                        }
                        else
                        {
                            _logger.InfoFormat("[{0}] - using same ApplicationPoolName", MethodInfo.GetCurrentMethod().Name);
                        }

                        _logger.InfoFormat("[{0}] - ===========", MethodInfo.GetCurrentMethod().Name);
                        foreach (VirtualDirectory itemVirtualDirectory in itemApplication.VirtualDirectories)
                        {
                            _logger.InfoFormat("[{0}] - itemVirtualDirectory.LogonMethod: {1}", MethodInfo.GetCurrentMethod().Name, (int)itemVirtualDirectory.LogonMethod);
                            _logger.InfoFormat("[{0}] - itemVirtualDirectory.LogonMethod[string]: {1}", MethodInfo.GetCurrentMethod().Name, itemVirtualDirectory.LogonMethod);
                            _logger.InfoFormat("[{0}] - itemVirtualDirectory.UserName: {1}", MethodInfo.GetCurrentMethod().Name, itemVirtualDirectory.UserName);
                            _logger.InfoFormat("[{0}] - itemVirtualDirectory.Password: {1}", MethodInfo.GetCurrentMethod().Name, itemVirtualDirectory.Password);
                            _logger.InfoFormat("[{0}] - itemVirtualDirectory.Path: {1}", MethodInfo.GetCurrentMethod().Name, itemVirtualDirectory.Path);
                            _logger.InfoFormat("[{0}] - itemVirtualDirectory.PhysicalPath: {1}", MethodInfo.GetCurrentMethod().Name, itemVirtualDirectory.PhysicalPath);
                            _logger.InfoFormat("[{0}] - ===========", MethodInfo.GetCurrentMethod().Name);
                        }
                        _logger.InfoFormat("[{0}] - ======================", MethodInfo.GetCurrentMethod().Name);
                    }
                }
                else
                {
                    _logger.InfoFormat("[{0}] - ApplicationSite doesn't exists", MethodInfo.GetCurrentMethod().Name);
                }
            }
            else
            {
                _logger.InfoFormat("[{0}] - Server doesn't have any ApplicationSite", MethodInfo.GetCurrentMethod().Name);
            }
            _logger.InfoFormat("[{0}] - =============================================", MethodInfo.GetCurrentMethod().Name);
        }
        public void StopApplicationSite(string applicationName)
        {
            if (applicationName == "Default Web Site")
            {
                _logger.InfoFormat("[{0}] - Need to stop this ApplicationSite: {1} manually because it can conflict with ApplicationPool default", MethodInfo.GetCurrentMethod().Name, applicationName);

                return;
            }

            ServerManager  serverManager  = new ServerManager();
            SiteCollection siteCollection = serverManager.Sites;
            bool           changeSite     = false;
            bool           changePool     = false;

            _logger.InfoFormat("[{0}] - =============================================", MethodInfo.GetCurrentMethod().Name);
            if ((siteCollection != null) && (siteCollection.Any()))
            {
                ApplicationPoolCollection applicationPoolCollection = serverManager.ApplicationPools;
                string previousApplicationPoolName = string.Empty;
                string currentApplicationPoolName  = string.Empty;

                _logger.InfoFormat("[{0}] - applicationName: {1}", MethodInfo.GetCurrentMethod().Name, applicationName);

                Site site = siteCollection.FirstOrDefault(p => p.Name == applicationName);
                if (site != null)
                {
                    ObjectState objectState = site.State;

                    _logger.InfoFormat("[{0}] - site.Id: {1}", MethodInfo.GetCurrentMethod().Name, site.Id);
                    _logger.InfoFormat("[{0}] - site.Name: {1}", MethodInfo.GetCurrentMethod().Name, site.Name);
                    _logger.InfoFormat("[{0}] - site.ServerAutoStart: {1}", MethodInfo.GetCurrentMethod().Name, site.ServerAutoStart);
                    _logger.InfoFormat("[{0}] - site.State: {1}", MethodInfo.GetCurrentMethod().Name, (int)objectState);
                    _logger.InfoFormat("[{0}] - site.State[string]: {1}", MethodInfo.GetCurrentMethod().Name, objectState);

                    if (objectState != ObjectState.Stopped)
                    {
                        _logger.InfoFormat("[{0}] - ======================", MethodInfo.GetCurrentMethod().Name);
                        _logger.InfoFormat("[{0}] - ApplicationSite need to be stopped", MethodInfo.GetCurrentMethod().Name);

                        bool retry = true;
                        int  ctr   = 0;

                        while (retry)
                        {
                            try {
                                ObjectState returnState = site.Stop();

                                _logger.InfoFormat("[{0}] - returnState: {1}", MethodInfo.GetCurrentMethod().Name, (int)returnState);
                                _logger.InfoFormat("[{0}] - returnState[string]: {1}", MethodInfo.GetCurrentMethod().Name, returnState);

                                if (returnState == ObjectState.Stopped)
                                {
                                    _logger.InfoFormat("[{0}] - ApplicationSite is stopped", MethodInfo.GetCurrentMethod().Name);

                                    site.ServerAutoStart = false;
                                    changeSite           = true;

                                    retry = false;
                                }
                                else
                                {
                                    _logger.InfoFormat("[{0}] - Failed to stop ApplicationSite", MethodInfo.GetCurrentMethod().Name);

                                    retry = true;
                                }
                            } catch (Exception ex) {
                                retry = true;

                                _logger.Error(string.Format("[{0}] - Problem on stop ApplicationPool. Message: {1}, retry {2} time(s)", MethodInfo.GetCurrentMethod().Name, ex.Message, ctr), ex);
                            }

                            _logger.InfoFormat("[{0}] - retry status: {1}", MethodInfo.GetCurrentMethod().Name, retry);

                            if (ctr > 10)
                            {
                                retry = false;
                            }

                            ctr++;

                            if (retry)
                            {
                                Thread.Sleep(THREAD_SLEEP);
                            }
                        }
                    }

                    bool changeLocalPool = false;
                    ApplicationCollection applications = site.Applications;
                    foreach (Application itemApplication in site.Applications)
                    {
                        currentApplicationPoolName = itemApplication.ApplicationPoolName;

                        _logger.InfoFormat("[{0}] - itemApplication.ApplicationPoolName: {1}", MethodInfo.GetCurrentMethod().Name, currentApplicationPoolName);
                        _logger.InfoFormat("[{0}] - Check if ApplicationPool need to be stopped", MethodInfo.GetCurrentMethod().Name);

                        if ((string.IsNullOrWhiteSpace(previousApplicationPoolName) || (currentApplicationPoolName != previousApplicationPoolName)))
                        {
                            previousApplicationPoolName = currentApplicationPoolName;
                            changeLocalPool             = StopApplicationPool(applicationPoolCollection, currentApplicationPoolName);

                            if (changeLocalPool)
                            {
                                changePool = true;
                            }
                        }
                        else
                        {
                            _logger.InfoFormat("[{0}] - ApplicationPool already been processed", MethodInfo.GetCurrentMethod().Name);
                        }

                        _logger.InfoFormat("[{0}] - ======================", MethodInfo.GetCurrentMethod().Name);
                    }

                    if ((changeSite) || (changePool))
                    {
                        serverManager.CommitChanges();
                    }
                }
                else
                {
                    _logger.InfoFormat("[{0}] - ApplicationSite doesn't exists", MethodInfo.GetCurrentMethod().Name);
                }
            }
            else
            {
                _logger.InfoFormat("[{0}] - Server doesn't have any ApplicationSite", MethodInfo.GetCurrentMethod().Name);
            }
            _logger.InfoFormat("[{0}] - =============================================", MethodInfo.GetCurrentMethod().Name);
        }