Error 1053 when starting a Windows Service (exe created in Visual Studio)

c#, service, visual-studio, visual-studio-2015, windows-services

Solution

The error you're seeing is a notification from Windows that the service you've started hasn't completed its startup within a reasonable amount of time (30 seconds).

This is happening because you've got the logic for your service stuffed in your app's public Main() method, which isn't what you want for a Windows service.

A Windows service includes a bit of structure to support the service. All that typically happens in a service's Main() is to load the service, but not actually start it running. The service includes event handlers to support responding to standard service actions, such as starting, stopping, pausing, continuing, and to handling when the system is being shutdown.

This structure that all Windows services have is a bit complicated and must be built to the operating system's specifications. While it's possible to build a Windows service manually, it can be difficult to get all the plumbing correct, and hence much easier to let Visual Studio help you out here.

The simplest and most straightforward approach when building a Windows service is to let VS create a Windows service project for when you create the new Visual Studio project. The new project will include most all the necessary plumbing and service features you'll need from the get-go.

Of course, you can build a service manually, but there's really no reason to. If you do want go down the manually built path you'll need to at a minimum perform the following actions (one caveat - I'm doing this from memory and I moved to VS 2017 a while back, so this might not be exactly right):

Add a Windows Service component to your project. To do that, right-click on your project in the Solution Explorer and choose "Add". In the menu that appears, select "Component...". In the dialog that appears, choose "Windows Service". A word of advice, give the file a meaningful name before pressing that "Add" button.

Once that Windows Service component is added, right-click on it and set its properties.

To program the OnStart, OnStop, OnPause, OnContinue, and OnShutdown event handlers, right click on the Windows Service design space (or right-click on the file in Solution Explorer) and choose "View code".

There are many more things to know about building Windows services, that there's not room for here. I suggest finding some good documentation on the subject and studying that before you do a lot in this space, as doing something wrong here can have a pretty drastic affect on the machine that's running your service. Have a look at MSDN: Walkthrough: Creating a Windows Service Application in the Component Designer. It should help explain this much more thoroughly.

Problem

I have an executable created from a project in Visual Studio that I would like to create a service with (so I can run it without the need of a console window). I publish the project, and create the Windows Service using: ``` sc create MY.SERVICE binpath= "C:\Program Files\Project\serviceProj\myService.exe ``` The service shows up inside the Windows Services Manager as expected. However, whenever I try to start the service, it fails after about 2 seconds and gives me the following error: ``` Windows could not start the MY.SERVICE on Local Computer. Error 1053: The service did not respond to the start or control request in a timely fashion. ``` Things I have done: Changed from Debug to Release in Visual Studio Run everything as administrator (creating the service, publishing the project, starting the service, etc.). I've also read somewhere that increasing the amount of time the Service Manager waits for the service to start may work. I added the Windows registry value to do just that but unfortunately it did not work. Starting the service from a command prompt usually only takes 2-3 seconds to startup and start listening for requests so I'm unsure of what is going on. Any help is appreciated. Here is my Startup.cs class: ``` using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Newtonsoft.Json.Serialization; using Microsoft.AspNetCore.Hosting.WindowsServices; using System.Diagnostics; using System.IO; using Serilog; using System.Linq; namespace My.Service { public class Startup { 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:5002") .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .Build(); host.Run(); } else { var host = new WebHostBuilder() .UseKestrel() .UseUrls("http://localhost:5002") .UseContentRoot(directoryPath) .UseIISIntegration() .UseStartup<Startup>() .Build(); host.RunAsService(); } } public Startup(IHostingEnvironment env) { //Setup Logger Log.Logger = new LoggerConfiguration() .WriteTo.Trace() .MinimumLevel.Debug() .CreateLogger(); // Set up configuration sources. var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json"); Configuration = builder.Build(); } public IConfigurationRoot Configuration { get; set; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc().AddJsonOptions(options => { options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime lifetime) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}"); }); } } } ```

Original source