weixin_39721370
weixin_39721370
2020-12-31 00:16

Create a sample using Microsoft.AspNetCore.Hosting.WindowsServices

I am trying to get an RC2 web sample hosted as windows service up and running using Microsoft.AspNetCore.Hosting.WindowsServices ..


    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseStartup<startup>()
                .Build();

            host.RunAsService();
        }
    }
</startup>

.. the RunAsService extension method says it blocks until the service is stopped but currently it seems to be running the Startup over and over again. (I have a simple log message inside the Configure method). Not sure if this is a bug or I am doing something wrong.

If someone could create a working sample it would be really useful to check against.

该提问来源于开源项目:aspnet/Hosting

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

14条回答

  • weixin_39528843 weixin_39528843 4月前

    Is it crashing and auto-restarting?

    点赞 评论 复制链接分享
  • weixin_39721370 weixin_39721370 4月前

    : My startup log message just gets printed over and over. I would expect it to print once and then wait for a web method to be called (which should then print another log line).

    When i run the .exe from the commandline it starts and then prints a warning (I assume from System.ServiceProcess) saying this needs to be installed as a service.

    There are no errors logged to the file, just seems like it is running the startup, finishing and starting again.

    (When ive had crashes before the windows service would stop, but in this case it keeps running).

    Hope that makes sense !

    点赞 评论 复制链接分享
  • weixin_39721370 weixin_39721370 4月前

    My Startup file in case it helps ..

    
        public class Startup
        {
            public Startup(IHostingEnvironment env)
            {
                Log.Logger = new LoggerConfiguration()
                                .Enrich.FromLogContext()
                                .MinimumLevel.Debug()
                                .WriteTo.RollingFile("output.log")
                                .CreateLogger();
            }
    
            public void ConfigureServices(IServiceCollection services)
            {
            }
    
            public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
            {
                loggerFactory.AddSerilog();
                var logger = loggerFactory.CreateLogger<startup>();
    
                logger.LogDebug("Starting up ..");
    
                app.Run(async (context) =>
                {
                    logger.LogDebug("Hello !!!");
                    await context.Response.WriteAsync("Hello World!");
                });
            }
        }
    </startup>

    Results in ..

    
    2016-05-27 16:49:36.284 +01:00 [Debug] Starting up ..
    2016-05-27 16:49:38.562 +01:00 [Debug] Starting up ..
    2016-05-27 16:49:42.868 +01:00 [Debug] Starting up ..
    2016-05-27 16:49:51.178 +01:00 [Debug] Starting up ..
    2016-05-27 16:50:07.468 +01:00 [Debug] Starting up ..
    2016-05-27 16:50:39.743 +01:00 [Debug] Starting up ..
    
    点赞 评论 复制链接分享
  • weixin_39781550 weixin_39781550 4月前

    The time-stamp differences from the logs have an interesting pattern.

    Roughly: 2 secs, 4 secs, 8 secs, 16 secs, 32 secs... Curious.

    点赞 评论 复制链接分享
  • weixin_39721370 weixin_39721370 4月前

    So do you think this a bug or have I mis-configured my startup ? (Just wondering if this is a dead-end for now or I just need to tweak my program!)

    点赞 评论 复制链接分享
  • weixin_39781550 weixin_39781550 4月前

    I don't see anything in ASP.NET stack that could be doing exponential back-off. Perhaps Windows Service Host is doing it, though I'm not sure. There's nothing that does this in your code?

    点赞 评论 复制链接分享
  • weixin_39721370 weixin_39721370 4月前

    : Nope .. all my (pretty vanilla) code is above

    点赞 评论 复制链接分享
  • weixin_39528843 weixin_39528843 4月前

    keep an eye out for this when you do your sign-offs.

    点赞 评论 复制链接分享
  • weixin_39781550 weixin_39781550 4月前

    We'd be happy to take a PR for this one.

    点赞 评论 复制链接分享
  • weixin_39528843 weixin_39528843 4月前

    Reference links: https://github.com/aspnet/Home/issues/1386#issuecomment-221891868 https://github.com/aspnet/Home/issues/1386#issuecomment-242989060 http://stackoverflow.com/questions/37346383/hosting-asp-net-core-as-windows-service/37464074#37464074 http://dotnetthoughts.net/how-to-host-your-aspnet-core-in-a-windows-service/

    点赞 评论 复制链接分享
  • weixin_39865440 weixin_39865440 4月前

    Community library that works with .NET Core: https://github.com/dasMulli/dotnet-win32-service

    点赞 评论 复制链接分享
  • weixin_39631467 weixin_39631467 4月前

    Following the guidance from http://dotnetthoughts.net/how-to-host-your-aspnet-core-in-a-windows-service/ I've been successful in getting a web project hosted as a service.

    点赞 评论 复制链接分享
  • weixin_39528843 weixin_39528843 4月前

    I was able to get this working following the instructions linked above. It works with both 1.0 and 1.1. I don't see any restart issues with RunAsService. My guess is that you have a crash when your app can't reach a resource due to running as a service, something like UserSecrets, or that you're not getting UseContentRoot set appropriately for the service as shown in the linked instructions.

    点赞 评论 复制链接分享
  • weixin_39689377 weixin_39689377 4月前

    I was also able to get this to work using link.

    The issues I encountered were that you first need to dotnet publish your application, then point the binPath for your service to the .exe in it as follows:

    sc create MyService binPath="D:\myapp\bin\Debug\net451\publish\myapp.exe"

    If you don't do this but run it from your bin folder you are going to find files are missing such as your appsettings.json

    Also when the Windows service starts its current directory is set to the System32 folder and not that of the binPath you specified. So again with the default ASP.NET Core template out of the box you are going to find it will fail to startup because it can't find files such as appsettings.json. You can resolve that by first setting the current directory to the base directory of your app:

    
        public class Program
        {
            public static void Main(string[] args)
            {
                Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
    
                var host = new WebHostBuilder()
                    .UseKestrel()
                    .UseContentRoot(Directory.GetCurrentDirectory())
                    .UseIISIntegration()
                    .UseStartup<startup>()
                    .Build();
    
                if (Debugger.IsAttached || args.Contains("--debug"))
                {
                    host.Run();
                }
                else
                {
                    host.RunAsService();
                }
            }
        }
    </startup>
    点赞 评论 复制链接分享

相关推荐