Introduction
If you ever had the need to create a large number of sites in your IIS server or servers, this article will show you how it is easily done. This little console application
generates sites and the required application pools following an XML template you provide. The code for reading values from the XML template is provided in the sample code for download.
Using the code
First we need to add a reference to "Microsoft.Web.Administration.dll" which is located in
"C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll". The Microsoft.Web.Administration
namespace contains classes that a developer can use to administer
IIS Manager.
We have our template:
="1.0"="utf-8"
<SiteTemplate>
<Site name="[sitePrefix].test.domain.com" path="D:\[sitePrefix]\test" port="80">
<Application alias="/ResourceManager" path="D:\[sitePrefix]\admin.rm"></Application>
<VirtualDirectory alias="/RMResources" path="D:\FS\ResourceManager"></VirtualDirectory>
<VirtualDirectory alias="/WebResources"
path="D:\[sitePrefix]\_shopWebResources"></VirtualDirectory>
<ApplicationPool name="[sitePrefix].test.domain.com" framework="v4.0"
IdentityUserName="" IdentityPassword=""></ApplicationPool>
</Site>
<Site name="[sitePrefix].test.services.domain.com"
path="D:\[sitePrefix]\services" port="80">
<ApplicationPool name="[sitePrefix].test.services.domain.com"
framework="v4.0" IdentityUserName=""
IdentityPassword=""></ApplicationPool>
</Site>
</SiteTemplate>
Each site can consist of many applications, virtual directories, and only one application pool. You must have noticed the [sitePrefix]
string.
This is useful if you need to create different sites using similar names and settings, like in my case we needed to create feature1.mydomain.com,
feature2.mydomain.com so you can do this using the same template with different values.
To start administering IIS:
ServerManager serverManager = new ServerManager();
Creating a site in IIS si done like this:
Site site = serverManager.Sites.Add(name, path, port);
site.ApplicationDefaults.ApplicationPoolName = name;
Binding oBinding = site.Bindings.CreateElement();
oBinding.Protocol = "http";
oBinding.BindingInformation = string.Format("*:{0}:{1}", port, name);
site.Bindings.Clear();
site.Bindings.Add(oBinding);
site.ServerAutoStart = true;
If you want to add an application to your site:
site.Applications.Add(alias, physicalPath);
Or virtual directory:
site.Applications[0].VirtualDirectories.Add(alias, physicalPath);
Application[0]
means the root application (site). Application[1]
is the application within the site structure.
If we need to create a custom application pool:
ApplicationPool newPool = _serverManager.ApplicationPools["CustomPool"];
if (newPool == null)
{
serverManager.ApplicationPools.Add("CustomPool");
newPool = serverManager.ApplicationPools["CustomPool"];
newPool.Name = "CustomPool";
newPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;
newPool.ManagedRuntimeVersion = "v4.0";
newPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
newPool.ProcessModel.UserName = "MyCustomUsername";
newPool.ProcessModel.Password = "MyCustomPassword";
}
When we are done, just call:
serverManager.CommitChanges();
Now would be a good idea to restart you IIS server.
In the supplied sample code you can see in detail how sites are created reading values from the template. Customize the template for your needs.
Hope this is helpful.