一、引言:

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

二、MWA 总侃

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

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

我们很容易对这类的结构进行可视化。在这个结构中,包括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 配置文件层次结构

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

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

 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

六、总结

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

IIS7 开发与 管理 编程 之 Microsoft.Web.Administration的更多相关文章

  1. IIS 7管理API——Microsoft.Web.Administration介绍

    原文:http://www.cnblogs.com/dflying/archive/2006/04/17/377276.html 本文翻译整理自Carlos Aguilar Mares的blog文章: ...

  2. C# IIS站点管理--Microsoft.Web.Administration.dll

    Microsoft中提供了管理IIS7及以上版本一个非常强大的API - Microsoft.Web.Administration.dll,利用该API可以让我们很方便的以编程的方式管理和设定IIS的 ...

  3. Microsoft.Web.Administration操作IIS7时的权限设置

    在用Microsoft.Web.Administration操作IIS7时,你可能会遇到如下权限错误: 文件名: redirection.config错误: 由于权限不足而无法读取配置文件 如下图: ...

  4. IIS7 Microsoft.Web.Administration 创建Application问题

    在使用DirectoryEntry操作IIS时,可以设置很多属性.但使用Microsoft.Web.Administration中的一些类时,不知道在哪设置.例如:AccessScript,Acces ...

  5. 使用 Microsoft.Web.Administration 管理iis

    How to Automate IIS 7 Configuration with .NET How to Automate IIS 7 Configuration with .NET Are you ...

  6. C#操作IIS站点 Microsoft.Web.Administration.dll

    利用IIS7自带类库管理IIS现在变的更强大更方便,而完全可以不需要用DirecotryEntry这个类了(网上很多.net管理iis6.0的文章都用到了DirecotryEntry这个类 ),Mic ...

  7. Microsoft.Web.Administration in IIS

    http://blogs.msdn.com/b/carlosag/archive/2006/04/17/microsoftwebadministration.aspx 最好使用在IIS8中,因为为每一 ...

  8. Web Fram 2 for IIS7.X(Microsoft Web Farm Framework)

    Microsoft Web Farm Framework (WFF) 2.0 是微软开发的.基于IIS 7.x的小插件,能够帮助我们轻松实现Web网站的高性能.高可用性,用来在Web服务器群上提供和管 ...

  9. 使用Microsoft Web Application Stress Tool对web进行压力测试

    Web压力测试是目前比较流行的话题,利用Web压力测试可以有效地测试一些Web服务器的运行状态和响应时间等等,对于Web服务器的承受力测试是个非常好的手法.Web 压力测试通常是利用一些工具,例如微软 ...

随机推荐

  1. ActiveMQ_2安装

    Linux安装 环境JDK7以上 gz文件拷贝到 /usr/local/目录下 解压 后缀为 .tar.gz的压缩包 进入解压后的文件夹 cd apache-activemq-x.xx.x/ cd b ...

  2. ESP32 windows开发环境的搭建(官方方法)

    首先保证电脑中的已经下载了git客户端,没有的自行去https://git-scm.com/下载 STEP1: 获得编译工具链 Windows没有内置的“make”环境,所以安装工具链你将需要一个兼容 ...

  3. CPP全面总结(涵盖C++11标准)

    OOP之类和对象 1. this指针的引入 每个成员函数都有一个额外的隐含的形参,这个参数就是this指针,它指向调用对象的地址.默认情况下,this的类型是指向类类型非常量版本的常量指针.可以表示成 ...

  4. MyBatis 源码分析 - 配置文件解析过程

    * 本文速览 由于本篇文章篇幅比较大,所以这里拿出一节对本文进行快速概括.本篇文章对 MyBatis 配置文件中常用配置的解析过程进行了较为详细的介绍和分析,包括但不限于settings,typeAl ...

  5. 漏洞应急响应之批量poc验证

    1.文章难易度 [★★★] 2.文章知识点: python,poc验证; 3.文章作者: 野驴 4.本文参与 i春秋学院原创文章奖励计划,未经许可禁止转载! 0x01前言 当互联网爆出高危漏洞,或者团 ...

  6. 使用new Image()进行预加载

    概述 这篇博文记录了用new Image()进行预加载的总结,供以后开发时参考,相信对其他人也有用. 旧的预加载 一般我们为了让背景图更快的加载,我们常常把背景图放在一个display:none的im ...

  7. 故事描述SVM----支持向量机/support vector machine (SVM)

    作者:简之链接:https://www.zhihu.com/question/21094489/answer/86273196来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  8. source insight 添加 python 支持

    从http://www.sourceinsight.com/public/languages/下载Python的配置文件Python.CLF 选择Options > Preferences,单击 ...

  9. Fiddler怎样抓取手机的包

    Fiddler作为代理服务器,可以拦截到手机发出的请求,再经过Fiddler发送到服务器,获取到服务器响应的数据,这个过程,只要设置配置好了,使用过程不受影响. 要想抓取到手机的包,先要给手机设置一个 ...

  10. Python内置类型(1)——真值测试

    python中任何对象都能直接进行真假值的测试,用于if或者while语句的条件判断,也可以做为布尔逻辑运算符的操作数 python中任何对象都能直接进行真假值的测试,而不需要额外的类型转换 这一点是 ...