检查是否在IIS中托管Asp.Net(Core)应用程序

时间:2017-02-16 11:00:55

标签: asp.net iis

如果应用程序在IIS中托管,我该如何检查它?

3 个答案:

答案 0 :(得分:1)

检查是否设置了环境变量APP_POOL_ID。

public static bool InsideIIS() =>
    System.Environment.GetEnvironmentVariable("APP_POOL_ID") is string;

All of environment variables that iis sets on a child process

答案 1 :(得分:0)

我认为没有直接的方法可以实现开箱即用。至少我没有找到一个。正如我所知道的那样,原因是ASP.NET Core应用程序实际上是一个独立的应用程序,对它的父上下文一无所知,除非后者将揭示有关其自身的信息。

例如,在配置文件中,我们可以告诉我们正在运行的安装类型:productiondevelopment。我们可以假设productionIIS,而development则不是。然而,这对我没有用。由于我的生产设置可能是IISwindows service

所以我通过向我的应用程序提供不同的命令行参数来解决这个问题,具体取决于它应该执行的运行类型。实际上,这对我来说很自然,因为windows service确实需要不同的方法来运行。

例如在我看来,代码看起来有点像:

namespace AspNetCore.Web.App
{
    using McMaster.Extensions.CommandLineUtils;
    using Microsoft.AspNetCore;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Hosting.WindowsServices;
    using System;
    using System.Diagnostics;
    using System.IO;

    public class Program
    {
        #region Public Methods

        public static IWebHostBuilder GetHostBuilder(string[] args, int port) =>
            WebHost.CreateDefaultBuilder(args)
                .UseKestrel()
                .UseIISIntegration()
                .UseUrls($"http://*:{port}")
                .UseStartup<Startup>();

        public static void Main(string[] args)
        {
            var app = new CommandLineApplication();

            app.HelpOption();
            var optionHosting = app.Option("--hosting <TYPE>", "Type of the hosting used. Valid options: `service` and `console`, `console` is the default one", CommandOptionType.SingleValue);
            var optionPort = app.Option("--port <NUMBER>", "Post will be used, `5000` is the default one", CommandOptionType.SingleValue);

            app.OnExecute(() =>
            {
                //
                var hosting = optionHosting.HasValue()
                    ? optionHosting.Value()
                    : "console";

                var port = optionPort.HasValue()
                    ? new Func<int>(() =>
                    {
                        if (int.TryParse(optionPort.Value(), out var number))
                        {
                            // Returning successfully parsed number
                            return number;
                        }

                        // Returning default port number in case of failure
                        return 5000;
                    })()
                    : 5000;

                var builder = GetHostBuilder(args, port);

                if (Debugger.IsAttached || hosting.ToLowerInvariant() != "service")
                {
                    builder
                        .UseContentRoot(Directory.GetCurrentDirectory())
                        .Build()
                        .Run();
                }
                else
                {
                    builder
                        .UseContentRoot(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName))
                        .Build()
                        .RunAsService();
                }
            });

            app.Execute(args);
        }

        #endregion Public Methods
    }
}

此代码不仅允许选择类型的托管(serviceconsole - IIS应该使用的选项,而且还允许更改重要的端口,当你是作为Windows服务运行的。

另一个好处是使用参数解析库McMaster.Extensions.CommandLineUtils - 它将显示有关已配置命令行开关的信息,因此可以轻松选择正确的值。

答案 2 :(得分:0)

我尝试了BranimirRičko的答案,但发现这是不正确的:在IIS express下运行时,也会设置此环境变量。

这是我的修改版本:

static bool IsRunningInsideIIS() =>
    System.Environment.GetEnvironmentVariable("ASPNETCORE_HOSTINGSTARTUPASSEMBLIES") is string startupAssemblies &&  
 startupAssemblies.Contains(typeof(Microsoft.AspNetCore.Server.IISIntegration.IISDefaults).Namespace);
相关问题