Windows Worker Service times out

Hello. Everytime I want to start my Worker Service I get the message that it doesnt reacted in time and I dont know why I get this error. because everytime I run the exe noremally the whole thing works. Program.cs
using My_Service;

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();
IHost host = builder.Build();
await host.RunAsync();
using My_Service;

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();
IHost host = builder.Build();
await host.RunAsync();
Worker.cs
using System.Net.Mime;

namespace My_Service;


public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;

public Worker(ILogger<Worker> logger)
{
_logger = logger;
}



protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{

while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("My Service running at: {time}", DateTimeOffset.Now);
try
{
myServiceCode.systemcode();
_logger.LogInformation("Started Service Service Code");
//await Task.Delay(1_000, stoppingToken);
}
catch (Exception e)
{
_logger.LogError("Error in Service Code: {e}", e);
}
await Task.Delay(60000, stoppingToken);
}
}
}
using System.Net.Mime;

namespace My_Service;


public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;

public Worker(ILogger<Worker> logger)
{
_logger = logger;
}



protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{

while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("My Service running at: {time}", DateTimeOffset.Now);
try
{
myServiceCode.systemcode();
_logger.LogInformation("Started Service Service Code");
//await Task.Delay(1_000, stoppingToken);
}
catch (Exception e)
{
_logger.LogError("Error in Service Code: {e}", e);
}
await Task.Delay(60000, stoppingToken);
}
}
}
80 Replies
reflectronic
reflectronic2y ago
what is the error that you are getting, exactly
Sir Rufo
Sir Rufo2y ago
Your ExecuteAsync runs sync until the first await Task.Delay. I guess serviceCode.systemcode() does some lengthy operation and because it is run sync you get that message
Kasumi (Deactivated Account)
Yes it's doing an infinite loop with a check So I just have to switch the logger message with the myservercode... Right?
reflectronic
reflectronic2y ago
oh, you want to run it as a Windows Service?
reflectronic
reflectronic2y ago
you did not call AddWindowsService
Kasumi (Deactivated Account)
Oh hm But I've the same thing with Sec I've that:
//System.Diagnostics.Debugger.Launch();
/*
IHost host = Host.CreateDefaultBuilder(args)
.UseWindowsService(options =>
{
options.ServiceName = "my Service";

})
.ConfigureServices(services =>
{
services.AddHostedService<Worker>0();
})
.Build();

await host.RunAsync();
//System.Diagnostics.Debugger.Launch();
/*
IHost host = Host.CreateDefaultBuilder(args)
.UseWindowsService(options =>
{
options.ServiceName = "my Service";

})
.ConfigureServices(services =>
{
services.AddHostedService<Worker>0();
})
.Build();

await host.RunAsync();
And it's also not working
reflectronic
reflectronic2y ago
well, i can guarantee that it will not work without UseWindowsService
Kasumi (Deactivated Account)
Okay And if I do that it should work then right?
reflectronic
reflectronic2y ago
no, that does not matter
Kasumi (Deactivated Account)
Worker Services - .NET
Learn how to implement a custom IHostedService and use existing implementations in C#. Discover various worker implementations, templates, and service patterns.
reflectronic
reflectronic2y ago
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddWindowsService(options => options.ServiceName = "My Worker");
builder.Services.AddHostedService<Worker>();
IHost host = builder.Build();
await host.RunAsync();
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddWindowsService(options => options.ServiceName = "My Worker");
builder.Services.AddHostedService<Worker>();
IHost host = builder.Build();
await host.RunAsync();
this should just work
Kasumi (Deactivated Account)
Okay i'll try as soon as I'm in work Doesn't work
reflectronic
reflectronic2y ago
is there anything in the event logs related to the application
Kasumi (Deactivated Account)
Oh missing .dll file But now "Could not resolve CoreCLR path."
reflectronic
reflectronic2y ago
what was the missing dll and how did you fix it
Kasumi (Deactivated Account)
Just added the .dll file into the folder
reflectronic
reflectronic2y ago
how did you configure the service like, what is the path it's trying to run
Sir Rufo
Sir Rufo2y ago
No. You block the ExecuteAsync method with the serviceCode.systemcode() and that causes the not responding service. As a rough method change it to await Task.Run( () => serviceCode.systemcode() );
Kasumi (Deactivated Account)
C:\Users<Name>\Desktop\servs
Sir Rufo
Sir Rufo2y ago
But you should be aware that you cannot stop that task, because you did not code that method to be stopable
reflectronic
reflectronic2y ago
you pointed it at the exe file?
Kasumi (Deactivated Account)
Like that?
try
{
_logger.LogInformation("Started My Service Code");
await Task.Run(() =>
{
[redacted].[redacted]();
});
await Task.Delay(1_000, stoppingToken);
}
try
{
_logger.LogInformation("Started My Service Code");
await Task.Run(() =>
{
[redacted].[redacted]();
});
await Task.Delay(1_000, stoppingToken);
}
yes :thonkThinkWatWutWhatDerpConfused: then I need to look how I can stop it then
reflectronic
reflectronic2y ago
well, the "Could not resolve CoreCLR path." means it is not even running right now i think the easiest way to fix it is to use self-contained
Kasumi (Deactivated Account)
running in a container will make the whole program useless Because it checks for a USB Device
reflectronic
reflectronic2y ago
it is not a container
reflectronic
reflectronic2y ago
it just means that it puts the .NET runtime next to the exe so it does not have to look for your installation which, for whatever reason, it can't do <SelfContained>true</SelfContained> in your project file in the PropertyGroup then when you build you have to copy the entire build folder to wherever the service is running from do not just copy a handful of files, you cannot forget anything or the whole thing doesn't work
Kasumi (Deactivated Account)
And I cant build it with -p:PublishReadyToRun=true into a single .exe
reflectronic
reflectronic2y ago
i mean if you are doing dotnet publish
Kasumi (Deactivated Account)
-p:PublishSingleFile=true
reflectronic
reflectronic2y ago
then instead of the <SelfContained> business i would do dotnet publish -p:PublishSelfContained=true -p:PublishSingleFile=true ... yeah that is fine
Kasumi (Deactivated Account)
What is SelfContained doing?
MODiX
MODiX2y ago
reflectronic
it just means that it puts the .NET runtime next to the exe
Quoted by
React with ❌ to remove this embed.
Kasumi (Deactivated Account)
Ah Now it's started But it does nothing :shyyDead:. But prob. I need to download the driver for the usb device sec @reflectronic but now it doesn't run the redacted code
reflectronic
reflectronic2y ago
how do you know it is not running it
Kasumi (Deactivated Account)
The [redacted].redacted; Because it does nothing Before I changed all of that, it worked But now if I unplug the usb device it does nothing
reflectronic
reflectronic2y ago
before you changed what
Kasumi (Deactivated Account)
All what we discussed here If I run the .exe normally in the console it doesn't log that what it should as soon as the [redacted].redacted; is called
reflectronic
reflectronic2y ago
yes, you can't just run a windows service as a console application if you want to run it as a console application you need to delete AddWindowsService and rebuild it
Kasumi (Deactivated Account)
Ik but it doesn't even call the one class Can I DM you the top of the code?
reflectronic
reflectronic2y ago
i mean. yes, if you do AddWindowsService, and then run it as a console application, it will do nothing this is expected
Kasumi (Deactivated Account)
It does nothing in generall It just starts not more
reflectronic
reflectronic2y ago
what do you mean by "in general"
Kasumi (Deactivated Account)
Sec I'll dm you the redacted things
reflectronic
reflectronic2y ago
i still don't understand what is different now if you delete AddWindowsService, and run it as a console application, it will work exactly as it did before it does not matter what the code is doing
Kasumi (Deactivated Account)
Yes it also would do the things it should do. But as soon as I run it as Service it doesn't work
reflectronic
reflectronic2y ago
the fact that it isn't working as a windows service is a different matter
reflectronic
reflectronic2y ago
it looks like your service uses some UI features
Kasumi (Deactivated Account)
Do you mean the Console logs?
reflectronic
reflectronic2y ago
i mean, there is MessageBox this means it needs to run as an interactive service
Kasumi (Deactivated Account)
Oh yea but that's not so important The most important thing is to get the base working :gx_laugh:
reflectronic
reflectronic2y ago
the fact that you are not running it as an interactive service may be the reason it is not working
Kasumi (Deactivated Account)
Hm sec Then shouldn't jt lognerrors?
reflectronic
reflectronic2y ago
you just need to check the check box i think
No description
reflectronic
reflectronic2y ago
in the service properties i mean. who knows what it does right now. maybe it just hangs
Kasumi (Deactivated Account)
Yes but still not working :/
reflectronic
reflectronic2y ago
do you see message boxes now
Kasumi (Deactivated Account)
Possible Nope nothing
reflectronic
reflectronic2y ago
if you put a MessageBox at the very beginning do you see it
Kasumi (Deactivated Account)
Sec Nothing Could it be that windows can't find the driver from the usb device?
Sir Rufo
Sir Rufo2y ago
Come on, interactive services are obsolete since years
Sir Rufo
Sir Rufo2y ago
Interactive Services - Win32 apps
Typically, services are console applications that are designed to run unattended without a graphical user interface (GUI).
Kasumi (Deactivated Account)
The only thing it should do is lock the user
Sir Rufo
Sir Rufo2y ago
It does not matter what it "only" should do - you simply can not have interaction from a service to the desktop. period.
Sir Rufo
Sir Rufo2y ago
If you want an interaction you need two instances/applications and one has to run in the context of the current user which will communicate with the service and do the interaction with the user
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Kasumi (Deactivated Account)
Both I tried Why? But could it be that it needs user32.dll?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Kasumi (Deactivated Account)
Because it needs to interact with user specific things like lock the user and more I'd do in private but not in public
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Kasumi (Deactivated Account)
If I share it to someone in private and the person publishes the code without my consent, then I know then who I can sue then Can I send you the files in private?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Kasumi (Deactivated Account)
All my code has the CC-BY-NC-ND 4.0 International License
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?