C
C#10mo ago
Akama Aka

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