You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I implemented AsyncLocal in a particular service method to avoid some repeated processing, however the same service method ends up being called by Hangfire and on every job run the data of AsyncLocal.Value, which should be null on each asynchronous call retains the same value until the app is retarted. The code works perfectly when coming from a SignalR or WebAPI request, it's only Hangfire processes that retain the value.
Code example for job, which will only ever run successfully once, then will error on every subsequent call:
private static AsyncLocal<string> _test = new AsyncLocal<string>();
public async Task DoJob()
{
if (_test.Value != null)
{
throw new Exception("AsyncLocal is retaining a value");
}
_test.Value = "ABC123";
}
The text was updated successfully, but these errors were encountered:
While I agree that AsyncLocal instances shouldn't be preserved across different background job runs, and ExecutionContext should be somehow flushed, but this is potentially a breaking change for those who use this feature. So I'm postponing this to 2.0 version without any specific point in time.
Is it possible to clean these values manually, for example in a try/finally block?
I implemented AsyncLocal in a particular service method to avoid some repeated processing, however the same service method ends up being called by Hangfire and on every job run the data of AsyncLocal.Value, which should be null on each asynchronous call retains the same value until the app is retarted. The code works perfectly when coming from a SignalR or WebAPI request, it's only Hangfire processes that retain the value.
Code for job setup:
Code example for job, which will only ever run successfully once, then will error on every subsequent call:
The text was updated successfully, but these errors were encountered: