『QQ:1353814576』

C#简化部署IIS网站 Microsoft.Web.Administration实现一键发布IIS网站


C#使用Microsoft.Web.Administration实现一键发布IIS网站

最近接到需求 简化部署IIS网站 之前都是手动部署,别人嫌弃麻烦 没办法就各种找办法实现自动化部署。 最终找到 利用 Microsoft.Web.Administration.dll 实现网站程序一键发布的功能 比之前写的方法好用了很多; 优点是可以直观的设置网站各方面的配置,缺点目前没发现 做下备忘

Microsoft.Web.Administration.dll%WinDir%\\System32\\InetSrv 目录下

using Microsoft.Web.Administration;  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Net.Sockets;  
using System.ServiceProcess;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace MedicalServiceMIS.Common  
{  
    /// <summary>  
    /// 网站发布帮助类  
    /// </summary>  
    public class PublishWebHelper  
    {  
        private string WebName;  
        private int Port;  
        private string ServerDomainName;  
        public PublishWebHelper(string WebName, int Port, string ServerDomainName = null)  
        {  
            this.WebName = WebName;  
            this.Port = Port;  
            this.ServerDomainName = ServerDomainName;  
        }  
  
        public bool Execute(string WebsiteDirectory, out string ErrorContent)  
        {  
            ServiceController service = ServiceController.GetServices("127.0.0.1").FirstOrDefault(x => x.ServiceName == "W3SVC");  
  
            if (service is null)  
            {  
                ErrorContent = "服务器尚未安装 IIS 服务模块!";  
                return false;  
            }  
  
            if (!System.IO.Directory.Exists(WebsiteDirectory))  
            {  
                ErrorContent = "指定目录不存在!";  
                return false;  
            }  
            try  
            {  
                ServerManager iismanager = new ServerManager();  
                //判断应用程序池是否存在  
                if (iismanager.ApplicationPools[WebName] != null)  
                {  
                    //移除应用程序池  
                    iismanager.ApplicationPools.Remove(iismanager.ApplicationPools[WebName]);  
                }  
                //判断web应用程序是否存在  
                if (iismanager.Sites[WebName] != null)  
                {  
                    ///移除应用程序  
                    iismanager.Sites.Remove(iismanager.Sites[WebName]);  
                }  
                //建立web应用程序(第二个参数为安装文件的地址)  
                iismanager.Sites.Add(WebName, WebsiteDirectory, Port);  
  
                //添加web应用程序池  
                ApplicationPool pool = iismanager.ApplicationPools.Add(WebName);  
                #region 应用程序池配置  
                pool.StartMode = StartMode.AlwaysRunning;//启动模式  
                pool.Recycling.PeriodicRestart.Schedule.Add(new TimeSpan(03, 00, 00));//凌晨回收一次资源  
                pool.Cpu.Limit = 50000;//限制最大CPU 50%  
                pool.Cpu.Action = ProcessorAction.ThrottleUnderLoad;//竞争cpu时限制使用最大cpu 百分比  
                //设置web应用程序池的Framework版本(注意版本号大小写问题)  
                pool.ManagedRuntimeVersion = "v4.0";  
                //设置是否启用32为应用程序  
                pool.Enable32BitAppOnWin64 = true;  
                ///设置专用内存限制 1G 超出释放一次  
                pool.Recycling.PeriodicRestart.PrivateMemory = 512000 * 2;  
                #endregion  
                //设置web网站的应用程序池  
                var website = iismanager.Sites[WebName];  
  
                website.Applications[0].ApplicationPoolName = WebName;  
                if (!string.IsNullOrEmpty(ServerDomainName))  
                {  
                    string str = website.Bindings[0].Host.Split(new char[] { '.' })[0];  
                    string bindingInformation = $"*:{Port}:{str}{ServerDomainName}";  
                    website.Bindings.Add(bindingInformation, "http");  
                }  
                //提交更改  
                iismanager.CommitChanges();  
            }  
            catch (Exception ex)  
            {  
                ErrorContent = ex.Message;  
                return false;  
            }  
            ErrorContent = "部署成功";  
            return true;  
        }  
    }  
}

调用例子:

///实例化IIS发布方法类
PublishWebManager publishWeb = new PublishWebManager("www.YuanTK.com", 10086);
string ErrorContent;//返回的错误信息
///返回true 就是发布成功 false就是失败 ErrorContent就是失败返回的相关错误信息
bool IsSuccess = publishWeb.Execute(@"C:\inetpub\www.yuantk.com\",out ErrorContent);