无法将Asp.Net核心应用程序作为Windows服务运行

时间:2020-05-21 08:29:08

标签: c# asp.net-core

我正在尝试允许将ASP.NET Core 3.1 Web API作为Windows服务执行... 我的问题是应用程序挂在RunAsService上。

这是我程序的代码。

 public static void Main(string[] args)
        {
            var configuration = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .Build();

            var logger = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration)
                .CreateLogger();

            Log.Logger = logger;
            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(() =>
            {
                //


                try
                {
                    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
                    {
                        logger.Information("ci arrivo");
                        var processModule = System.AppDomain.CurrentDomain.BaseDirectory;
                        logger.Information("ci arrivo2");

                        Directory.SetCurrentDirectory(processModule);
                        if (processModule != null)
                            builder
                                .UseContentRoot(processModule)
                                .Build()
                                .RunAsService();

                    }
                }
                catch (Exception exception)
                {

                    logger.Error(exception, "");
                }
            });

            app.Execute(args);

        }

这是GetHostBuilder

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

查看MSDN here可以发现

在Windows服务内运行指定的Web应用程序,并阻塞直到服务停止为止

更新1

进行一些调试后,我看到Web api响应了,但是当我将其作为控制台运行时,我进入了日志

11:25:08 INF] Running as Console application
[11:25:10 DBG] Hosting starting
[11:25:10 DBG] Failed to locate the development https certificate at 'null'.
[11:25:10 DBG] Hosting started
[11:25:10 DBG] Loaded hosting startup assembly xxx
Hosting environment: Development
Content root path: C:\xxxx\xxxx
Now listening on: http://[::]:46700
Application started. Press Ctrl+C to shut down.

当我作为服务运行时

2020-05-21 11:15:25.748 +02:00 [DBG] Hosting starting
2020-05-21 11:15:25.980 +02:00 [DBG] Failed to locate the development https certificate at 'null'.
2020-05-21 11:15:26.022 +02:00 [DBG] Hosting started
2020-05-21 11:15:26.022 +02:00 [DBG] Loaded hosting startup assembly xxx

没有有关托管端口的信息,等等。另一个奇怪的是,即使我设置了WorkindDirectory以及ContentRoot

,它仍继续在c:\windows\system32\下写入

0 个答案:

没有答案