Asp.net 编程实现应用程序池的创建

出处 http://martinnormark.com/adding-an-application-pool-to-iis7-programmatically/

 

Adding an Application Pool to IIS7 programmatically

April 16, 2008

Internet Information Services 7 (IIS7) has a great new set of features regarding configuration. Using the Integrated Configuration system, you can configure your server from XML files, the IIS7 manager, the command prompt using the APPCMD tool, but IIS7 also lets you manage your server from managed code in a very intuitive manner.

If you navigate to the %WINDIR%\System32\InetSrv folder in Windows Vista, you’ll find all the executables, DLL’s and XML (.config) configuration files you need. It doesn’t matter if you use the IIS7 Manager, managed code or the APPCMD command-line based tool to manage your server – at the end of the day you are changing an XML file. That is applicationHost.config which is located here: C:\Windows\System32\inetsrv\config\applicationHost.config.

The integrated configuration system on IIS7 is great news for hosters, and web developers. Hosters can easily automate server management through managed code, and as a web developer, you can configure your server from your web.config file, which makes it easy to move your web application from development, to test, and further up towards production. Read my post on how to set a websites default document, from within the web application’s web.config file.

In this post, I will create an application pool on my local IIS7.

Launch Visual Studio 2005 or 2008 – whatever you’ve got will work.

Create a new Console Application, and give it a name of your own choice.

Right click References in the Solution Explorer and add a new reference.

Locate Microsoft.Web.Administration.dll from the C:\Windows\System32\inetsrv folder.

To access IIS7, we use the ServerManager class, as shown below.

 1: using System;
 2: using System.Collections.Generic;
 3: using System.Linq;
 4: using System.Text;
 5: using Microsoft.Web.Administration;
 6: 
 7: namespace Iis7ManagementTest
 8: {
 9:   class Program
 10:   {
 11:     static void Main(string[] args)
 12:     {
 13:       ServerManager mgr = new ServerManager();
 14: 
 15:       // Add a new application pool called MyAppPool
 16:       ApplicationPool myAppPool = mgr.ApplicationPools.Add("MyAppPool");
 17: 
 18:       // Configure my new app pool to start automatically.
 19:       myAppPool.AutoStart = true;
 20: 
 21:       // What action should IIS take when my app pool exceeds 
 22:       // the CPU limit specified by the Limit property
 23:       myAppPool.Cpu.Action = ProcessorAction.KillW3wp;
 24: 
 25:       // Use the Integrated Pipeline mode
 26:       myAppPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;
 27: 
 28:       // Set the runtime version of ASP.NET to 2.0
 29:       myAppPool.ManagedRuntimeVersion = "v2.0";
 30: 
 31:       // Use the Network Service account
 32:       myAppPool.ProcessModel.IdentityType = ProcessModelIdentityType.NetworkService;
 33: 
 34:       // Shut down after being idle for 5 minutes.
 35:       myAppPool.ProcessModel.IdleTimeout = TimeSpan.FromMinutes(5);
 36: 
 37:       // Max. number of IIS worker processes (W3WP.EXE)
 38:       myAppPool.ProcessModel.MaxProcesses = 1;
 39: 
 40:       // Commit the changes
 41:       mgr.CommitChanges();
 42:     }
 43:   }
 44: }

After the application has been executed, we will see our new application pool inside the IIS7 Manager.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值