IIS7 开发与 管理 编程 之 Microsoft.Web.Administration

时间:2023-03-08 19:48:55

一、引言:

关于IIS7 Mocrosoft.Web.Administration  网上这方面详细资料相对来说比较少,大家千篇一律的(都是一篇翻译过来的文章,msdn 里面的实列没有)。前段做了一个相关的项目,找得非常勤苦,借此机会把相关东西整理记录下来分享给大家 !!!

二、MWA 总侃

Microsoft.Web.Administration(MWA) API 是专门为IIS7.0 开发新功能,为IIS 管理编程提供了一个强类型的.net类型的集合。这个API 保持在(%WinDir%\System32\InetSrv)

MWA 根级别类是ServerManager 这个是其他所有类的基类。

IIS7 开发与 管理 编程 之 Microsoft.Web.Administration

我们很容易对这类的结构进行可视化。在这个结构中,包括5个类对象: Site、Application、VirtualDirectory、ApplicationPool,以及WorkerProcess。此外还包括了一些辅助类。

一个Application 属于一个Site, 而一个VirtualDirectory 属于一个Application。这些类型对象都不知对立的,他们必须是父对象组成部分。

利用WorkerProcess 类,我们可以实时观察服务器的配置数据!我们还可以访问当前运行的工作进程,其实可以观察当前处于运行状态的强求(页就是我们在server2008 IIS Manager )

另外,ServerManager类提供了一系列的方法,利用这些方法,可以直接管理配置文件,如果大家熟悉IIS 配置方面东西应该知道IIS 包括一系列的config xml 文件,如果我们直接去管理编辑这些文件是比较复杂的,而ServerManager使得这个过程变得非常的简单。

二、配置类

核心的配置类包括 ApplicationPool、Site、Application 和VirtualDirectory,还有包括其他的一些辅助类,用于设置对象默认值。在创建这些对象非常容易。

1)  显示站点列表

 void getListOfIIS(){
this.listBox1.Items.Clear();
string StateStr = "";
for(int i=; i<IISManager.Sites.Count;i++){ switch(IISManager.Sites[i].State){
case ObjectState.Started:{
StateStr = "正常";break;
}
case ObjectState.Starting:{
StateStr = "正在启动"; break;
}
case ObjectState.Stopping:{
StateStr = "正在关闭"; break;
}
case ObjectState.Stopped:{
StateStr = "关闭";break;
}
}
this.listBox1.Items.Add(IISManager.Sites[i].Name+"【"+StateStr+"】");
}
}

2)  创建站点、绑定域名

 static void Main(string[] args)
{
ServerManager serverManager = new ServerManager();
Site site = IISManager.Sites.Add("site name", "http", "*:80:"+siteUrl, sitePath);
mySite.ServerAutoStart = true;
serverManager.CommitChanges();
}

3)  站点权限设置

 Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection anonymousAuthenticationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication",sitename );
anonymousAuthenticationSection["enabled"] = true;
anonymousAuthenticationSection["userName"] = @"IUSR_" + this.txt_No.Text.ToString();
anonymousAuthenticationSection["password"] = @"" + this.txt_password.Text.ToString();
serverManager.CommitChanges();

4)  创建应用池

 ApplicationPool newPool = IISManager.ApplicationPools[appoolname];
if (newPool == null)
{
IISManager.ApplicationPools.Add(appoolname);
newPool = IISManager.ApplicationPools[appoolname];
newPool.Name =appoolname;
newPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;
newPool.ManagedRuntimeVersion = "v2.0";
}

5)  站点应用池绑定

 site.Applications["/"].ApplicationPoolName  = appoolName //此appoolname就是新的的引用池

6)  追加域名

 private void BindURL(string UserName)
{
string str = this.IISManager.Sites[UserName].Bindings[].Host.Split(new char[]{'.'})[];
string bindingInformation = "*:80:" + str + "." + this.txt_addUrl.Text;
this.IISManager.Sites[UserName].Bindings.Add(bindingInformation, "http");
this.IISManager.CommitChanges();
}

7)  删除站点、删除应用池

 this.IISManager.Sites[sitename)].Applications.Remove(this.IISManager.Sites[sitename].Applications[]);
this.IISManager.Sites.Remove(this.IISManager.Sites[sitename]);
this.IISManager.ApplicationPools.Remove(this.IISManager.ApplicationPools[appoolname]);
this.IISManager.CommitChanges();
this.txt_log.AppendText(sitename+ "站点删除成功\r\n!");

三、动态运行库类  

 ServerManager ms = new ServerManager();
foreach(WorkerProcess wp in ms.WorkerProcesses){
Console.WriteLine(wp.AppPoolName+ " " +wp.ProcessGuid+" "+wp.Schema);
foreach(Request request In wp.GetRequests()){
Console.WriteLine(" request:"+request.Url);
}
}

用户访问控制(UAC)可能会干扰正常工作,如果没有得到结果,那么首先确认是否有管理员的权限,这样就避免UAC阻止的问题,查看所有处于运行的状态的请求,注意GetRequest(0)中参数0代表

也没的总运行时间 。

四、访问远程服务器

 try{
ServerManager ms = ServerManager.OpenRemote("116.254.199.39");
for(int i=; i<ms.Sites.Count;i++){
Console.WriteLine(ms.Sites[i].Name);
}
}catch(Exception ex){
Console.WriteLine(ex.Message);
}finally{
Console.WriteLine("成功");
}

连接一台远程IIS 服务器,只需要简单的调用SerManager.OpenRemote。 可以在OpenRemotede的ServerName中使用IP地址主机名或者域名。

五、直接编辑属性和元素

这里需要先补充下IIS 配置文件层次结构

IIS7 开发与 管理 编程 之 Microsoft.Web.Administration

可以使用ServerManager基类查看、创建、更新或者删除配置文件中的任何属性和元素。我们可以编辑任何IIS配置文件,包括appclicationHost.config、administration.cconfig、redirection.config, 已经所有网站应用程序配置

不同的配置文件对应着不同的 对象

IIS7 开发与 管理 编程 之 Microsoft.Web.Administration
 1 ServerManager ms = new ServerManager();
2 //获取 applicationHost.conf
3 Configuration config = ms.GetApplicationHostConfiguration;
4
5 //获取特定网站的web.config配置文件
6 Configuration config = ms.GetWebConfiguration("site1");
7
8 // 获取一个应用程序的 配置文件
9 Configuration config = ms.GetWebConfiguration("site1","/App1");
10
11 //获取一个特定的location标记
12 ConfigurationSection section= config.GetSection("system.webServer/defaultDocumnet");
13
14 //获取site1 web.config 配置文件中属于启动的状态属性
15 ConfigurationAttribute enabled = section.Attributes("enabled");
16 Console.WriteLine(enabled.Value);
17
18 //获取网站默认文档
19 ConfigurationElementCollection filescollection = section.GetCollection("files");
20 foreach(ConfigurationElement el in filescollection){
21 Console.WriteLine(el.Attributes("value"));
22 }
23
24
IIS7 开发与 管理 编程 之 Microsoft.Web.Administration

六、总结

以下基本上能完全实现一个自助站点 管理 IIS 的程序。希望对大家的工作,学习提供一定层度上的帮助。 下节 我们总结 IIS7 模块 和 ISAPI 开发!!