如何使用c#在IIS 6和IIS 7中启用32位应用程序模式 [英] How to enable 32-bit applications mode in IIS 6 and IIS 7 using c#

查看:1557
本文介绍了如何使用c#在IIS 6和IIS 7中启用32位应用程序模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用C#更改 Enable32BitAppOnWin64 属性。我知道与 IIS 6 IIS 7 进行交互的方式不同。但我需要两个版本的解决方案。

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.

推荐答案

在以编程方式管理IIS 6和IIS 7方面存在一些差异。

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

使用 DirectoryEntry 类和元数据库数据库API以编程方式管理IIS 6.

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

IIS 7使用 Microsoft.Web.Administration 程序集
ServerManager 上课。

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

此外,IIS 6无法同时运行64位和32位工作进程
(请参阅 MSDN博客)。
因此,为IIS 6设置 Enable32BitAppOnWin64 true 意味着所有worker
进程(所有应用程序)池)以32位进程运行。

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能够同时运行64位和32位工作进程。
这意味着您为特定应用程序池
设置 Enable32BitAppOnWin64 ,而不是为所有应用程序池设置。

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.

您还必须检测IIS的版本才能使用正确的API。
这可以通过从注册表
中读取以下DWORD值来完成(有关更多信息,请参阅学习IIS ):

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

所以,这里是一些为IIS 6和IIS 7
设置 Enable32BitAppOnWin64 的代码(请注意,您必须引用 Microsoft.Web.Administration
System.DirectoryServices Visual Studio项目中的程序集:

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");
}

我还应该提一下,有可能使用元数据库API $ b IIS 7也是$ b。在Windows Server 2008操作系统上,您可以
安装名为IIS 6管理兼容性的角色服务。这个
角色服务使您可以使用旧IIS 6 API来管理IIS 7.

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.

如果IIS 6管理兼容性是您的选项更改
函数 Enable32BitAppOnWin64IIS7 ,如下所示:

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();
  }
}

当然,你不必参考 Microsoft.Web.Administration assembly。

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

这篇关于如何使用c#在IIS 6和IIS 7中启用32位应用程序模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