IIS7 – post #9 – Reading MIME Types w/code

I try to visit as many IIS7 places as possible.  One item mentioned not in the IIS7 GUI is how to add MIME types.  Well, I couldn't figure how to add them but I can 'read' them from my Vista RC1 machine.  How to add will be in another posting.  This code below doesn't build a list, but gives the basics for reading the values stored in the applicationHost.config file.  Happy IIS7 geeking!

Dim sm As New Microsoft.Web.Administration.ServerManager()
Dim appHostConfig As Configuration = sm.GetApplicationHostConfiguration()
Dim section As ConfigurationSection = appHostConfig.GetSection("system.webServer/staticContent")

For Each element As ConfigurationElement In section.GetCollection()
        Dim fileExtension As String = element.Item("fileExtension").ToString
        Dim mimeType As String = element.Item("mimeType").ToString
Next

Vista RC1, IIS7 – Microsoft.Web.Administration code samples

I installed Vista RC1 / 5600 edition and have been experimenting with the Microsoft.Web.Administration namespace. These are examples I got working on my local machine running Vista Ultimate Edition. Hope these help. If you find a more efficient way, by all means pass them along! These are my first attempt to understand and learn the new Microsoft.Web.Administration Namespace.

'Create a new site called SteveSchofield.net
Dim objSite As New Microsoft.Web.Administration.ServerManager
objSite.Sites.Add("SteveSchofield.net", "c:domainssteveschofield.net", 80)
objSite.Sites("SteveSchofield.net").ServerAutoStart = True
objSite.CommitChanges()

'Add an ManagedPipelineMode.Integrated application pool
Dim objAppPool As New Microsoft.Web.Administration.ServerManager
objAppPool.ApplicationPools.Add("SteveSchofield.net")
objAppPool.ApplicationPools("SteveSchofield.net").ManagedPipelineMode = ManagedPipelineMode.Integrated
objAppPool.ApplicationPools("SteveSchofield.net").AutoStart = True
objAppPool.CommitChanges()

'Add an ManagedPipelineMode.Classic application pool
Dim objAppPool As New Microsoft.Web.Administration.ServerManager
objAppPool.ApplicationPools.Add("SteveSchofield.net")
objAppPool.ApplicationPools("SteveSchofield.net").ManagedPipelineMode = ManagedPipelineMode.Classic
objAppPool.ApplicationPools("SteveSchofield.net").AutoStart = True
objAppPool.CommitChanges()

'Create a Site, Application Pool (ManagedPipelineMode.Integrated) and add new site to the new App pool that was just created.
Dim objSite As New Microsoft.Web.Administration.ServerManager
objSite.Sites.Add("SteveSchofield.net", "c:domainssteveschofield.net", 80)
objSite.ApplicationPools.Add("SteveSchofield.net").ManagedPipelineMode = ManagedPipelineMode.Integrated
objSite.Sites("SteveSchofield.net").Applications("/").ApplicationPoolName = "SteveSchofield.net"
objSite.CommitChanges()

'Create a Site, Application Pool (ManagedPipelineMode.Classic) and add new site to the new App pool that was just created.
Dim objSite As New Microsoft.Web.Administration.ServerManager
objSite.Sites.Add("SteveSchofield.net", "c:domainssteveschofield.net", 80)
objSite.ApplicationPools.Add("SteveSchofield.net").ManagedPipelineMode = ManagedPipelineMode.Classic
objSite.Sites("SteveSchofield.net").Applications("/").ApplicationPoolName = "SteveSchofield.net"
objSite.CommitChanges()

'List application pools using IIS7 namespace
Dim Server As New Microsoft.Web.Administration.ServerManager
Dim col As ApplicationPoolCollection
Dim AppPoolName as string
Dim x as integer

col = Server.ApplicationPools

For x = 0 To col.Count – 1
 AppPoolName = col(x).Name
Next

'List sites using IIS7 namespace
Dim Server As New Microsoft.Web.Administration.ServerManager
Dim col As SiteCollection
Dim SiteName as string
Dim x as integer

col = Server.Sites

For x = 0 To col.Count – 1
 SiteName = col(x).Name
Next

'List all worker processes using IIS7 namespace. (Note this is just an example, there are other properties that can retrieve information about worker processes).
Dim Server As New Microsoft.Web.Administration.ServerManager
Dim col As Microsoft.Web.Administration.WorkerProcessCollection
col = Server.WorkerProcesses
dim x as integer
For x = 0 To col.Count – 1
 Response.Write(col.Item(x).ApplicationDomains(x).Id & "<BR>")
 Response.Write(col.Item(x).ApplicationDomains(x).Idle & "<BR>")
 Response.Write(col.Item(x).ApplicationDomains(x).PhysicalPath & "<BR>")
 Response.Write(col.Item(x).ApplicationDomains(x).VirtualPath & "<BR>")
 Response.Write(col.Item(x).ApplicationDomains(x).WorkerProcess.ProcessGuid & "<BR>")
