-1

I created a worker service on .Net 3.1, after I created a windows service in Services.msc, but I can't start the new service. here are all the related errors I found in eventviewer.

  1. The TestWorkerService service failed to start due to the following error: The service did not respond to the start or control request in a timely fashion.

  2. Faulting application name: TestWorkerService.exe, version: 1.0.0.0, time stamp: 0x6243f19c Faulting module name: KERNELBASE.dll, version: 6.1.7601.24384, time stamp: 0x5c6e248c Exception code: 0xe0434352 Fault offset: 0x000000000000be0d Faulting process id: 0x8778 Faulting application start time: 0x01d9409075d9a7b9 Faulting application path: C:\inetpub\TestWorkerService\TestWorkerService.exe Faulting module path: C:\Windows\system32\KERNELBASE.dll Report Id: b3e4df75-ac83-11ed-a1fc-000c290f795a

  3. Application: TestWorkerService.exe CoreCLR Version: 4.700.22.30802 .NET Core Version: 3.1.27 Description: The process was terminated due to an unhandled exception. Exception Info: System.PlatformNotSupportedException: ServiceController enables manipulating and accessing Windows services and it is not applicable for other operating systems. at System.ServiceProcess.ServiceBase..ctor() at Microsoft.Extensions.Hosting.WindowsServices.WindowsServiceLifetime..ctor(IHostEnvironment environment, IHostApplicationLifetime applicationLifetime, ILoggerFactory loggerFactory, IOptions1 optionsAccessor, IOptions1 windowsServiceOptionsAccessor) at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType) at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.Extensions.Hosting.HostBuilder.<>c__DisplayClass35_0.b__2(IServiceProvider _) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType) at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.Extensions.Hosting.HostBuilder.ResolveHost(IServiceProvider serviceProvider, DiagnosticListener diagnosticListener) at Microsoft.Extensions.Hosting.HostBuilder.Build() at TestWorkerService.Program.Main(String[] args) in C:\Users\a.arabuli\Desktop\intranet\TestWorkerService\Program.cs:line 21

I tried to make a service in a several way like on .net 6.0, 3.1, 2.1, 5.0 on a console application, on a web api, but all the attempts have failed with the same exact errors I mentioned above

  • What OS are you on? – Fildor Feb 15 '23 at 07:42
  • 1
    Well the error seems pretty clear: The process was terminated due to an unhandled exception. Exception Info: System.PlatformNotSupportedException: ServiceController enables manipulating and accessing Windows services and it is not applicable for other operating systems. – jeb Feb 15 '23 at 07:43
  • I use windows 10, yes the error message is pretty clear but I cant find a way to fix it, that's why I asked for help – Adomel Arabuli Feb 15 '23 at 07:46
  • @jeb While that's true, it would be very odd if OP _were operating on Windows_. Then it would be an indication of a rogue error. I mean some error elsewhere with a different reason, resulting in the observed behavior through some mysterious ways ... and those are hard to find. – Fildor Feb 15 '23 at 07:47
  • Can you do a proof of concept? Make a new service that does just waste some time and try to start it. Will it? If not, then maybe it's something in your specific way of doing things. If it works, then make it one step more similar to your actual service, test again, and so on. Sooner or later you will then identify the change that causes the error. – Fildor Feb 15 '23 at 07:50
  • https://github.com/adomelArabuli/workerService – Adomel Arabuli Feb 15 '23 at 07:53
  • For testing reason I have already done it I created a stupid service which is logging time. But error is the same, the repo for this service I posted above this comment – Adomel Arabuli Feb 15 '23 at 07:54
  • Even I tried to make launch this testService on another pc but the result are same. I am an admin, I am the owner of the app, I have started Interactive services detection in services, and also I have added myself in secpol.msc in log on as a service – Adomel Arabuli Feb 15 '23 at 08:00
  • your repo works on my machine. The worker is running – jeb Feb 15 '23 at 08:10
  • sc create testService binPath= "path\path.exe" did you use the same way to create a service ? – Adomel Arabuli Feb 15 '23 at 09:06
  • @jeb or did you just try to launch exe file ? exe file is launching and worker is logging but I can't start it after registering it as a service in services.msc – Adomel Arabuli Feb 15 '23 at 09:16

1 Answers1

0

In order to host the worker as a windows service a nuget package is required. This is the Version 7 for net7.

 <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="7.0.0" />

Also you have to include

UseWindowsService()

 public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .UseSerilog()
            .UseWindowsService()
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService<Worker>();
            });

with these changes i was able to run you code as a windows service.

jeb
  • 848
  • 5
  • 16