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

问题描述 投票:4回答:1

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

asp.net iis
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 - 它将显示有关已配置命令行开关的信息,因此可以轻松选择正确的值。

© www.soinside.com 2019 - 2024. All rights reserved.