C# 远程服务器 创建、修改、删除 应用程序池 网站

首先 C# 操作 站点 需要 引用Microsoft.Web.Administration.dll 文件,创建站点我们一般需要 远程服务的IP,网站名称、端口、物理路径;这里默认网站名称和应用程序池名称一致。

应用程序池默认不启动,应为刚创建站点是没有对应真实的物理文件,修改 队列长度、启动模式、回收时间、最大工作进程, 以及日志路径。修改的时候如果修改站点物理路径的话,我们需要把文件 从旧得目录拷贝到新的目录下,删除站点就比较简单了。

但是站点应用程序池的停止 和启动就比较难搞了,不是调用stop后就马上能停止的,我们需要一个检测状态的机制以及重试机制,如果没有停止 就等待一段时间,因为只有应用程序池完全停止后我们在可以拷贝文件到应用程序目录下;启动也是一样的需要一个 等待 和重试的机制。相关code如下:

  #region IIS 操作
        /// <summary>
        /// 创建IIS site
        /// </summary>
        /// <param name="serverIP">服务器IP</param>
        /// <param name="webName">site 名称</param>
        /// <param name="port">site端口</param>
        /// <param name="path">site地址</param>
        void CreateWebSite(string serverIP, string webName, int port, string path)
        {
            using (ServerManager sm = ServerManager.OpenRemote(serverIP))
            {
                //创建应用程序池
                ApplicationPool appPool = sm.ApplicationPools.FirstOrDefault(x => x.Name == webName);
                if (appPool == null)
                {
                    appPool = sm.ApplicationPools.Add(webName);
                    appPool.AutoStart = false;

                    appPool.QueueLength = 10000;
                    appPool.StartMode = StartMode.AlwaysRunning;//启动模式
                    appPool.Recycling.PeriodicRestart.Time = new TimeSpan();//回收时间间隔
                    appPool.ProcessModel.IdleTimeout = new TimeSpan();//闲置超时
                    appPool.ProcessModel.MaxProcesses = 1;//最大工作进程数
                }
                //创建Site
                Site site = sm.Sites.FirstOrDefault(x => x.Name == webName);
                if (site == null)
                {
                    //检查远程文件夹是否存在 不存在创建
                    string remotePath = PathUtil.GetRemotePath(serverIP, path);
                    if (!Directory.Exists(remotePath))
                    {
                        Directory.CreateDirectory(remotePath);
                    }

                    site = sm.Sites.Add(webName, path, port);
                    site.ServerAutoStart = true;

                    site.Bindings[0].EndPoint.Port = port;
                    Application root = site.Applications["/"];
                    root.ApplicationPoolName = webName;
                    root.VirtualDirectories["/"].PhysicalPath = path;
                    root.SetAttributeValue("preloadEnabled", true); /*预加载*/

                    #region 修改日志路径
                    if (!string.IsNullOrEmpty(ConfigUtil.IISLog))
                    {
                        string remoteLog = PathUtil.GetRemotePath(serverIP, ConfigUtil.IISLog);
                        if (!Directory.Exists(remoteLog))
                        {
                            Directory.CreateDirectory(remoteLog);
                        }
                        site.LogFile.Directory = ConfigUtil.IISLog;
                        string failedLog = Path.Combine(ConfigUtil.IISLog, "FailedReqLogFiles");
                        if (!Directory.Exists(failedLog))
                        {
                            Directory.CreateDirectory(failedLog);
                        }
                        site.TraceFailedRequestsLogging.Directory = failedLog;
                    }
                    #endregion
                }
                sm.CommitChanges();
            }
        }

        /// <summary>
        /// 修改IIS站点名 端口 和路径
        /// </summary>
        /// <param name="serverIP">服务器IP</param>
        /// <param name="oldWebName">旧的名称</param>
        /// <param name="newWebName">新的web名称</param>
        /// <param name="newPort">新的端口</param>
        /// <param name="newPath">新的路径</param>
        void ModifyWebSite(string serverIP, string oldWebName, string newWebName, int newPort, string newPath)
        {
            using (ServerManager sm = ServerManager.OpenRemote(serverIP))
            {
                //修改应用程序池
                ApplicationPool appPool = sm.ApplicationPools.FirstOrDefault(x => x.Name == oldWebName);
                if (appPool != null && oldWebName != newWebName)
                {
                    appPool.Name = newWebName;
                }
                //修改Site
                Site site = sm.Sites.FirstOrDefault(x => x.Name == oldWebName);
                if (site != null)
                {
                    Application root = site.Applications["/"];
                    if (oldWebName != newWebName)
                    {
                        site.Name = newWebName;
                        root.ApplicationPoolName = newWebName;
                    }

                    int oldPort = site.Bindings[0].EndPoint.Port;
                    if (oldPort != newPort)
                    {
                        site.Bindings[0].EndPoint.Port = newPort;
                    }
                    string oldPath = root.VirtualDirectories["/"].PhysicalPath;
                    if (oldPath.ToLower() != newPath.ToLower())
                    {
                        string remoteOldPath = PathUtil.GetRemotePath(serverIP, oldPath);
                        string remoteNewPath = PathUtil.GetRemotePath(serverIP, newPath);
                        if (!Directory.Exists(remoteNewPath))
                        {
                            Directory.CreateDirectory(remoteNewPath);
                        }

                         //拷贝文件
                         CopyFiles(remoteOldPath, remoteNewPath);

                        if (Directory.Exists(remoteOldPath))
                        {
                            Directory.Delete(remoteOldPath, true);
                        }
                        root.VirtualDirectories["/"].PhysicalPath = newPath;
                    }
                }
                #region 修改日志路径
                if (!string.IsNullOrEmpty(ConfigUtil.IISLog))
                {
                    string remoteLog = PathUtil.GetRemotePath(serverIP, ConfigUtil.IISLog);
                    if (!Directory.Exists(remoteLog))
                    {
                        Directory.CreateDirectory(remoteLog);
                    }
                    site.LogFile.Directory = ConfigUtil.IISLog;
                    string failedLog = Path.Combine(ConfigUtil.IISLog, "FailedReqLogFiles");
                    if (!Directory.Exists(failedLog))
                    {
                        Directory.CreateDirectory(failedLog);
                    }
                    site.TraceFailedRequestsLogging.Directory = failedLog;
                }
                #endregion
                sm.CommitChanges();
            }
        }

        /// <summary>
        /// 删除IIS 站点
        /// </summary>
        /// <param name="serverIP">服务器地址</param>
        /// <param name="webName">站点名</param>
        void RemoveWebSite(string serverIP, string webName)
        {

            string path = string.Empty;
            using (ServerManager sm = ServerManager.OpenRemote(serverIP))
            {
                //删除应用程序池
                ApplicationPool appPool = sm.ApplicationPools.FirstOrDefault(x => x.Name == webName);
                if (appPool != null)
                {
                    //appPool.Stop();
                    sm.ApplicationPools.Remove(appPool);
                }
                //删除Site
                Site site = sm.Sites.FirstOrDefault(x => x.Name == webName);
                if (site != null)
                {
                    path = site.Applications["/"].VirtualDirectories["/"].PhysicalPath;
                    sm.Sites.Remove(site);
                }
                sm.CommitChanges();
            }
            //删除文件
            if (!string.IsNullOrEmpty(path))
            {
                string remotePath = PathUtil.GetRemotePath(serverIP, path);
                if (Directory.Exists(remotePath))
                {
                    Directory.Delete(remotePath, true);
                }
            }
        }

        /// <summary>
        /// 在服务器上检查端口是否被其他站点使用
        /// </summary>
        /// <param name="webIP">服务器地址</param>
        /// <param name="port">端口号</param>
        /// <param name="webName">站点名</param>
        /// <returns></returns>
        bool CheckPortIsUsed(string webIP, string webName, int webPort)
        {
            bool exist = false;
            try
            {
                using (ServerManager sm = ServerManager.OpenRemote(webIP))
                {
                    List<Site> sites = sm.Sites.ToList();
                    foreach (Site site in sites)
                    {
                        foreach (var item in site.Bindings)
                        {
                            if (item.EndPoint != null && item.EndPoint.Port == webPort && site.Name != webName)
                            {
                                exist = true;
                                break;
                            }
                        }//end for Bindings
                    }//end for Site
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return exist;
        }

        /// <summary>
        /// 停止或启动应用程序池
        /// </summary>
        /// <param name="serverIP">远程服务器IP</param>
        /// <param name="poolNames">应用程序池名称集合</param>
        /// <param name="stop">true 停止 false 启动</param>
        public void StopAndStartAppPool(string serverIP, string poolName, bool stop = true)
        {
            using (ServerManager sm = ServerManager.OpenRemote(serverIP))
            {
                ApplicationPool pool = sm.ApplicationPools.FirstOrDefault(x => x.Name == poolName);
                if (pool != null)
                {
                    StopAndStartAppPool(pool, stop);
                    if (stop)
                    {
                        WaiteAppPoolState(pool, ObjectState.Stopped);
                    }
                }
            }
        }

        /// <summary>
        /// 停止或启动应用程序池
        /// </summary>
        /// <param name="pool">应用程序池对象</param>
        /// <param name="stop">是否是停止</param>
        void StopAndStartAppPool(ApplicationPool pool, bool stop = true)
        {
            Action act = () =>
            {
                ObjectState poolSate = pool.State;
                if (stop && (poolSate == ObjectState.Starting || poolSate == ObjectState.Started))
                {
                    //如果当前应用程序池是启动或者正在启动状态,调用停止方法
                    pool.Stop();
                }
                if (!stop && (poolSate == ObjectState.Stopped || poolSate == ObjectState.Stopping))
                {
                    pool.Start();
                }
            };
            int retryCount = 0;
            int maxCount = 4;
            while (pool != null && retryCount <= maxCount)
            {
                try
                {
                    act();
                    break;
                }
                catch (Exception ex)
                {
                    retryCount++;
                    if (retryCount == maxCount)
                    {
                        throw new Exception($"{(stop ? "停止" : "启动")}启动应用程序池{pool.Name}出错{ex.Message}");
                    }
                    Thread.Sleep(1000 * 30);
                }
            }//end while
        }

        /// <summary>
        /// 检查引用程序池的状态
        /// </summary>
        /// <param name="pool">程序池</param>
        /// <param name="state">状态</param>
        void WaiteAppPoolState(ApplicationPool pool, ObjectState state)
        {
            int times = 0;
            while (pool.State != state && times < 50 /*5分钟*/)
            {
                //检查应用程序池是否已经停止
                Thread.Sleep(1000 * 10);
                times++;
            }
        }

        /// <summary>
        /// 获取应用程序池 和站点的状态
        /// </summary>
        /// <param name="serverIP">服务器IP</param>
        /// <param name="webName">站点名称</param>
        /// <returns></returns>
        string GetWebState(string serverIP, string webName)
        {
            ObjectState poolState = ObjectState.Unknown;
            ObjectState siteState = ObjectState.Unknown;
            using (ServerManager sm = ServerManager.OpenRemote(serverIP))
            {
                //应用程序池
                ApplicationPool appPool = sm.ApplicationPools.FirstOrDefault(x => x.Name == webName);
                if (appPool != null)
                {
                    poolState = appPool.State;
                }

                //Site
                Site site = sm.Sites.FirstOrDefault(x => x.Name == webName);
                if (site != null)
                {
                    siteState = site.State;
                }
            }
            return $"{poolState.ToString()} | {siteState.ToString()}";
        }
        #endregion
 public class ConfigUtil
    {
        static string _dotNetPath = string.Empty;
        public static string DotNetPath
        {
            get
            {
                if (string.IsNullOrEmpty(_dotNetPath))
                {
                    string sysDisk = Environment.SystemDirectory.Substring(0, 3);
                    _dotNetPath = sysDisk + @"WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe";//因为当前用的是4.0的环境
                }
                return _dotNetPath;
            }
        }

        static string _iisLog = string.Empty;
        public static string IISLog
        {
            get
            {
                if (string.IsNullOrEmpty(_iisLog))
                {
                    _iisLog = ConfigurationManager.AppSettings["IISLog"];
                }
                return _iisLog;
            }
        }
    }
时间: 2024-08-02 02:46:33

C# 远程服务器 创建、修改、删除 应用程序池 网站的相关文章

DDL 数据库定义语言--库创建/修改/删除--表的创建/修改/删除/复制

/*进阶 11 DDL 数据库定义语言 库和表的管理 一:库的管理:创建/修改/删除 二:表的管理:创建/修改/删除 创建: CREATE DATABASE [IF NOT EXISTS] 库名; 修改: alter 删除: DROP DATABASE [IF EXISTS] 库名; */ #一 : 库的管理 #1 库的创建 CREATE DATABASE books; CREATE DATABASE IF NOT EXISTS books; #2/库的修改 #库名的修改,需要停止服务器,然后修

Linux创建修改删除用户和组

Linux 创建修改删除用户和组 介绍 在日常的维护过程中创建用户操作用的相对会多一些,但是在这个过程中涉及到的知识点就不单单就是useradd了,接下来就来详细了解账号管理的相关信息. 用户信息 先从用户信息开始分析,可以通过查询/etc/password文件,每一行代表一个用户信息 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin m

python ssh 连接远程服务器,修改文本内容,调用脚本

今天小编get到一个用python的paramiko库创建ssh对象,连接到远程服务器,并且修改文件内容,调用脚本的好方法!! 主角当然是paramiko库啦,利用paramiko创建一个ssh对象,用于连接远程服务器 import paramiko ssh = paramiko.SSHClient()#创建SSH对象 设置允许连接不在know_hosts的主机,ssh会将 访问过计算机的公钥(public key)都记录在~/.ssh/known_hosts,当下次访问相同计算机时,OpenS

Linux基础学习-用户的创建修改删除

用户添加修改删除 1 useradd添加用户 添加一个新用户hehe,指定uid为3000,家目录为/home/haha [[email protected] ~]# useradd -u 3000 -d /home/haha hehe hehe:x:3000:3000::/home/haha:/bin/bash [[email protected] ~]# ls /home/ haha 这里-u是uid,-g可以指定组id,不定值系统自动分配,-G可以指定附加组,如果不想让用户登录系统可以指定

Linux 创建修改删除用户和组

200 ? "200px" : this.width)!important;} --> 介绍 在日常的维护过程中创建用户操作用的相对会多一些,但是在这个过程中涉及到的知识点就不单单就是useradd了,接下来就来详细了解账号管理的相关信息. 用户信息 先从用户信息开始分析,可以通过查询/etc/password文件,每一行代表一个用户信息 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon

oracle11g创建修改删除表

我的数据库名字: ORCL         密码:123456 1.模式 2.创建表 3.表约束 4.修改表 5.删除表 1.模式 set oracle_sid=ORCL sqlplus /nolog 1)进入同名模式(首次使用可能需要设置见附录,我设置scott用户的密码 123) connect scott/123 show user 2)进入sys模式(无法删除sys列) connect  /as sysdba show user 3)进入public模式 connect sys/1234

mongoDB集合 文档创建修改删除以及查询命令总结

mongodb在windows下的安装,启动查看上一篇:mongoDB安装详解 一.登录 查看数据库 数据库中的集合 文档 添加文档,修改文档,删除文档 1.查看有哪些数据库可以用: show dbs; 2.查看当前使用的数据库的名称: db.getName(); 3.使用某个数据库,和mysql中一样可以进行数据库之间的转化 use  dbname; 4. 如果没有数据库则创建数据库,mongodb没有提供像mysql等的创建数据库的语句但有相似功能的命令:如果有这个数据库则使用这个数据库如果

ElasticSearch.net NEST批量创建修改删除索引完整示例

本示例采用Elasticsearch+Nest 网上查了很多资料,发现用C#调用Elasticsearch搜索引擎的功能代码很分散,功能不完整,多半是非常简单的操作,没有成型的应用示例.比如新增或修改索引,都是发起一个request新增或修改一条数据,当一次性修改几千条数据时,发起的requst请求过多容易导致429 Too Many Request的错误,单个新增修改索引就非常不适用.其实Nest有批量新增.修改索引的功能,批量删除也可以.现将项目中采用Elasticsearch的C#代码分享

MySQL入门很简单: 4 创建 修改删除表

1. 创建表的方法 1)创建表的语法形式 首先,选择数据库: USE 数据库名: 创建表: CREATE TABLE 表名 (属性名 数据类型 [完整性约束条件], 属性名 数据类型 [完整性约束条件], 属性名 数据类型 ); 2)设置表的主键 主键用于标识每一个记录, 主键必须唯一. 单字段主键: 主键是由一个字段构成的 语法规则: 属性名 数据类型 PRIMARY KEY 例子: CREATE TABLE example1(stu_id INT PRIMARY KEY, stu_name