Skip to main content

IIS Administration using Microsoft.Web.Administration using F#

A friend had mentioned his joy at using Powershell. I guess this is pretty cool and I don't mind Powershell. I sort of missed the boat a little with it because I haven't done any Windows Administration since I used to look after Windows Server 2000 machines (and possibly a couple of 2003). At that time I had a different arsenal to cause untold woe on my fellow colleagues....VBSCRIPT!!!! Boy could I cause trouble with that. With a combination of that, VBA and SQL I used to love creating spider webs of pure madness, once written the apps were tied together so precariously; one false move and the entire thing would explode.... anyway that's a different story.

Back to the Powershell. He was using it to automate IIS (or else I heard what I wanted to so I could try and push F# onto him, who knows?). I have heard various stories of extremely large platform automation scripts being written recently (for example .net rocks interview with Steve Evans) and whilst they seem to be going the right way (code reviews, source control etc....), one thing I don't like about these scripts is their loose typing. One false move, one little misspelling and you are done for. The scripting of infrastructure is business critical so it needs to be treated seriously.

Why not still use scripts but enjoy proper testability, strong typing and a wonderful easy to learn syntax? IT people are perfect candidates to learn F# because they're not tied down with all the ceremony and object orientated nonsense that a normal developer coming from C# suffers from. Also whilst the various Powershell IDE's are ok, they're no match for Visual Studio with the F# Powertools installed.

There are .NET libraries for AWS and Azure and if you are creating resources such as IIS Sites on your infrastructure you can just use Libraries such as Microsoft.Web.Administration for controlling IIS.

Below is an example of creating a live and acceptance site using Microsoft.Web.Administration library. It really is this easy.

I have had a more advanced version of this type of code at work for some time now. I originally made an app to migrate all of our old sites to a new server and automatically create the correct folder structures, sites and application pools.

Available on fssnip http://www.fssnip.net/s1

 1: #r "C:\\git\\bitbucket\\folder-generator\\packages\\Microsoft.Web.Administration.7.0.0.0\\lib\\net20\\Microsoft.Web.Administration.dll"
 2: 
 3: open System
 4: open Microsoft.Web.Administration
 5:                     
 6: let siteName = "doms-wicked-site"
 7: 
 8: let name(siteType: string) = 
 9:     String.Format("{0}_{1}", siteName, siteType)
10:         
11: let serverManager = new ServerManager()
12:         
13: let createSite(siteName: string) = 
14:     serverManager.Sites.Add(siteName, String.Format(@"C:\Sites\{0}", siteName), 80)
15: 
16: let applicationPool(poolName: string) = 
17:     let livePool:ApplicationPool = serverManager.ApplicationPools.Add(poolName)
18:     livePool.Enable32BitAppOnWin64 <- true;
19:     livePool.ManagedRuntimeVersion <- "v4.0";            
20:     livePool
21: 
22: let site(siteName: string, poolName: string) = 
23:     let liveSite:Site = createSite(siteName)
24:     liveSite.ServerAutoStart <- true
25:     liveSite.ApplicationDefaults.ApplicationPoolName <- poolName       
26:     liveSite.TraceFailedRequestsLogging.Enabled <- true;
27:     liveSite.TraceFailedRequestsLogging.Directory <- String.Format(@"C:\IISLogs\{0}", siteName)
28: 
29: let liveName = name("live")
30: let livePool = applicationPool(liveName)            
31: let liveSite = site(liveName, livePool.Name)
32: 
33: let accName = name("acc")
34: let accPool = applicationPool(accName)            
35: site(accName, accPool.Name) |> ignore
36:         
37: serverManager.CommitChanges()   
namespace System
namespace Microsoft
namespace Microsoft.Web
namespace Microsoft.Web.Administration
val siteName : string

Full name: Snippet.siteName

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
val name : string -> string

Full name: Snippet.name
val siteType : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
Multiple items
val string : 'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
type String =
  class
    new : char -> string
    new : char * int * int -> string
    new : System.SByte -> string
    new : System.SByte * int * int -> string
    new : System.SByte * int * int * System.Text.Encoding -> string
    new : char [] * int * int -> string
    new : char [] -> string
    new : char * int -> string
    member Chars : int -> char
    member Clone : unit -> obj
    member CompareTo : obj -> int
    member CompareTo : string -> int
    member Contains : string -> bool
    member CopyTo : int * char [] * int * int -> unit
    member EndsWith : string -> bool
    member EndsWith : string * System.StringComparison -> bool
    member EndsWith : string * bool * System.Globalization.CultureInfo -> bool
    member Equals : obj -> bool
    member Equals : string -> bool
    member Equals : string * System.StringComparison -> bool
    member GetEnumerator : unit -> System.CharEnumerator
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> System.TypeCode
    member IndexOf : char -> int
    member IndexOf : string -> int
    member IndexOf : char * int -> int
    member IndexOf : string * int -> int
    member IndexOf : string * System.StringComparison -> int
    member IndexOf : char * int * int -> int
    member IndexOf : string * int * int -> int
    member IndexOf : string * int * System.StringComparison -> int
    member IndexOf : string * int * int * System.StringComparison -> int
    member IndexOfAny : char [] -> int
    member IndexOfAny : char [] * int -> int
    member IndexOfAny : char [] * int * int -> int
    member Insert : int * string -> string
    member IsNormalized : unit -> bool
    member IsNormalized : System.Text.NormalizationForm -> bool
    member LastIndexOf : char -> int
    member LastIndexOf : string -> int
    member LastIndexOf : char * int -> int
    member LastIndexOf : string * int -> int
    member LastIndexOf : string * System.StringComparison -> int
    member LastIndexOf : char * int * int -> int
    member LastIndexOf : string * int * int -> int
    member LastIndexOf : string * int * System.StringComparison -> int
    member LastIndexOf : string * int * int * System.StringComparison -> int
    member LastIndexOfAny : char [] -> int
    member LastIndexOfAny : char [] * int -> int
    member LastIndexOfAny : char [] * int * int -> int
    member Length : int
    member Normalize : unit -> string
    member Normalize : System.Text.NormalizationForm -> string
    member PadLeft : int -> string
    member PadLeft : int * char -> string
    member PadRight : int -> string
    member PadRight : int * char -> string
    member Remove : int -> string
    member Remove : int * int -> string
    member Replace : char * char -> string
    member Replace : string * string -> string
    member Split : char [] -> string []
    member Split : char [] * int -> string []
    member Split : char [] * System.StringSplitOptions -> string []
    member Split : string [] * System.StringSplitOptions -> string []
    member Split : char [] * int * System.StringSplitOptions -> string []
    member Split : string [] * int * System.StringSplitOptions -> string []
    member StartsWith : string -> bool
    member StartsWith : string * System.StringComparison -> bool
    member StartsWith : string * bool * System.Globalization.CultureInfo -> bool
    member Substring : int -> string
    member Substring : int * int -> string
    member ToCharArray : unit -> char []
    member ToCharArray : int * int -> char []
    member ToLower : unit -> string
    member ToLower : System.Globalization.CultureInfo -> string
    member ToLowerInvariant : unit -> string
    member ToString : unit -> string
    member ToString : System.IFormatProvider -> string
    member ToUpper : unit -> string
    member ToUpper : System.Globalization.CultureInfo -> string
    member ToUpperInvariant : unit -> string
    member Trim : unit -> string
    member Trim : char [] -> string
    member TrimEnd : char [] -> string
    member TrimStart : char [] -> string
    static val Empty : string
    static member Compare : string * string -> int
    static member Compare : string * string * bool -> int
    static member Compare : string * string * System.StringComparison -> int
    static member Compare : string * string * System.Globalization.CultureInfo * System.Globalization.CompareOptions -> int
    static member Compare : string * string * bool * System.Globalization.CultureInfo -> int
    static member Compare : string * int * string * int * int -> int
    static member Compare : string * int * string * int * int * bool -> int
    static member Compare : string * int * string * int * int * System.StringComparison -> int
    static member Compare : string * int * string * int * int * bool * System.Globalization.CultureInfo -> int
    static member Compare : string * int * string * int * int * System.Globalization.CultureInfo * System.Globalization.CompareOptions -> int
    static member CompareOrdinal : string * string -> int
    static member CompareOrdinal : string * int * string * int * int -> int
    static member Concat : obj -> string
    static member Concat : obj [] -> string
    static member Concat<'T> : System.Collections.Generic.IEnumerable<'T> -> string
    static member Concat : System.Collections.Generic.IEnumerable<string> -> string
    static member Concat : string [] -> string
    static member Concat : obj * obj -> string
    static member Concat : string * string -> string
    static member Concat : obj * obj * obj -> string
    static member Concat : string * string * string -> string
    static member Concat : obj * obj * obj * obj -> string
    static member Concat : string * string * string * string -> string
    static member Copy : string -> string
    static member Equals : string * string -> bool
    static member Equals : string * string * System.StringComparison -> bool
    static member Format : string * obj -> string
    static member Format : string * obj [] -> string
    static member Format : string * obj * obj -> string
    static member Format : System.IFormatProvider * string * obj [] -> string
    static member Format : string * obj * obj * obj -> string
    static member Intern : string -> string
    static member IsInterned : string -> string
    static member IsNullOrEmpty : string -> bool
    static member IsNullOrWhiteSpace : string -> bool
    static member Join : string * string [] -> string
    static member Join : string * obj [] -> string
    static member Join<'T> : string * System.Collections.Generic.IEnumerable<'T> -> string
    static member Join : string * System.Collections.Generic.IEnumerable<string> -> string
    static member Join : string * string [] * int * int -> string
  end

Full name: System.String

  type: String
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
String.Format(format: string, args: obj []) : string
String.Format(format: string, arg0: obj) : string
String.Format(provider: IFormatProvider, format: string, args: obj []) : string
String.Format(format: string, arg0: obj, arg1: obj) : string
String.Format(format: string, arg0: obj, arg1: obj, arg2: obj) : string
val serverManager : ServerManager

Full name: Snippet.serverManager

  type: ServerManager
  implements: IDisposable
type ServerManager =
  class
    new : unit -> Microsoft.Web.Administration.ServerManager
    new : bool * string -> Microsoft.Web.Administration.ServerManager
    new : string -> Microsoft.Web.Administration.ServerManager
    member ApplicationDefaults : Microsoft.Web.Administration.ApplicationDefaults
    member ApplicationPoolDefaults : Microsoft.Web.Administration.ApplicationPoolDefaults
    member ApplicationPools : Microsoft.Web.Administration.ApplicationPoolCollection
    member CommitChanges : unit -> unit
    member Dispose : unit -> unit
    member GetAdministrationConfiguration : unit -> Microsoft.Web.Administration.Configuration
    member GetAdministrationConfiguration : Microsoft.Web.Administration.WebConfigurationMap * string -> Microsoft.Web.Administration.Configuration
    member GetApplicationHostConfiguration : unit -> Microsoft.Web.Administration.Configuration
    member GetMetadata : string -> obj
    member GetRedirectionConfiguration : unit -> Microsoft.Web.Administration.Configuration
    member GetWebConfiguration : string -> Microsoft.Web.Administration.Configuration
    member GetWebConfiguration : string * string -> Microsoft.Web.Administration.Configuration
    member GetWebConfiguration : Microsoft.Web.Administration.WebConfigurationMap * string -> Microsoft.Web.Administration.Configuration
    member SetMetadata : string * obj -> unit
    member SiteDefaults : Microsoft.Web.Administration.SiteDefaults
    member Sites : Microsoft.Web.Administration.SiteCollection
    member VirtualDirectoryDefaults : Microsoft.Web.Administration.VirtualDirectoryDefaults
    member WorkerProcesses : Microsoft.Web.Administration.WorkerProcessCollection
    static member OpenRemote : string -> Microsoft.Web.Administration.ServerManager
  end

Full name: Microsoft.Web.Administration.ServerManager

  type: ServerManager
  implements: IDisposable
val createSite : string -> Site

Full name: Snippet.createSite
val siteName : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
property ServerManager.Sites: SiteCollection
ConfigurationElementCollectionBase.Add(element: Site) : Site
SiteCollection.Add(name: string, physicalPath: string, port: int) : Site
SiteCollection.Add(name: string, bindingInformation: string, physicalPath: string, certificateHash: byte []) : Site
SiteCollection.Add(name: string, bindingProtocol: string, bindingInformation: string, physicalPath: string) : Site
val applicationPool : string -> ApplicationPool

Full name: Snippet.applicationPool
val poolName : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
val livePool : ApplicationPool

  type: ApplicationPool
  inherits: ConfigurationElement
type ApplicationPool =
  class
    inherit Microsoft.Web.Administration.ConfigurationElement
    member AutoStart : bool with get, set
    member Cpu : Microsoft.Web.Administration.ApplicationPoolCpu
    member Enable32BitAppOnWin64 : bool with get, set
    member Failure : Microsoft.Web.Administration.ApplicationPoolFailure
    member ManagedPipelineMode : Microsoft.Web.Administration.ManagedPipelineMode with get, set
    member ManagedRuntimeVersion : string with get, set
    member Name : string with get, set
    member ProcessModel : Microsoft.Web.Administration.ApplicationPoolProcessModel
    member QueueLength : int64 with get, set
    member Recycle : unit -> Microsoft.Web.Administration.ObjectState
    member Recycling : Microsoft.Web.Administration.ApplicationPoolRecycling
    member Start : unit -> Microsoft.Web.Administration.ObjectState
    member State : Microsoft.Web.Administration.ObjectState
    member Stop : unit -> Microsoft.Web.Administration.ObjectState
    member WorkerProcesses : Microsoft.Web.Administration.WorkerProcessCollection
  end

Full name: Microsoft.Web.Administration.ApplicationPool

  type: ApplicationPool
  inherits: ConfigurationElement
property ServerManager.ApplicationPools: ApplicationPoolCollection
ConfigurationElementCollectionBase.Add(element: ApplicationPool) : ApplicationPool
ApplicationPoolCollection.Add(name: string) : ApplicationPool
property ApplicationPool.Enable32BitAppOnWin64: bool
property ApplicationPool.ManagedRuntimeVersion: string
val site : string * string -> unit

Full name: Snippet.site
val liveSite : Site

  type: Site
  inherits: ConfigurationElement
type Site =
  class
    inherit Microsoft.Web.Administration.ConfigurationElement
    member ApplicationDefaults : Microsoft.Web.Administration.ApplicationDefaults
    member Applications : Microsoft.Web.Administration.ApplicationCollection
    member Bindings : Microsoft.Web.Administration.BindingCollection
    member GetWebConfiguration : unit -> Microsoft.Web.Administration.Configuration
    member Id : int64 with get, set
    member Limits : Microsoft.Web.Administration.SiteLimits
    member LogFile : Microsoft.Web.Administration.SiteLogFile
    member Name : string with get, set
    member ServerAutoStart : bool with get, set
    member Start : unit -> Microsoft.Web.Administration.ObjectState
    member State : Microsoft.Web.Administration.ObjectState
    member Stop : unit -> Microsoft.Web.Administration.ObjectState
    member ToString : unit -> string
    member TraceFailedRequestsLogging : Microsoft.Web.Administration.SiteTraceFailedRequestsLogging
    member VirtualDirectoryDefaults : Microsoft.Web.Administration.VirtualDirectoryDefaults
  end

Full name: Microsoft.Web.Administration.Site

  type: Site
  inherits: ConfigurationElement
property Site.ServerAutoStart: bool
property Site.ApplicationDefaults: ApplicationDefaults
property ApplicationDefaults.ApplicationPoolName: string
property Site.TraceFailedRequestsLogging: SiteTraceFailedRequestsLogging
property SiteTraceFailedRequestsLogging.Enabled: bool
property SiteTraceFailedRequestsLogging.Directory: string
val liveName : string

Full name: Snippet.liveName

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
val livePool : ApplicationPool

Full name: Snippet.livePool

  type: ApplicationPool
  inherits: ConfigurationElement
val liveSite : unit

Full name: Snippet.liveSite

  type: unit
  implements: IComparable
property ApplicationPool.Name: string
val accName : string

Full name: Snippet.accName

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
val accPool : ApplicationPool

Full name: Snippet.accPool

  type: ApplicationPool
  inherits: ConfigurationElement
val ignore : 'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
ServerManager.CommitChanges() : unit

Comments

Popular posts from this blog

Accessing the UI Thread with Tasks in F#

I have a Windows Forms program written in F# that can deploy a code base to n number of sites at once (you select the sites you would like to deploy to and it goes off and completes a number of tasks (backing up current sites, various unpacking and moving of files etc... ). Once you start it, it begins it's merry journey and begins to update the UI with what has happened. At the moment this method of updating the UI is not pretty because the threads I am doing the work on can't update the UI so I perform some fiendery to make that happen (don't ask). I knew there was a better way using some newer .NET features but I just hadn't got round to having a fiddle yet. I have now found that if you use the built in Task class but break your code up in a nicer way and then chain the tasks together you can then pass the correct context into the task that you want to talk to the UI. Here's a little script to give you a feel for it. You can press the "start" butt

An instantiated object should be "ok"

I've been QA'ing quite a bit of work recently and one common theme I've noticed across both Java and C# projects I have been looking at is that we occasionally open ourselves up unessacarily to Exceptions by the way objects are being created. My general rule of thumb (which I have seen mentioned in a Pluralsight video recently but also always re-iterate in various Robust Software talks I have done) is that you shouldn't be able to create an object and then call a method or access a property that then throws an exception. At worst, it should return null (I'm not going to moan about that now). I've created an example below. We have two Dojos, one is good and one is bad. The bad dojo looks very familiar though. It's a little class written in the style that seems often encouraged. In fact, many classes start life as something like this. Then as years go on, you and other colleagues add more features to the class and it's instantiation becomes a second

NESTA - Next Gen.

via nesta.org.uk Following on from an article on the BBC about Raspberry Pi, this next gen report has some interesting findings. The scariest stat which I picked out from the BBC website was "out of the 28,767 teachers who were awarded Qualified Teacher Status... in 2010, only three qualified in computing or computing science as their primary qualification" Having worked as a computer science teacher for a year in a school that was a specialist in Computing I can concur that the uptake in Comp Sci was woeful. 2 Students for A2... The other teachers backgrounds in Computer Science was also fairly woeful (most knowing a bit about Office but still a paltry amount even about that). I couldn't speak for my counterpart that I was covering however. I suspect they were fairly up on things. All in all what kills me is that Computer science is not a secondary level subject. Areas are often covered, a little in IT, a little in DT subjects (if kids choose Systems and Contr