in Education by
My good ladies and gentlemen, I recently had my first go at Worker services in .Net Core 3.1, and only the second go at Windows services in general (first one was made in .Net Framework and works fine to this day). If anyone could maybe shed some light at what I'm missing in the example that I will provide that would be great. So, to keep it simple, my problem is this: My supposed long (forever) running Worker service unexpectedly stops doing work at an arbitrary time of day, but still is shown as "Running" in service manager (that's probably how Windows deals with services). It doesn't necessarily have to be every day, but it stops doing work every now and then until I manually stop it and then restart it in Service Manager. I have also stumbled upon this question which seemed to deal with my problem, but even after completely wrapping all of my service's code blocks in try-catchs, even on top-level, I still get nothing registered in my Log table, or even in the file I set up to write in if my DB connection fails. Service seems to just stop calling ExecuteAsync() method. Ok here's how my code's logically structured, I have excluded implementation and I'm just showing what happens until DoWork is called: public class Worker : BackgroundService { private readonly IConfiguration _configuration; public Worker(IConfiguration configuration) { _configuration = configuration; } public override Task StartAsync(CancellationToken cancellationToken) { return base.StartAsync(cancellationToken); } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { try { while (true) { try //paranoid try-catch { await DoWork(); await Task.Delay(TimeSpan.FromSeconds(45), stoppingToken); } catch (Exception e) { await Log(e, customMessage: "Proccess failed at top level."); } } } catch (Exception e) { await Log(e, customMessage: "Proccess failed at topmost level."); } } private async Task DoWork() { try { } catch (Exception e) { await Log(e); } } public async Task Log(Exception e, string user = null, string emailID = null, string customMessage = null) { } } As you can see, I am not handling cancellation, as in the question I linked above. Now that I think about it maybe I should, and something is inadvertently sending cancellation? The reason I didn't is because I'm not sure what events exactly signal the cancellation. Only the manual stopping of service, or something else maybe? And if it is the cancellation that was sent that caused my service to stop doing work, shouldn't it also stop my service from running? Btw I just tested cancellation on dummy service which implements my logic with while(true) and it catches the stopping exception, even though it's a bit awkward, as it catches it and logs it multiple times before stopping, so I presume it may not be the cancellation token that is causing my DoWork not to fire. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Ok guys I'd fixed it. See comment below. I'd guessed that what was causing deadlock was probably too many concurrent calls from different threads to database over the same connection. Not that that I knew that would be the cause (and I still don't know and can only guess why this happens so if someone can clarify why this happens and why don't the calls get queued please do), but as I tried to fix it that seemed like a good starting point. What I did was just limit possible concurrent calls to 1: Instantiate SemaphoreSlim on a class level: private static SemaphoreSlim Semaphore = new SemaphoreSlim(1); Insert a SemaphoreSlim.WaitAsync before each of my DB calls and its respective SemaphoreSlim.Release in a finally block after the call: try { await Semaphore.WaitAsync(); var id = await sqlCommand.ExecuteScalarAsync().ToString(); } finally { Semaphore.Release(); } I thought this would decrease the performance but to my pleasant surprise I felt no noticeable difference. Also, I was tempted to set Semaphore's initial count to more than 1 thread but I figured if deadlock happens for many threads, then it might happen for 2-10 threads. Does anyone perhaps know anything more about this number? Is it processor related, SQL related, or perhaps C# related?

Related questions

0 votes
    I have an object that I want to move by swipe, for example when the swipe is up the object ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I am investigating the design of a work queue processor where the QueueProcessor retrieves a Command Pattern object ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    We have some .NET code that does a call out to a COM component (dnsapi.dll) to query whether a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I have a website which has a service worker like: // Import a script from another domain importScripts(' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I have a wcf service that is hosted via a windows service which is used to connect to a database ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
    I have a wcf service that is hosted via a windows service which is used to connect to a database ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I'm working on a casual game on XNA with the intention of deploying to the Xbox 360. I'm not ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    When I run my test against that HTML code foo bar Cypress doesn't look beyond the first "div". ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Which of the following stops execution of a thread? (a) Calling SetPriority() method on a Thread object (b ... Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    _________ is an indication that a fatal problem has occurred and execution of the function stops. (a) message (b ... of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    I was running this script to highlight the active page in bold. I had to run jQuery 1.7 for ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 15, 2022 in Education by JackTerrance
0 votes
    Why a cell stops working after some time ? Select the correct answer from above options...
asked Jan 5, 2022 in Education by JackTerrance
0 votes
    A elevator starts with m passengers and stops at n floors (m≤n) . The probability that no two passengers leaves at same ... D. .nCm nm Select the correct answer from above options...
asked Nov 15, 2021 in Education by JackTerrance
0 votes
    Which of the following stops execution of a thread? (a) Calling SetPriority() method on a Thread object ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
...