使用Microsoft.Web.Administration 自动配置.Net Core 或.Net FrameWork到IIS

这篇文章是关于如何使用最新的Microsoft.Web.Administration名称空间, 来操作IIS7.0,从而自动配置.Net Core 或.Net FrameWork到IIS,其中包括对应用程序池名字的检测与创建和网站名字的检测与创建。

  • 首先使用Nuget安装Microsoft.Web.Administration库
  • 创建一个方法的返回类型,用于记录网站名和应用程序名是否存在和是否创建成功。
    /// <summary>
    /// 自定义类型,用来记录判断的状态和信息
    /// </summary>
    public struct Result
    {
        public bool State { get; set; }
        public string Msg { get; set; }
    }
  • 创建检测端口、网站名、应用池名是否存在,以及创建站点和创建应用池的方法
    /// <summary>
        /// 判断应用池是否存在
        /// </summary>
        /// <param name="iisManager">IIS管理对象</param>
        /// <param name="appPoolName">应用池名字</param>
        /// <returns>不存在:true</returns>
        public Result CheckAppPool(ServerManager iisManager, string appPoolName)
        {
            Result result = new Result { State = false };
            try
            {
                if (iisManager.ApplicationPools[appPoolName] == null)
                {
                    result.State = true;
                }
                else
                {
                    result.Msg = "应用池名已存在!";
                }
            }
            catch (Exception e)
            {
                result.State = false;
                result.Msg = e.Message;
            }
            return result;
        }
        /// <summary>
        /// 创建应用池
        /// </summary>
        /// <param name="iisManager">IIS管理对象</param>
        /// <param name="appPoolName">应用池名称</param>

        /// <param name="iisPoolManager">将设置应用池的IIS管理带出去,统一保存</param>
        /// <returns>创建成功为True</returns>
        public Result CreateAppPool(ServerManager iisManager, string appPoolName,string appPoolVer, out ServerManager iisPoolManager)
        {
            Result result = new Result { State = false };
            try
            {
                ApplicationPool appPool = iisManager.ApplicationPools.Add(appPoolName);

                appPool.AutoStart = true;//启动
                appPool.ManagedRuntimeVersion = appPoolVer;//设置应用池的版本,空时则为无托管模式用于.net core
                //appPool.StartMode = StartMode;//启动模式
                //appPool.Recycling.PeriodicRestart.Time = new TimeSpan(RecoveryTime);//回收时间间隔
                //appPool.ProcessModel.IdleTimeout = new TimeSpan(IdleTime);//闲置超时
                //appPool.ProcessModel.MaxProcesses = MaxProcesses;//最大工作进程数
                //appPool.SetAttributeValue("enable32BitAppOnWin64", false);//启用32为应用程序
                //appPool.ManagedRuntimeVersion = "";
                result.State = true;
            }
            catch (Exception e)
            {
                result.State = false;
                result.Msg = "创建应用池失败!"+e.Message;
            }
            iisPoolManager = iisManager;
            return result;
        }
        /// <summary>
        /// 判断端口是否被占用
        /// </summary>
        /// <param name="port">端口名称</param>
        /// <returns>占用:true</returns>
        public bool CheckPort(int port)
        {
            bool inUse = false;

            IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
            IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();

            foreach (IPEndPoint endPoint in ipEndPoints)
            {
                if (endPoint.Port == port)
                {
                    inUse = true;
                    break;
                }
            }

            return inUse;
        }
        /// <summary>
        /// 判断网站名是否存在
        /// </summary>
        /// <param name="iisManager">IIS管理对象</param>
        /// <param name="siteName">网站名字</param>
        /// <returns>不存在为True</returns>
        public Result CheckWebName(ServerManager iisManager, string siteName)
        {
            Result result = new Result { State = false };
            try
            {
                if (iisManager.Sites[siteName] == null)
                {
                    result.State = true;
                }
                else
                {
                    result.Msg = "网站名存在!";
                }

            }
            catch (Exception e)
            {
                result.State = false;
                result.Msg = e.Message;
            }
            return result;
        }
        /// <summary>
        /// 创建站点
        /// </summary>
        /// <param name="iisManager">IIS管理对象</param>
        /// <param name="webName">站点名</param>
        /// <param name="path">站点路径</param>
        /// <param name="port">站点端口</param>
        /// <param name="iisWebManager">将设置应用池的IIS管理带出去,统一保存</param>
        /// <returns>创建成功:true</returns>
        public Result CreateWebSit(ServerManager iisManager, string webName,string ip, string path,string HostName, int port,string binding, out ServerManager iisWebManager)
        {
            Result result = new Result { State = false, Msg = "" };
            try
            {
                string fullIP = ip + ":" + port + ":"+HostName;
                iisManager.Sites.Add(webName, binding, fullIP, path);
                iisManager.Sites[webName].Applications[0].ApplicationPoolName = webName;

                iisManager.CommitChanges();
                result.State = true;
            }
            catch (Exception e)
            {
                result.Msg ="站点发布失败"+ e.Message;
            }
            iisWebManager = iisManager;
            return result;
        }
  • 调用这些方法来自动发布站点即可
  • 
            /// <summary>
            /// IIS自动发布,适用于.Net Core 和.Net FramWork
            /// </summary>
            /// <param name="webName">网站名</param>
            /// <param name="path">网站发布路径</param>
            /// <param name="port">端口号</param>
            /// <param name="ip">ip地址</param>
            /// <param name="HostName">域名</param>
            /// <param name="appPoolVer">应用池版本,如果是.Net Core默认即可,.Net Framework包括"v4.0"和"v2.0"</param>
            /// <param name="binding">Http或者https</param>
            /// <returns></returns>
            public Result IISAutoPublish(string webName, string path, int port, string ip, string HostName = "", string appPoolVer = "", string binding = "https")
            {
                Result result = new Result { State = false, Msg = "" };
    
                ServerManager iisManager = new ServerManager();
                InfoCheck infoCheck = new InfoCheck();
    
                //判断端口是否被占用
                if (infoCheck.CheckPort(port))
                {
                    result.Msg = "端口已被占用!";
                    result.State = false;
                    return result;
                }
    
    
                //判断是否存在应用池,不存在则创建
                result = infoCheck.CheckAppPool(iisManager, webName);
                if (result.State)
                {
                    //创建应用池
                    result = infoCheck.CreateAppPool(iisManager, webName, appPoolVer, out ServerManager iisPoolManager);
    
                    if (result.State)
                    {
                        iisManager = iisPoolManager;
                    }
                    else
                    {
                        return result;
                    }
                }
                else
                {
                    return result;
                }
    
                //判断是否存在网站,不存在则创建
                result = infoCheck.CheckWebName(iisManager, webName);
                if (result.State)
                {
                    //创建网站
                    result = infoCheck.CreateWebSit(iisManager, webName, ip, path, HostName, port, binding, out ServerManager iisWebManager);
                }
                else
                {
                    return result;
                }
                return result;
            }
    

    注意:.net core 发布必须先安装.NET Core 托管捆绑包安装程序(直接下载),(学习如何将.net core 发布到IIS上),如果想要发布.net core 请在调用自动发布方法时仔细看代码注释

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值