简体   繁体   中英

How to enable 32-bit applications mode in IIS 6 and IIS 7 using c#

I want to change Enable32BitAppOnWin64 property using C#. I know that the way of interacting with IIS 6 and IIS 7 are different. but I need the solution for both versions.

There are a few differences in programmatically managing IIS 6 and IIS 7.

IIS 6 is programmatically managed using the DirectoryEntry class and the metabase database API.

IIS 7 is managed using the Microsoft.Web.Administration assembly and the ServerManager class.

Furthermore IIS 6 is not able to run both 64 bit and 32 bit worker processes at the same time (see this MSDN BLOG ). So setting Enable32BitAppOnWin64 to true for IIS 6 means that all worker processes (all application pools) are running as 32 bit processes.

IIS 7 is capable of running 64 bit and 32 bit worker processes at the same time. This means that you set Enable32BitAppOnWin64 for a specific application pool and not for all application pools.

You also have to detect the version of IIS in order to use the correct API. This can be done by reading the following DWORD values from the registry (for more information see Learn IIS ):

HKLM\Software\Microsoft\InetStp\MajorVersion and
HKLM\Software\Microsoft\InetStp\MinorVersion

So, here is some code to set Enable32BitAppOnWin64 for IIS 6 and IIS 7 (please note that you have to reference the Microsoft.Web.Administration and System.DirectoryServices assemblies in your Visual Studio project):

private static Version GetIISVerion()
{
  using (RegistryKey inetStpKey = 
    Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\InetStp"))
  {
    int majorVersion = (int)inetStpKey.GetValue("MajorVersion");
    int minorVersion = (int)inetStpKey.GetValue("MinorVersion");

    return new Version(majorVersion, minorVersion);
  }
}

private static void Enable32BitAppOnWin64IIS7(string appPoolName)
{
  Console.Out.WriteLine("Setting Enable32BitAppOnWin64 for {0} (IIS7)", appPoolName);
  using (ServerManager serverMgr = new ServerManager())
  {
    ApplicationPool appPool = serverMgr.ApplicationPools[appPoolName];
    if (appPool == null)
    {
      throw new ApplicationException(String.Format("The pool {0} does not exist", appPoolName));
    }

    appPool.Enable32BitAppOnWin64 = true;
    serverMgr.CommitChanges();
  }
}

private static void Enable32BitAppOnWin64IIS6(string serverName)
{
  Console.Out.WriteLine("Setting Enable32BitAppOnWin64 for IIS6");
  using (DirectoryEntry appPools = 
    new DirectoryEntry(String.Format("IIS://{0}/W3SVC/AppPools", serverName)))
  {
    appPools.Properties["Enable32BitAppOnWin64"].Value = true;

    appPools.CommitChanges();
  }
}    

public static void Enable32BitAppOnWin64(string serverName, string appPoolName)
{
  Version v = GetIISVerion(); // Get installed version of IIS

  Console.Out.WriteLine("IIS-Version: {0}", v);

  if (v.Major == 6) // Handle IIS 6
  {
    Enable32BitAppOnWin64IIS6(serverName);
    return;
  }

  if (v.Major == 7) // Handle IIS 7
  {        
    Enable32BitAppOnWin64IIS7(appPoolName);
    return;
  }

  throw new ApplicationException(String.Format("Unknown IIS version: {0}", v.ToString()));
}


static void Main(string[] args)
{
  Enable32BitAppOnWin64(Environment.MachineName, "DefaultAppPool");
}

I should also mention that there is a possibility to use the metabase API for IIS 7, too. On Windows Server 2008 operating systems you can install a role service called "IIS 6 Management Compatibility". This role service enables you to use the "old" IIS 6 API to manage IIS 7.

If "IIS 6 Management Compatibility" is an option for you change the function Enable32BitAppOnWin64IIS7 as follows:

private static void Enable32BitAppOnWin64IIS7(string serverName, string appPoolName)
{
  Console.Out.WriteLine("Setting Enable32BitAppOnWin64 for {0} (IIS7)", appPoolName);

  using (DirectoryEntry appPools = 
    new DirectoryEntry(String.Format("IIS://{0}/W3SVC/AppPools/{1}", serverName, appPoolName)))
  {
    appPools.Properties["Enable32BitAppOnWin64"].Value = true;

    appPools.CommitChanges();
  }
}

Of course, then you do not have to reference the Microsoft.Web.Administration assembly.

Rather than trying to reproduce someone elses answers, heres a link to a previous asker and the answers they received. I hope they are helpful.

IIS Application Pool: change settings programmatic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM