Galin Iliev's blog

Software Architecture & Development

Start/Stop IIS Site with C#

Have you ever had you deal with IIS management using C#? I mean IIS6?

for IIS 6 you had to deal with DirectoryEntry class like this (code got from here):

   1:  try
   2:  {
   3:      const string WebServerSchema = "IIsWebServer"; // Case Sensitive
   4:      string ServerName = "localhost";
   5:      DirectoryEntry W3SVC = new DirectoryEntry("IIS://" + ServerName + "/w3svc");
   6:      foreach (DirectoryEntry Site in W3SVC.Children)
   7:      {
   8:          if (Site.SchemaClassName == WebServerSchema)
   9:          {
  10:              Console.WriteLine(Site.Name + " - " + Site.Properties["ServerComment"].Value.ToString());
  11:          }
  12:      }
  13:  }
  14:  // Catch any errors
  15:  catch (Exception e)
  16:  {
  17:      Console.WriteLine("Error: " + e.ToString());
  18:  }

and once you find your site you had to execute DirectoryEntry.Invoke("Start", null) or DirectoryEntry.Invoke("Stop", null)  in order to start/stop the site.

Not very convinient but works :).

Well with IIS7 there are a good news and bad news... The bad news is this code won't work on IIS7 (and Vista).

The good news is that there is new .NET assembly () for managing IIS. It is located in %WinDir%\System32\InetSrv\Microsoft.Web.Administration.dll. It contains some wrapper classes that makes dev life much easier :)

with it the task above become:

   1:  ServerManager iisManager = new ServerManager();
   2:  iisManager.Sites["NewSite"].Stop(); 

Much prettier huh?! :)

for more info about Microsoft.Web.Administration.dll read CarlosAg blog's entry Microsoft.Web.Administration in IIS 7

Microsoft gives lessons how to design class libraries

MSR Technical Education Series: Designing .NET Class Libraries

Krzysztof Cwalina (co-author of Framework Design Guidelines) recently did a talk for the Microsoft Research Group on great framework design. 

I heard somewhere that .NET Framework started from about 6000 classes in version 1.0 and grew to 18 000 classes in version 3.0. I am not sure how precise are these numbers but they could give rough idea how big is .NET. All these classes should had been carefully designed so watch this video where Krzysztof Cwalina (program manager, .NET Framework Team, Microsoft) esplains how to design such libraries.

The video is more than 3 hours but I am sure it worths.

(via Brad Adams blog)

Sofia.NET UG meeting

Last week I had a talk on monthly meeting of Sofia.NET User group. The meeting was held in Microsoft Bulgaria office.

There was also beer and snacks kindly provided by Microsoft. There was also presents for the atendees :) You can take a look at pictures taken by Ruslan.

As I promised I publish slides and demos so everyone who is interested could play with them :)

Sofia.NET is free professional group so everyone is invited. If you want to become member and attend on our meetings register here.