In this article, we will look into creation of an IIS 7\7.5 website
and corresponding application pool programmatically using a C# console
application. These activities are most commonly done by an IIS
administrator. We can automate those activities using this application.
Let's create a console application and name it IISAutomation in Visual Studio 2010 by targeting .NET 3.5 as in the following:
Let's add a reference to Microsoft.Web.Administration that is present under C:\Windows\System32\inetsrv [IIS installation directory]. This assembly contains classes that a developer can use to administer the IIS Manager. First we will create an application pool using the following code:
Here,
we created an application pool by creating an instance of
ApplicationPool object. Then, we set the properties of the application
pool, like the name, .NET version to be used and the committed changes
by calling CommitChanges(). Similarly, we will create a website using
the following code:
Here,
we created a new web site by creating an instance of ServerManager,
adding it to the Sites collection and setting its properties like name,
physical path and so on and committed the changes.
We will use the preceding methods in our Main method and create an application pool and a web site using the following code:
Here,
we set the attributes necessary for web site and application creation
by getting input from the console. Let's run the application and input
the details as shown below:
Let's add a reference to Microsoft.Web.Administration that is present under C:\Windows\System32\inetsrv [IIS installation directory]. This assembly contains classes that a developer can use to administer the IIS Manager. First we will create an application pool using the following code:
- private static void CreateAppPool(string poolname,bool enable32bitOn64, ManagedPipelineMode mode,string runtimeVersion="v4.0")
- {
- using (ServerManager serverManager = new ServerManager())
- {
- ApplicationPool newPool = serverManager.ApplicationPools.Add(poolname);
- newPool.ManagedRuntimeVersion = runtimeVersion;
- newPool.Enable32BitAppOnWin64 = true;
- newPool.ManagedPipelineMode = mode;
- serverManager.CommitChanges();
- }
- }
- private static void CreateIISWebsite(string websiteName, string hostname, string phyPath, string appPool)
- {
- ServerManager iisManager = new ServerManager();
- iisManager.Sites.Add(websiteName, "http", "*:80:" + hostname, phyPath);
- iisManager.Sites[websiteName].ApplicationDefaults.ApplicationPoolName = appPool;
- foreach (var item in iisManager.Sites[websiteName].Applications)
- {
- item.ApplicationPoolName = appPool;
- }
- iisManager.CommitChanges();
- }
We will use the preceding methods in our Main method and create an application pool and a web site using the following code:
- static void Main(string[] args)
- {
- Console.WriteLine("Do you want to create an Application Pool:y/n");
- string response = Console.ReadLine();
- if (response.ToString() == "y")
- {
- Console.Write("Please enter Application Pool Name:");
- string poolname = Console.ReadLine();
- bool isEnable32bit = false;
- ManagedPipelineMode mode = ManagedPipelineMode.Classic;
- Console.Write("Need to enable 32 bit on Windows 64 bit?y/n [Applicable for 64 bit OS]: y/n?");
- string enable32bit = Console.ReadLine();
- if (enable32bit.ToLower() == "y")
- {
- isEnable32bit = true;
- }
- Console.Write("Please select Pipeline Mode: 1 for Classic, 2 for Integrated:");
- string pipelinemode = Console.ReadLine();
- if (pipelinemode.ToLower() == "2")
- {
- mode = ManagedPipelineMode.Integrated;
- }
- Console.Write("Please select Runtime Version for Application Pool: 1 for v2.0, 2 for v4.0:");
- string runtimeVersion = Console.ReadLine()== "1" ? "v2.0" : "v4.0";
- CreateAppPool(poolname, isEnable32bit, mode, runtimeVersion);
- Console.WriteLine("Application Pool created successfully...");
- }
- Console.WriteLine("Do you want to create a website:y/n");
- response = Console.ReadLine();
- if (response.ToString() == "y")
- {
- Console.Write("Please enter website name:");
- string websiteName = Console.ReadLine();
- Console.Write("Please enter host name:");
- string hostname = Console.ReadLine();
- Console.Write("Please enter physical path to point for website:");
- string phypath = Console.ReadLine();
- Console.WriteLine("Please enter Application pool Name:");
- foreach(var pool in new ServerManager().ApplicationPools)
- {
- Console.WriteLine(pool.Name);
- }
- Console.WriteLine("");
- Console.Write("Please enter Application pool Name for web site:");
- string poolName = Console.ReadLine();
- CreateIISWebsite(websiteName,hostname,phypath,poolName);
- Console.WriteLine("Web site created successfully...");
- Console.ReadLine();
- }
- }
No comments:
Post a Comment