Category Archives: Technical

These posts are technical in nature. The casual reader may not care about these posts. These are for my fellow geeks.

Change the runtime of an IIS 7 application pool

The following quick SDK sample demonstrates how to list and change the managed runtime version of application pools programatically in IIS 7.

[VB]

Imports System
Imports Microsoft.Web.Administration
Public Class AppPoolSample
 Shared manager As ServerManager = New ServerManager()
  ' Main application processing
  Public Shared Sub Main(ByVal args As String())
      ' Get the apppool to change
      Dim iPool As Integer = GetAppPool()
      ' Get the framework version desired
      Dim rtVersion As String = GetVersion()
      ' Set the apppool runtime
      Dim poolToSet As ApplicationPool = manager.ApplicationPools(iPool)
      Console.WriteLine(
            "Setting application pool '{0}' to runtime version: {1}...", _
            poolToSet.Name, rtVersion)
      poolToSet.ManagedRuntimeVersion = rtVersion
      ' Commit the changes and recycle the application pool
      manager.CommitChanges()
      poolToSet.Recycle()
      Console.WriteLine("Your changes have been committed.")
  End Sub

  ' Prompts the user to select an application pool
  Public Shared Function GetAppPool() As Integer
    Dim pool As String = String.Empty
    Dim iPool As Integer = 0
    While (Not Integer.TryParse(pool, iPool))
      Console.WriteLine("Available ApplicationPools: Managed runtime version")
      Dim i As Integer
      For i = 0 To manager.ApplicationPools.Count - 1 Step i + 1
        Dim appPool As ApplicationPool = manager.ApplicationPools(i)
        Console.WriteLine("{3}{0,3}.{3}{1}: {2}", i + 1, _
            appPool.Name, appPool.ManagedRuntimeVersion, vbTab)
      Next
      Console.Write("{0}Choose an application pool to change: ", vbCrLf)
      pool = Console.ReadLine()
    End While
    Return iPool - 1
  End Function

    ' Prompts a user to select the version of runtime they would like
    ' the application pool to use
    Public Shared Function GetVersion() As String
        Dim rtVersion As String = String.Empty
        Dim iVersion As Integer = 0
        While (Not Integer.TryParse(rtVersion, iVersion))
            Console.WriteLine("{0}  1.{0}Framework version 1.0", vbTab)
            Console.WriteLine("{0}  2.{0}Framework version 1.1", vbTab)
            Console.WriteLine("{0}  3.{0}Framework version 2.0", vbTab)
            Console.Write("Choose the new managed runtime version: ")
            rtVersion = Console.ReadLine()
        End While
        Select Case iVersion
            Case 1
                rtVersion = "v1.0"
            Case 2
                rtVersion = "v1.1"
            Case 3
                rtVersion = "v2.0"
        End Select
        Return rtVersion
    End Function
End Class

