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
reflectronic4mo ago
what is the error that you are getting, exactly
Sir Rufo
Sir Rufo4mo 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
Akama Aka @ DoKomi
Yes it's doing an infinite loop with a check So I just have to switch the logger message with the myservercode... Right?
reflectronic
reflectronic4mo ago
oh, you want to run it as a Windows Service?
Akama Aka @ DoKomi
Yes
reflectronic
reflectronic4mo ago
you did not call AddWindowsService
Akama Aka @ DoKomi
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
reflectronic4mo ago
well, i can guarantee that it will not work without UseWindowsService
Akama Aka @ DoKomi
Okay And if I do that it should work then right?
reflectronic
reflectronic4mo ago
no, that does not matter
Akama Aka @ DoKomi
Hm
Akama Aka @ DoKomi
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
reflectronic4mo 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
Akama Aka @ DoKomi
Okay i'll try as soon as I'm in work Doesn't work
reflectronic
reflectronic4mo ago
is there anything in the event logs related to the application
Akama Aka @ DoKomi
Oh missing .dll file But now "Could not resolve CoreCLR path."
reflectronic
reflectronic4mo ago
what was the missing dll and how did you fix it
Akama Aka @ DoKomi
Just added the .dll file into the folder
reflectronic
reflectronic4mo ago
how did you configure the service like, what is the path it's trying to run
Sir Rufo
Sir Rufo4mo 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() );
Akama Aka @ DoKomi
C:\Users<Name>\Desktop\servs
Sir Rufo
Sir Rufo4mo ago
But you should be aware that you cannot stop that task, because you did not code that method to be stopable
reflectronic
reflectronic4mo ago
you pointed it at the exe file?
Akama Aka @ DoKomi
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
reflectronic4mo 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
Akama Aka @ DoKomi
running in a container will make the whole program useless Because it checks for a USB Device
reflectronic
reflectronic4mo ago
it is not a container
Akama Aka @ DoKomi
oh
reflectronic
reflectronic4mo 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
Akama Aka @ DoKomi
And I cant build it with -p:PublishReadyToRun=true into a single .exe
reflectronic
reflectronic4mo ago
i mean if you are doing dotnet publish
Akama Aka @ DoKomi
-p:PublishSingleFile=true
reflectronic
reflectronic4mo ago
then instead of the <SelfContained> business i would do dotnet publish -p:PublishSelfContained=true -p:PublishSingleFile=true ... yeah that is fine
Akama Aka @ DoKomi
What is SelfContained doing?
MODiX
MODiX4mo ago
reflectronic
it just means that it puts the .NET runtime next to the exe
Quoted by
React with ❌ to remove this embed.
Akama Aka @ DoKomi
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
reflectronic4mo ago
how do you know it is not running it
Akama Aka @ DoKomi
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
reflectronic4mo ago
before you changed what
Akama Aka @ DoKomi
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
reflectronic4mo 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
Akama Aka @ DoKomi
Ik but it doesn't even call the one class Can I DM you the top of the code?
reflectronic
reflectronic4mo ago
i mean. yes, if you do AddWindowsService, and then run it as a console application, it will do nothing this is expected
Akama Aka @ DoKomi
It does nothing in generall It just starts not more
reflectronic
reflectronic4mo ago
what do you mean by "in general"
Akama Aka @ DoKomi
Sec I'll dm you the redacted things
reflectronic
reflectronic4mo 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
Akama Aka @ DoKomi
Yes it also would do the things it should do. But as soon as I run it as Service it doesn't work
reflectronic
reflectronic4mo ago
the fact that it isn't working as a windows service is a different matter
Akama Aka @ DoKomi
Yes
reflectronic
reflectronic4mo ago
it looks like your service uses some UI features
Akama Aka @ DoKomi
Do you mean the Console logs?
reflectronic
reflectronic4mo ago
i mean, there is MessageBox this means it needs to run as an interactive service
Akama Aka @ DoKomi
Oh yea but that's not so important The most important thing is to get the base working :gx_laugh:
reflectronic
reflectronic4mo ago
the fact that you are not running it as an interactive service may be the reason it is not working
Akama Aka @ DoKomi
Hm sec Then shouldn't jt lognerrors?
reflectronic
reflectronic4mo ago
you just need to check the check box i think
No description
reflectronic
reflectronic4mo ago
in the service properties i mean. who knows what it does right now. maybe it just hangs
Akama Aka @ DoKomi
Yes but still not working :/
reflectronic
reflectronic4mo ago
do you see message boxes now
Akama Aka @ DoKomi
Possible Nope nothing
reflectronic
reflectronic4mo ago
if you put a MessageBox at the very beginning do you see it
Akama Aka @ DoKomi
Sec Nothing Could it be that windows can't find the driver from the usb device?
Sir Rufo
Sir Rufo4mo ago
Come on, interactive services are obsolete since years
Sir Rufo
Sir Rufo4mo ago
Interactive Services - Win32 apps
Typically, services are console applications that are designed to run unattended without a graphical user interface (GUI).
Akama Aka @ DoKomi
The only thing it should do is lock the user
Sir Rufo
Sir Rufo4mo ago
It does not matter what it "only" should do - you simply can not have interaction from a service to the desktop. period.
Akama Aka @ DoKomi
Yes
Sir Rufo
Sir Rufo4mo 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
Akama Aka @ DoKomi
Yes
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Akama Aka @ DoKomi
Both I tried Why? But could it be that it needs user32.dll?
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Akama Aka @ DoKomi
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 User4mo ago
Message Not Public
Sign In & Join Server To View
Akama Aka @ DoKomi
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 User4mo ago
Message Not Public
Sign In & Join Server To View
Akama Aka @ DoKomi
All my code has the CC-BY-NC-ND 4.0 International License
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server
More Posts