Next

'Recycle Application Pool
Dim RecycleSite As New ServerManager
RecycleSite.ApplicationPools("SteveSchofield.net").Recycle()

'WebPages to help test if recycle actually worked.
Place all webpages in the http://localhost website running IIS7
Create webpage called Default.aspx
Create webpage called Recycle.aspx
Create webpage called Recycle.aspx.vb
Paste the code listed below for the 'Default.aspx webpage
Paste the code listed below for the Recycle.aspx webpage
Paste the code listed below for the Recycle.aspx.vb webpage

'Test out the process
Open http://localhost/default.aspx?id=Show
This will output the datetime session variable to the webpage
Change the URL to http://localhost/default.aspx?id=Hide
Refresh the webpage, the Session variable won't change
Run the RecycleSite code in a separate webpage
Refresh the default.aspx webpage
The default.aspx webpage should not show any values

'Default.aspx to show session variable
<html>
<body>
 <% If Request.Querystring("ID") = "Show" Then %>
  <% Session("Steve") = System.DateTime.Now() %>
 <% End If %>

 <% response.write(Session("Steve")) %>
</body>
</html>

'Recycle.aspx webpage
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
  <body>
  <form id="form1" runat="server">
 

  Click Button to recycle

   

 </form>
  </body>
</html>

'Recycle.aspx.vb code behind file
Imports Microsoft.Web.Administration
Partial Class _Default
System.Web.UI.Page

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

 Dim RecycleSite As New ServerManager
 RecycleSite.ApplicationPools("SteveSchofield.net").Recycle()

End Sub
End Class

IIS7 – post #8

Here is sample code to return a collection of Application Pools using WMI and IIS7.  This is dynamic because this could be used to retrieve from a local machine or altering the code to connect to a remote server running IIS7.  Here is an article that explains what IIS7 features are on various versions of Vista.  This is bound to confuse! 🙂  http://www.iis.net/1100/SinglePageArticle.ashx

'Using System.Management to retrieve WMI / IIS7 info.
'Define the WMI connection information
Dim options As New System.Management.ConnectionOptions()
'options.Username = strUID
'options.Password = strPWD

'Define the Scope information / Note the path defined.
Dim scope As System.Management.ManagementScope
scope =
New System.Management.ManagementScope(\.rootWebAdministration)

'Define Query and Searcher objects
Dim WMIQuery As New System.Management.SelectQuery("SELECT * FROM ApplicationPool")
Dim searcher As New System.Management.ManagementObjectSearcher(scope, WMIQuery)

'Connect to WMI
Try
       scope.Connect()
Catch ex As Exception
       Exit Sub
End Try

'Dim variables for information that will be returned
Dim AppPoolName As System.Management.ManagementObject
Dim col As System.Management.ManagementObjectCollection

'Return the collection
col = searcher.Get()

'Write the list of Application Pools to webpage
For Each AppPoolName In col
       Response.Write(AppPoolName.GetPropertyValue(
"Name").ToString() & "<BR>")
Next

Here is the IIS7'ish way of using the Microsoft.Web.Administration
Dim Server As New Microsoft.Web.Administration.ServerManager
Dim col As ApplicationPoolCollection

col = Server.ApplicationPools

Dim AppPoolName As String = ""
Dim x As Integer = 0

For x = 0 To col.Count – 1
    AppPoolName = col(x).Name
Next

'Return a list of Sites
Dim Server As New Microsoft.Web.Administration.ServerManager
Dim col As SiteCollection

col = Server.Sites

Dim SiteName As String = ""
Dim x As Integer = 0


For x = 0 To col.Count – 1
 SiteName = col(x).Name
Next

IIS7 – post #7

Here is a good article I came across on MSDN / IIS7.  It was written in July 2006. 

Extending IIS7 Runtime Infrastructure and Management Components
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dniis/html/iis7run.asp

Summary: This article demonstrates how you can leverage the modular architecture of Internet Information Server 7.0 (IIS7) to extend the functionality of the core Web server. In addition, it takes advantage of new management service and tool extensibility features to create a compelling sample solution that increases the Web server feature set.

IIS7 WMI beta reference link.  Many of these links show returning information using VBScripts.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/IIS_70_AdminSDK/html/7b785db0-a6d8-5278-b524-bdcfc0463613.asp

IIS7 – post #6

Looking at the appcmd.exe utility lately in IIS7?  If not, here is a good article http://www.iis.net/default.aspx?tabid=2&subtabid=25&i=954.   I was also trying to review all the 'objects' and what each of them would do.   I found an XML file called 'appcmd.xml' in C:WindowsSystem32inetsrv which is a nice GUI to display all objects instead of typing out the commands. This opened in IE for me.  I'm all for command line power and ease of use.  To get familar quickly, reviewing the appcmd.XML file make it quicker.  I recommend checking it out.