博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IIS7 开发与 管理 编程 之 Microsoft.Web.Administration
阅读量:7186 次
发布时间:2019-06-29

本文共 4810 字,大约阅读时间需要 16 分钟。

一、引言:

        关于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)  显示站点列表

1 void getListOfIIS(){ 2             this.listBox1.Items.Clear(); 3             string StateStr = ""; 4             for(int i=0; i
View Code

2)  创建站点、绑定域名

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

 3)  站点权限设置

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

 4)  创建应用池

1 ApplicationPool newPool = IISManager.ApplicationPools[appoolname];    2 if (newPool == null)3 {4     IISManager.ApplicationPools.Add(appoolname);5     newPool = IISManager.ApplicationPools[appoolname];6     newPool.Name =appoolname;7     newPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;8     newPool.ManagedRuntimeVersion = "v2.0";9 }
View Code

5)  站点应用池绑定

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

6)  追加域名

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

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

1 this.IISManager.Sites[sitename)].Applications.Remove(this.IISManager.Sites[sitename].Applications[0]);2     this.IISManager.Sites.Remove(this.IISManager.Sites[sitename]);3     this.IISManager.ApplicationPools.Remove(this.IISManager.ApplicationPools[appoolname]);4     this.IISManager.CommitChanges();5     this.txt_log.AppendText(sitename+ "站点删除成功\r\n!");
View Code

 

三、动态运行库类  

1 ServerManager ms = new ServerManager();2             foreach(WorkerProcess wp in ms.WorkerProcesses){3                 Console.WriteLine(wp.AppPoolName+ " " +wp.ProcessGuid+" "+wp.Schema);4                 foreach(Request request In wp.GetRequests(0)){5                     Console.WriteLine(" request:"+request.Url);6                 }7             }8
View Code

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

也没的总运行时间 。

 

四、访问远程服务器

1 try{ 2                 ServerManager ms = ServerManager.OpenRemote("116.254.199.39"); 3                 for(int i=0; i
View Code

连接一台远程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 开发!!

 

待续。。。。。。。。。。。。。。。。。。。。。

 

 

   

 

转载于:https://www.cnblogs.com/echosong/p/3485534.html

你可能感兴趣的文章
curl提示不支持https协议解决方法
查看>>
Spring定时任务@Scheduled的cron表达式
查看>>
centos 防火墙设置
查看>>
SSH Secure Shell Client
查看>>
Linksys e3200初试tomato系统
查看>>
Runtime 类的使用
查看>>
Cassandra删除数据的坑
查看>>
AGC018D Tree and Hamilton Path(树+树的重心)
查看>>
【Codeforces #168 Div1 & Div2】Solutions
查看>>
求逆元的四种办法
查看>>
Ubuntu 12.04下LAMP安装配置
查看>>
Nginx连载
查看>>
【leetcode】Department Top Three Salaries
查看>>
【原创】空中鼠标一DMP欧拉角
查看>>
【汇编】Linux 下汇编程序开发
查看>>
git常用命令归纳
查看>>
leetcode-cn 删除排序数组中的重复项
查看>>
计算机网络之前端必备
查看>>
持久化、序列化、反序列化、编码、解码的概念
查看>>
javascript函数
查看>>