Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Create a sample using Microsoft.AspNetCore.Hosting.WindowsServices #771

Closed
stephenpope opened this issue May 27, 2016 · 14 comments
Closed
Labels
Milestone

Comments

@stephenpope
Copy link

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();
        }
    }

.. 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.

@Tratcher
Copy link
Member

Is it crashing and auto-restarting?

@stephenpope
Copy link
Author

@Tratcher: 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 !

@stephenpope
Copy link
Author

stephenpope commented May 27, 2016

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!");
            });
        }
    }

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 ..

@muratg
Copy link

muratg commented May 27, 2016

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

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

@stephenpope
Copy link
Author

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!)

@muratg
Copy link

muratg commented Jun 2, 2016

@stephenpope 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?

@stephenpope
Copy link
Author

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

@muratg muratg added this to the 1.0.1 milestone Jun 9, 2016
@Tratcher
Copy link
Member

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

@muratg muratg modified the milestones: Backlog, 1.1.0 Sep 7, 2016
@muratg
Copy link

muratg commented Sep 7, 2016

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

@Tratcher Tratcher self-assigned this Jan 4, 2017
@DamianEdwards
Copy link
Member

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

@Kieranties
Copy link

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.

@Tratcher
Copy link
Member

Tratcher commented Jan 9, 2017

@stephenpope I was able to get this working following the instructions @Kieranties 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.

@khayes
Copy link

khayes commented Mar 22, 2017

I was also able to get this to work using @Kieranties 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();
            }
        }
    }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

7 participants