You also can build console applications that would allow you to interface with the various server objects to give you quick access to executing requests, site status, or ASP.NET applicat
Trang 1MWA, like all of our other toolsets, can only work against an IIS 7.0 server, thus causing you to use other administration tools to administer previous versions of IIS
Getting Started with MWA
Using Microsoft.Web.Administration isn’t necessarily the most convenient methodology for many
administrators However, there are a great deal of developers who would like to set up packages for
their Web applications using Visual Studio, and MWA makes this extremely simple Imagine you are
a developer for your company and you have an HR application you have built and would like to
create the setup so that all administrators are required to do is click setup.exe With your project in
Visual Studio, you can easily add a reference in your setup project to MWA and add logic to execute confi guration changes to IIS 7.0
You also can build console applications that would allow you to interface with the various server objects to give you quick access to executing requests, site status, or ASP.NET application domains
In our example, we will take you through doing some simple administration using a compiled
executable that is reusable throughout your Web infrastructure
Using C# Express to Create a Console Application
The fi rst step is to download a fl avor of Visual Studio 2005 that works for you For many, you already have Visual Studio 2005 and can’t skip this step However, if you do not have access to Visual Studio, you can download one of the Visual Studio Express Editions, such as Visual C# Express (available for download at http://msdn.microsoft.com/vstudio/express/visualcsharp/)
To create a console application project in C# express, add a reference to the Microsoft.Web
Administration library To do this, perform the following steps:
1 Open Visual Studio 2005 or Visual C# Express Edition
2 Enter your project name and select your save location
3 Click Project and Select Add Reference.
4 Click the Browse tab
5 Browse to %windir%\system32\inetsrv
6 Select Microsoft.Web.Administration.dll
7 Click OK
Trang 2Figure 16.8 Create Your MWA Project
SOME INDEPENDENT ADVICE
Microsoft.Web.Administration shipped as part of Windows Vista The entire API documentation is available on Microsoft’s MSDN Web site at http://msdn2.microsoft com/en-us/library/microsoft.web.administration
Trang 3Figure 16.9 Add an MWA Reference on Windows Vista
Accessing Runtime Information with MWA
As you have learned, MWA is simple and easy to use to create Web sites, application pools, and
applications It can also be used easily access confi guration attributes and properties on those Web sites such as authentication, default documents, and much more
IIS 7.0 offers more than just confi guration data—server objects and runtime data are also
accessible using MWA This includes server objects such as site status, application pool status, and
ASP.NET application domains These state objects are not represented in any confi guration fi le and
instead are stored within the worker process or Web Application Services (WAS) process
Creating a Web Site Using MWA
It is a fundamental fi rst step as we have seen when doing administration to create a Web site You will need to understand how to instantiate the MWA objects and create the new site Lastly, you will call the commit method to ensure that the new changes are committed to fi le
Trang 4using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Web.Administration;
namespace MSWebAdmin_Application
{
private static void CreateSite()
{
using (ServerManager serverManager = new ServerManager() )
{
Site site = serverManager.Sites.Add(“My First MWA Site”, @“%SystemDrive%\ inetpub\wwwroot”, 8080);
site.ServerAutoStart = true;
serverManager.CommitChanges();
}
}
In this sample, we grabbed the ServerManager object and created a new instance After that, we simply set the variable (myFirstMWASite) so we can use it later while also creating the new site using the Add method Lastly, we set the site auto start status to True so that after creating the site it will
automatically be started
Shortcut…
Setting the Auto Start Status
This is often a confusing concept for IIS veterans What exactly does the term site status mean? It is important to understand that it is possible to have a site created though not running This is often the case in staging or development environments
When ServerAutoStart is set to false, you cannot access the Web site using the bindings
(e.g., URL) until the administrator or site administrator enables it
This step isn’t required since the site will auto-start when you create it unless you explicitly set it to false It should be noted that if you are creating a site without
providing bindings that are unique, you should set ServerAutoStart to false until you
set the bindings to be unique
Trang 5Creating Virtual Directories Using MWA
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Web.Administration;
namespace MSWebAdmin_Application
{
Class CreateVirtualDirectory(“My First MWA Site”,“/”, “/VDir”, “C:\\temp”);
private static void CreateVirtualDirectory(string siteName, string
applicationPath, string virtualDirectoryPath, string physicalPath)
{
using (ServerManager serverManager = new ServerManager() )
{
Site site = serverManager.Sites[siteName];
Application app = site.Applications[applicationPath];
app.VirtualDirectories.Add(virtualDirectoryPath, physicalPath);
serverManager.CommitChanges();
}
}
In this example, we create a virtual directory called for the site My First MWA site with the
one single application In this case, we point the root application and virtual directory content
path to c:\temp
Adding Application Pools Using MWA
In order to set My First MWA Site to run as isolated and in its own application pool, you must
create the application pool and then assign the site’s root application to run in this newly created
application pool
In this example, we will accomplish two goals:
1 Create a new application pool
2 Assign the path My First MWA Site/ (root application) to your new application pool