Roka545
Roka545

Reputation: 3636

The service on Local Computer started then stopped

I've created a Service that I've installed on several machines. They all work fine except for one. When I try to start the service via Windows Services I get the following error:

The My.Service service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs.

I've seen other posts suggesting you change the service's log on properties to log on as a local system account - I'm already doing this.

Event viewer gives me this information:

Application: myProject.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AggregateException
Stack:
   at Microsoft.AspNetCore.Server.Kestrel.KestrelServer.Start[[Microsoft.AspNetCore.Hosting.Internal.HostingApplication+Context, Microsoft.AspNetCore.Hosting, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]](Microsoft.AspNetCore.Hosting.Server.IHttpApplication`1<Context>)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.Start()
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(Microsoft.AspNetCore.Hosting.IWebHost, System.Threading.CancellationToken, System.String)
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(Microsoft.AspNetCore.Hosting.IWebHost)
   at Nexus.Startup.Main(System.String[])

I've tried running the service manually from a command prompt and passing in --debug and received this error message:

09:05:47.289 [1] ERROR - 
System.AggregateException: One or more errors occurred. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Listener.<DisposeAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.ListenerPrimary.<DisposeAsync>d__20.MoveNext()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.WaitAll(Task[] tasks, Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.WaitAll(Task[] tasks, Int32 millisecondsTimeout)
   at System.Threading.Tasks.Task.WaitAll(Task[] tasks, TimeSpan timeout)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.KestrelEngine.DisposeListeners(List`1 listeners)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.KestrelEngine.CreateServer(ServerAddress address)
   at Microsoft.AspNetCore.Server.Kestrel.KestrelServer.Start[TContext](IHttpApplication`1 application)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.Start()
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host, CancellationToken token, String shutdownMessage)
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host)
   at MyProject.Startup.Main(String[] args) in D:\bamboo-home\xml-data\build-dir\163841\CUT-CUP-CR\repos\myProject\Startup.cs:line 42
---> (Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Listener.<DisposeAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.ListenerPrimary.<DisposeAsync>d__20.MoveNext()<---

I'm fairly confused since this service starts up perfectly fine on other machines.

Here is my Startup.cs main method:

        public static void Main(string[] args)
    {
        var exePath = Process.GetCurrentProcess().MainModule.FileName;
        var directoryPath = Path.GetDirectoryName(exePath);

        if (Debugger.IsAttached || args.Contains("--debug"))
        {
            var host = new WebHostBuilder()
               .CaptureStartupErrors(true)
               .UseKestrel()
               .UseUrls("http://localhost:5000")
               .UseContentRoot(Directory.GetCurrentDirectory())
               .UseIISIntegration()
               .UseStartup<Startup>()
               .Build();
            host.Run();
        }
        else
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseUrls("http://localhost:5000")
                .UseContentRoot(directoryPath)
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();
            host.RunAsService();
        }
    }

Upvotes: 0

Views: 8797

Answers (1)

Scott Chamberlain
Scott Chamberlain

Reputation: 127603

@ScottChamberlain Yes, I passed in --debug – Roka545

You can't do that. Run() is a blocking call that does not allow the windows service to fully start. You need to always need to do RunAsSevice() from inside a service even if you are debugging. The pattern I normally do is

    if (args.Contains("--console"))
    {
        var host = new WebHostBuilder()
           .CaptureStartupErrors(true)
           .UseKestrel()
           .UseUrls("http://localhost:5000")
           .UseContentRoot(Directory.GetCurrentDirectory())
           .UseIISIntegration()
           .UseStartup<Startup>()
           .Build();
        host.Run();
    }
    else
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseUrls("http://localhost:5000")
            .UseContentRoot(directoryPath)
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();
        host.RunAsService();
    }

So debugger attached or not, the service runs as a service when you start it from within a service. If you want to run it standalone (like in F5 from visual studio) you pass --console to the arguments.

Upvotes: 0

Related Questions