之前由于工作需要自己做一个一键部署的小工具,实现三个模块的功能:TFS操作创建映射并获取最新源代码;SQL Server数据库注册表配置数据库连接;IIS站点部署,生成可访问的IIS站点。由于是基于自己的工作环境下的开发,所以在TFS和SQL Server配置工具化实现,有一些点是默认按照公司的环境配置参数默认的,虽然不是广泛适用每一种情况的环境部署,但是在学习这三个模块的开发过程中,还是有很多东西是可以值得分享的。
今天先分享一下,如何通过工具化实现IIS站点部署和配置,为了可复用性,IIS操作这次是将其封装成一个类,以方便调用。
可以看一下工具的截图:由于这个只是为了节省开发部署环境的时间而开发的小工具,所以没那么高大上,还请各位看官包涵。
进入正题:先从使用角度来讲解IIS操作,然后再深入到具体的IIS服务底层原理。
【1】前提掌握要点:
(1)、IIS到目前经历了四个版本分别为 IIS4.0 IIS5.0 IIS6.0 IIS7.0,其中IIS6.0 IIS7.0是在5.0的安全问题的基础上获得的发展,目前为止。6.0版本以后的都是比较安全稳定的,为什么需要了解IIS版本,是因为6.0以后和之前的IIS提供的操作API是不一样的,不过IIS6.0时代主要以using System.DirectoryServices空间下的DirectoryEntry 对象作为编程访问一个主要载体.但随着IIS7.0发布.NET的Web程序由IIS6.0开始逐渐过渡到 7.0版本.而且在编程控制IIS上新添加的Microsoft.Web.Administration名称空间, 可以操作7.0
(2)、平时的站点部署需要配置什么参数或者说是我们一般自己部署站点需要什么场景的操作,考虑可以用程序实现的,然后可以模型化为对象去实现。
我采用的方案是:将部署的站点声明为一个对象,其中包含了一般我们站点部署时设置的参数作为它的属性和字段,将我们一般站点设置实现为一些方法。
可以先定义一个站点类:
//站点身份验证模式 public enum autherRight {asp.net模拟, Form身份验证, Windows身份验证, 基本身份验证,匿名身份验证,摘要式身份验证 }; /// <summary> /// 站点类 /// </summary> public class newWebSiteInfo { //站点设置参数 public string hostip; //主机ip public string porNum; //端口号 public string hostName;//主机名 public string webName; //网站名 public string appName; //应用程序池 public string webPath; //根目录 public string visualPath;//虚拟目录 public Dictionary<string, string> newMimeType;//需要新添加mime类型 public autherRight autherRight;//身份验证模式 public string defoultPage;//默认文档 public newWebSiteInfo(string hostip, string portnum, string hostname, string webname, string appName, string webpath, string visualPath, Dictionary<string, string> newMimeType, autherRight autherRight,string defoultPage) { this.hostIp = hostip; this.porNum = portnum; this.hostName = hostname; this.webName=webname; this.appName = appName; this.webPath = webpath; this.visualPath = visualPath; this.newMimeType = newMimeType; this.autherRight = autherRight; this.defoultPage = defoultPage; } /// <summary> /// 返回站点绑定信息 /// </summary> /// <returns></returns> public string bindString() { return String.IsNullOrEmpty(hostName) ? String.Format("http://{0}:{1}", hostIp, porNum) : String.Format("http://{0}:{1}", hostName, porNum); } }
目前IIS操作主要有两种方式:一种是System.DirectoryServices空间下面的类,用于IIS5/6版本,和可以兼容iis6的IIS7版本;Microsoft.Web.Administration空间下面的类,IIS7引入的新的管理类,主要通过ServerManger类进行站点新增。
本次主要通过System.DirectoryServices方式操作IIS,也简要介绍一下ServerManger新建站点:
(1) 添加.net引用Microsoft.Web.Administration.dll,实例化ServerManger新增站点方式如下:
using System;using Microsoft.Web.Administration;class CreateASite{ static void Main(string[] args) { ServerManager serverManager = new ServerManager(); Site mySite = serverManager.Sites.Add( "MySite", "d://inetpub//mysite", 8080); mySite.ServerAutoStart = true; serverManager.CommitChanges(); }}
(2)通过System.DirectoryServices空间下面的类获取IIS服务,但其实对于高版本的也可以同时引用Microsoft.Web.Administration.dll中通过ServerManger来进行参数设置各有各的好处。
首先介绍一下我用到的几个命名空间:
using System.DirectoryServices; //获取目录实体类using Microsoft.Web.Administration; //ServerManger类所属命名空间using IISOle; //IIS管理添加mime类型using System.Security.accessControl; //设置文件安全权限类所属命名空间using System.IO; //文件路径类所属命名空间using System.ServicePRocess; //上一节中所用的系统服务对象所属命名空间
现在提供我自己的源代码,可以适用一般类需求,对于一些特殊配置的应用程序部署,可以稍微做微调。
///created by george///date:2014-5-3///QQ:709617880namespace IISmng{ //站点身份验证模式 public enum autherRight {asp_net模拟, Form身份验证, Windows身份验证, 基本身份验证,匿名身份验证,摘要式身份验证 }; /// <summary> /// 站点类 /// </summary> public class newWebSiteInfo { //站点设置参数 public string hostIp; //主机ip public string porNum; //端口号 public string hostName;//主机名 public string webName; //网站名 public string appName; //应用程序池 public string webPath; //物理路径 public string visualPath;//虚拟目录 public Dictionary<string, string> newMimeType;//需要新添加mime类型 public autherRight autherRight;//身份验证模式 public string defoultPage;//默认文档 public newWebSiteInfo(string hostip, string portnum, string hostname, string webname, string appName, string webpath, string visualPath, Dictionary<string, string> newMimeType, autherRight autherRight,string defoultPage) { this.hostIp = hostip; this.porNum = portnum; this.hostName = hostname; this.webName=webname; this.appName = appName; this.webPath = webpath; this.visualPath = visualPath; this.newMimeType = newMimeType; this.autherRight = autherRight; this.defoultPage = defoultPage; } /// <summary> /// 返回站点绑定信息 /// </summary> /// <returns></returns> public string bindString() { return String.IsNullOrEmpty(hostName) ? String.Format("http://{0}:{1}", hostIp, porNum) : String.Format("http://{0}:{1}", hostName, porNum); } } //托管模式 public enum modelType{集成,经典}; //net版本 public enum netVersion{ v2_0 , v4_0}; /// <summary> /// IIS操作类 /// </summary> public class myIIS { /// <summary> /// IIS版本属性 /// </summary> public String IISVersion { get { return IISVersion; } set{ DirectoryEntry IISService = new DirectoryEntry("IIS://localhost/W3SVC/INFO"); IISVersion = "v"+IISService.Properties["MajorIISVersionNumber"].Value.ToString(); } } /// <summary> /// 检测客户端或服务器是否安装IIS服务 /// </summary> /// <returns>true OR false</returns> public Boolean checkIIS() { Boolean retMsg = false; try { DirectoryEntry IISService = new DirectoryEntry("IIS://localhost/W3SVC"); if(IISService.GetType().ToString().Equals("DirectoryEntry")) { if (checkServiceIsRunning("IIS Admin Service")) retMsg=true; } } catch(Exception e){ } return retMsg; } /// <summary> /// 检测服务是否开启 /// </summary> /// <param name="serviceName"></param> /// <returns></returns> public Boolean checkServiceIsRunning(string serviceName) { ServiceController[] allServices = System.ServiceProcess.ServiceController.GetServices(); Boolean runing = false; foreach (ServiceController sc in allServices) { if (sc.DisplayName.Trim() == serviceName.Trim()) { if (sc.Status.ToString() == "Running") { runing = true; } } } return runing; } /// <summary> /// 获取本机IIS版本 /// </summary> /// <returns></returns> public String getIISVersion() { String retStr = ""; if (checkIIS()) { DirectoryEntry IISService = new DirectoryEntry("IIS://localhost/W3SVC/INFO"); retStr = "v"+IISService.Properties["MajorIISVersionNumber"].Value.ToString(); } return retStr; } /// <summary> /// 判断程序池是否存在 /// </summary> /// <param name="AppPoolName">程序池名称</param> /// <returns>true存在 false不存在</returns> private bool i
新闻热点
疑难解答