Relative Content

Tag Archive for c#multithreadingasynchronousbackground-serviceexecutioncontext

How do I clean-up/clear an AsyncLocal object in a BackgroundService(Microsoft.Hosting.Extensions.BackgroundService)?

I have a Background Service that performs some tasks and jobs on a long running thread. For simplicity, We’ll consider this as a scheduler that makes an async SQL call and repeats. Now I use AsyncLocal to store metadata for logging purpose on the service and I want to group each repetition of the task as a transaction. I figured that unlike web applications the BackgroundService will have a single AsyncLocal(ExecutionContext) object that I have to clear after every repetition (every SQL call in this example). Coming to the problem, I cannot seem to clean up the object .