[C#]

using System;
using Microsoft.Web.Administration;

public class AppPoolSample 
{
  static ServerManager manager = new ServerManager();
  // Main application processing
  public static void Main(string[] args)  
  {
    // Get the apppool to change
    int iPool = GetAppPool();
    // Get the framework version desired
    string rtVersion = GetVersion();
    // Set the apppool runtime
    ApplicationPool poolToSet = manager.ApplicationPools[iPool];
    Console.WriteLine(
        "Setting application pool '{0}' to runtime version: {1}...",
        poolToSet.Name, rtVersion);
    poolToSet.ManagedRuntimeVersion = rtVersion;
    // Commit the changes and recycle the application pool
    manager.CommitChanges();
    poolToSet.Recycle();
    Console.WriteLine("Your changes have been committed.");
  }
  // Prompts the user to select an application pool
 public static int GetAppPool()
 {
    string pool = String.Empty;
    int iPool = 0;
    while ((!int.TryParse(pool, out iPool)) ||
            (iPool > manager.ApplicationPools.Count || iPool <= 0))
    {
      Console.WriteLine(
                    "Available ApplicationPools: Managed runtime version");
      for (int i = 0; i <= manager.ApplicationPools.Count - 1; i++)
      {
        ApplicationPool appPool = manager.ApplicationPools[i];
        Console.WriteLine("t{0,3}.t{1}: {2}", i + 1, 
            appPool.Name, appPool.ManagedRuntimeVersion);
      }
      Console.Write("rnChoose an application pool to change: ");
      pool = Console.ReadLine();
  }
  return iPool -1;
}
  // Prompts a user to select the version of runtime they would like
  // the application pool to use
  public static string GetVersion()
  {
    string rtVersion = String.Empty;
    int iVersion = 0;
    while ((!int.TryParse(rtVersion, out iVersion)) ||
            (iVersion > 3 || iVersion < 1))
    {
      Console.WriteLine("rnt   1.tFramework version 1.0");
      Console.WriteLine("t   2.tFramework version 1.1");
      Console.WriteLine("t   3.tFramework version 2.0");
      Console.Write("Choose the new managed runtime version: ");
      rtVersion = Console.ReadLine();
    }
    switch (iVersion)
    {
        case 1:
            rtVersion  = "v1.0";
            break;
        case 2:
            rtVersion = "v1.1";
            break;
        case 3:
            rtVersion = "v2.0";
            break;
    }
    return rtVersion;
  }
}

Security: Incompetence

It’s one type of incompetence to keep the personal identifiers and financial data of customers on your laptop and then lose ittwice;  It’s an entirely different type of incompetence that allows government data to be compromised through a network.  Last year at TechEd, a demo showed how a completely patched network could be compromised using an exploit in a web site.  The best part of the exploit was made possible due to turning on more functionality than was necessary. Namely, one issue in the demo was that the router configuration allowed port 80 and port 443 traffic — despite the fact that SSL was not in use on the web site. 

Regardless of the platform being used, many of these compromises are possible these days not due to the operating system itself, but due to assumptions made about users, lack of planning, or pure laziness of administrators and developers.  This is one major reason why I’m not a big fan of agile. Despite the best arguments I’ve heard for agile software development, I have witnessed too much emphasis on feature completion without regard to overall system security. I would encourage you all to read Michael Howard’s new book on the security development lifecycle (link provided below).

Whatever the case — whatever the cause — I would urge the community to pay attention to the recent news stories, learn to start protecting important data and please stop putting personal and financial information that doesn’t belong to you on your laptop!

For more Microsoft resources on security please check out the following:

General Security Websites:
http://www.microsoft.com/security/default.mspx

Blogs:
http://blogs.technet.com/msrc/default.aspx
http://blogs.msdn.com/michael_howard/

Books:
http://books.mcgraw-hill.com/getbook.php?isbn=0072260858
http://www.microsoft.com/MSPress/books/5957.asp
http://www.microsoft.com/MSPress/books/8753.asp
http://www.microsoft.com/mspress/books/6893.asp
http://www.microsoft.com/mspress/books/6788.asp
http://www.microsoft.com/mspress/books/6892.asp
http://www.microsoft.com/mspress/books/6432.asp

ADO.NET 2.0 Boot Camp

Sahil Malik, a prolific speaker, Microsoft MVP and author of “Professional ADO.NET 2.0” is holding a one-day ADO.NET boot camp in Charlotte next month.  If you are in the area, I think this class will definitely give you your money’s worth.  Sahil has a very unique way of teaching that is easy to follow and highly effective.  If you are going to be in the area on July 21st, and want to master ADO.NET, I would encourage you to take a look at this great opportunity in the Charlotte, NC.

Workaround: Adding a script map in IIS 5.1

I was contacted by a customer who commented that he could not add a Script Map to IIS 5.1.  After selecting his executable for the script map and adding his extension, the “OK” button was still disabled — preventing him from committing the script map change.

To work around this issue, once you have selected the executable and set the extension, click inside the “Executable” text box to expand the full path to the executable.  Doing so will enable the OK button and you will be able to commit your script map change.

Bill Gates affect on my mood

Being a new employee at Microsoft leaves me at a bit of a disadvantage in weeks like this.  First, we hear that our “chief blogging officer”, Robert Scoble is leaving the company.  Today, it’s been announced that BillG will be stepping back his responsibilities at the companies once again.  It is not like this wasn’t expected, but it is a bit disheartening, none-the-less. 

Bill Gates has had a profound affect on my life.  I grew up in a poor town with family that worked VERY hard to provide for us, but by no means were we wealthy.  However, when I was writing code on my Atari 800XL in fifth grade, I told my mom that I would be working for Microsoft one day.  Since then, Bill Gates’ drive has inspired this poor kid from East Liverpool, OH to succeed.  No matter what life throws at you, who makes fun of you or attacks you with pies you can always come out on top.  Like BillG, I didn’t finish college — but unlike Mr. Gates, I quit because I ran out of money.  None-the-less, in my mind, I said “If Bill can succeed without a degree, so can I!”  Over the years, my life has thrown one curve ball after another at me.  I’ve had good times and bad. The one common factor in all those years has been Microsoft software;  I’ve made my career of it; I’ve based my life on it;  My house and my belt clip are embedded with it;  I’ve tied my success (if I can call it that) to it.  So with all of this said, it’s a deeply saddening experience to see the man who is responsible for getting it all started is now slowly backing out of his role here.

However…

If you talk to the larger percentage of Microsoft employees, most of us have had little to no interaction with Bill. Our software goes in and out the door much without his micro-management or interference.  How much of the Microsoft Office 2007 feature set was designed or coded by Mr. Gates? What drivers for Windows Vista do you think Bill had a part in creating?  My guess is, very little of our software is directly controlled by Bill.  Instead, he delegates those responsibilities as he sees fit.

I think it’s great that Bill gets to follow this path with his life. There is such an emphasis on giving at Microsoft. I’m proud to be part of a company that thinks about more than what goes on inside its own walls.  I, too, intend to do charity work full time at some point in my life. I think, one again, Bill is setting an example for my own life.  I obviously will not likely be able to do this to the same degree that Bill Gates is, but the example has been set and I admire the man.

Microsoft will go on making great software on and off the desktop for years to come and Bill is responsible for the spark that started this software industry off in such spectacular fashion. It’s time for someone else to take the torch and move on while Bill starts a new spark in charitable giving. If it’s possible for someone 11 levels under Mr Gates to say this, I’m proud of him for doing this and I can’t wait to see what he accomplishes with The Gates Foundation when he goes full time with it in 2008.

Good luck, Mr. Gates, and God Bless.

ASP.NET 1.1, IIS 6.0 and 64-bit Windows

A few days ago, one of the many distribution lists I belong to was presented with the following requests:

I’m trying to create a Web Service in Visual Studio .NET 2003 and am getting an error “Visual Studio .NET has detected that the specified Web server is not running ASP .NET version 1.1. You will be unable to run ASP .NET Web applications or services.”

The individual said that he had checked the script maps and everything seemed to be in order on the server.  They had installed and uninstalled the ASP.NET extensions several times using aspnet_regiis.exe.  After a few more communications the poster added:

(Potential complication: my machine is a 64-bit OS; does this change the equation?)

The answer is that running 64-bit Windows does have an affect on your ability to run ASP.NET 1.1 in IIS 6.  ASP.NET 1.1 only supports running in 32-bit mode.  Fortunately, IIS 6 on 64-bit Windows can run in either 64-bit mode or 32-bit mode.

The following steps to run IIS 6 in 32-bit mode can be found in our MSDN documentation:

  1. Click Start, click Run, type cmd, and then click OK.
  2. Type the following command to enable the 32-bit mode:

    cscript %SYSTEMDRIVE%inetpubadminscriptsadsutil.vbs SET W3SVC/AppPools/Enable32bitAppOnWin64 1

  3. Type the following command to install the version of ASP.NET 1.1 and to install the script maps at the IIS root and under:

    %SYSTEMROOT%Microsoft.NETFrameworkv1.1.4322aspnet_regiis.exe -i

  4. Make sure that the status of ASP.NET version 1.1.4322 is set to Allowed in the Web service extension list in Internet Information Services Manager.

After following these instructions, the issue still wasn’t resolved.  In fact, the script maps for the web application were not being properly updated.  I had the customer execute the “aspnet_regiis -ua” which would remove all versions of ASP.NET from the machine.  To reinstall the ASP.NET 1.1 again, you then need to reissue the “aspnet_regiis -i” command (use “aspnet_regiis -i -enable” if you are using Windows 2003).  This should allow you to run ASP.NET 1.1 on IIS 6.

Keep in mind, however, that IIS 6.0 cannot run in both 64-bit mode and 32-bit mode at the same time. By running IIS 6.0 in 32-bit mode on 64-bit Windows, ASP.NET 2.0 applications will also run in 32-bit mode.

Improving code performance

Recently, an old co-worker contacted me to ask me a question about code performance. Specifically, he was emitting IL from his code and had some questions about some of the opcode usage he witnessed when viewing the IL of some compiled assemblies.  The question was based on a simple application he wrote in C#, compiled, and disassembled.  He did this to see how the C# compiler produced IL and give him clues in how he should emit IL.  The function in question was as follows:


public object GetProp(string name)
{   if (name == “X”
   {    return this.X;
   }
   return null;
}

Now, the code obviously isn’t meant to do anything other than lend some insight into the IL.  Compiling to ‘debug’ the following IL was produced.

.method public hidebysig instance object
        GetProp(string name) cil managed
{
// Code size       35 (0x23)
.maxstack  2
.locals init ([0] object CS$1$0000,
          [1] bool CS$4$0001)
IL_0000:  nop
IL_0001:  ldarg.1
IL_0002:  ldstr      “X”
IL_0007:  call       bool [mscorlib]System.String::op_Equality(string, string)
IL_000c:  ldc.i4.0
IL_000d:  ceq
IL_000f:  stloc.1
IL_0010:  ldloc.1
IL_0011:  brtrue.s   IL_001d
IL_0013:  nop
IL_0014:  ldarg.0
IL_0015:  call       instance string class TestApp.TClass`1::get_X()
IL_001a:  stloc.0
IL_001b:  br.s       IL_0021
IL_001d:  ldnull
IL_001e:  stloc.0
IL_001f:  br.s       IL_0021
IL_0021:  ldloc.0
IL_0022:  ret
} // end of method TClass`1::GetProp

The question was, why was the stloc.1 and ldloc.1 needed after the ceq instruction at IL_000d (there are actually other issues in this small snippet, but I’ll focus on this particular one) . I, too, tried to resolve the issue and batted a few guesses around.  I proffered two ideas, and then ultimately suggested that the JIT compiler would likely be modifying this code anyway (particularly once it was recompiled in ‘release’ with optimization).

Still curious as to why the compiler produced the stloc and ldloc opcodes, I asked around internally until Vance set me straight with this blog post.

Introduction: What does ‘foreach’ actually do?
http://blogs.msdn.com/vancem/archive/2006/02/20/535807.aspx


Essentially, he states what I initially felt — that the JIT transformations on the IL are so dramatic, that you cannot judge an application’s performance based on the IL.  He also gives some great information on how to view your JITed code — with release optimizations and everything.  The other side to this is, that after further review, the inefficiencies of the IL were fixed in the optimized IL anyway once the code was set to ‘release’. 

Sometimes, it’s really easy to get side-tracked by these discussions in your quest for software glory.  I’m glad to know we have people like Vance around to set me straight when I do.


IIS 7 on a MacBook !

The IIS 7 Commander-in-chief, Bill Staples , has just released a screenshot of his new 17″ MacBook running IIS 7!  I simply must get one now!

Beta 2 Live – IIS.NET now open!

Now that Beta 2 has been announced and IIS 7 is now publicly available to beta testers on both the Vista and Longhorn platforms, our new portal, IIS.NET is now open and live!  Many long hours were spent on this by some great guys here at Microsoft.  Be sure to stop by and tell them what you think of the site!

IIS 7.0 UE team is hiring!

We’re hiring!  Take a look at the details below

“Do you love Internet and Web server technologies? Join the IIS UE team and work on the coolest version of Internet Information Services (IIS) ever. IIS 7.0 joins forces with ASP.NET to deliver a Web application development platform that’s getting rave reviews from customers.

We’re looking for a programmer writer to define and deliver essential developer-focused, solution-based documentation for a programming audience. Your responsibilities will include writing API reference topics, conceptual topics about IIS, and code examples that demonstrate product features. You’ll be responsible for multiple feature areas, so good organizational skills and the ability to prioritize your workload are a must.”


(continue)