如何使用 c# 在 IIS 6 和 IIS 7 中启用 32 位应用程序模式 [英] How to enable 32-bit applications mode in IIS 6 and IIS 7 using c#
问题描述
我想使用 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.
IIS 6 是使用 DirectoryEntry
类和元数据库 API 以编程方式管理的.
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
意味着所有工作进程(所有应用程序池)作为 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):
HKLMSoftwareMicrosoftInetStpMajorVersion and
HKLMSoftwareMicrosoftInetStpMinorVersion
所以,这里有一些代码可以为 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(@"SoftwareMicrosoftInetStp"))
{
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对于 IIS 7,也是.在 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
程序集.
Of course, then you do not have to reference the Microsoft.Web.Administration
assembly.
这篇关于如何使用 c# 在 IIS 6 和 IIS 7 中启用 32 位应用程序模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!