以编程方式确定代码是否在IIS Express下运行

时间:2012-03-02 21:12:34

标签: c# iis iis-express

我不确定这是否可行,但我希望能找到一条线索,以确定当前正在执行的代码是否在IIS Express下运行。到目前为止,我的最佳近似值是令人难以置信的hackish,并且肯定会在某些时候失败/中断:

bool IsExpress = 
  Request.ServerVariables["SERVER_SOFTWARE"] == "Microsoft-IIS/7.5"
  && Int32.Parse(Request.ServerVariables["INSTANCE_ID"]) > 1000000000;

当然必须有更好的方法。我对应用程序,服务器和请求对象的检查似乎没有揭示可能提供更好洞察力的任何内容。也许我只需要看看其他一些对象?

更新

我真的很好奇是否有办法检测到这一点 - 在这一点上我真的很有学问,我不需要使用它。最初的问题就是。但本着回应评论的精神,特别是我有兴趣回答本网站上另一个问题/答案的批评:How to search the server's MIME map。批评是,发布的答案不适用于IIS Express,只适用于传统的IIS实例。 IIS Express将MIME配置存储在applicationhost.config XML文件中,我想更新该答案,以便为IIS Express提供返回该信息的方法。我当然可以添加一些从XML中获取适当值的代码(Yay for LINQ to XML!)但我真的想让它变得更聪明。为了清楚起见,我不需要帮助解析该文件 - 只是尝试检测代码当前是否在IIS Express引擎中执行时更优雅。

更新2:

IIS 8.0 Express Beta为released this week,它进一步表明我的问题中的方法很脆弱并且会破坏。虽然它不是针对特定版本的交易破坏者,但考虑到这一点并尝试确保代码至少与今天的已知版本一起使用会很好。

4 个答案:

答案 0 :(得分:27)

检查当前进程名称是否有效?

bool isExpress = 
  String.Compare(Process.GetCurrentProcess().ProcessName,"iisexpress") == 0;

正常的IIS在内存中w3wp.exe下运行。

答案 1 :(得分:2)

如果您不介意放入COM级API,可以使用IIS Version Manager API

http://msdn.microsoft.com/en-us/library/gg418429.aspx

有一些关于如何在此SO帖子中使用它的讨论:Starting and stopping IIS Express programmatically - 不完全是你想要的,但他们确实讨论了使用API​​。

编辑:我应该补充一点,我自己没试过,但看起来很有希望,祝你好运!

答案 2 :(得分:2)

我们是否可以尝试查看一个或多个limitation of IIS Express是否有效,如果有效则不是IIS Express。示例IIS Express不支持sharepoint服务

答案 3 :(得分:2)

奥兰·丹尼森(Oran Dennison)的答案对于dotnet核心不再适用。

我通过首先创建一个可以获取父进程的方法来扩展它:

/// <summary>
/// A utility class to determine a process parent.
/// </summary>
/// <remarks>
/// From https://stackoverflow.com/a/3346055/240845
/// </remarks>
public static class ParentProcessUtilities
{
    /// <summary>
    /// Gets the parent process.
    /// </summary>
    /// <param name="process">The process to get the parent of</param>
    /// <returns>The parent process.</returns>
    public static Process Parent(this Process process)
    {
        return GetParentProcess(process.Handle);
    }

    /// <summary>
    /// Gets the parent process of the current process.
    /// </summary>
    /// <returns>An instance of the Process class.</returns>
    public static Process GetParentProcess()
    {
        return Process.GetCurrentProcess().Parent();
    }

    /// <summary>
    /// Gets the parent process of a specified process.
    /// </summary>
    /// <param name="handle">The process handle.</param>
    /// <returns>The parent process.</returns>
    public static Process GetParentProcess(IntPtr handle)
    {
        ProcessInformation pbi = new ProcessInformation();

        // Note, according to Microsoft, this usage of NtQueryInformationProcess is 
        // unsupported and may change
        int status = NtQueryInformationProcess(
            handle, 0, ref pbi, Marshal.SizeOf(pbi), out _);
        if (status != 0)
        {
            throw new Win32Exception(status);
        }

        try
        {
            return Process.GetProcessById(pbi.InheritedFromUniqueProcessId.ToInt32());
        }
        catch (ArgumentException)
        {
            // not found
            return null;
        }
    }

    [DllImport("ntdll.dll")]
    private static extern int NtQueryInformationProcess(
        IntPtr processHandle, int processInformationClass,
        ref ProcessInformation processInformation, int processInformationLength, 
        out int returnLength);

    /// <summary>
    /// Used in the NtQueryInformationProcess call.
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct ProcessInformation
    {
        // These members must match PROCESS_BASIC_INFORMATION
        internal IntPtr Reserved1;
        internal IntPtr PebBaseAddress;
        internal IntPtr Reserved2_0;
        internal IntPtr Reserved2_1;
        internal IntPtr UniqueProcessId;
        internal IntPtr InheritedFromUniqueProcessId;

    }
}

然后,您可以执行以下操作:

public bool IsUnderIisExpress()
{
    var currentProcess = Process.GetCurrentProcess();
    if (string.CompareOrdinal(currentProcess.ProcessName, "iisexpress") == 0)
    {
       return true;
    }

    var parentProcess = currentProcess.Parent();
    if (string.CompareOrdinal(parentProcess.ProcessName, "iisexpress") == 0
        || string.CompareOrdinal(parentProcess.ProcessName, "VSIISExeLauncher") == 0)
    {
        return true;
    }

    return false;
}