0

I need a .Net core, user-triggered background job approach, which supports the HttpContext of that user. I've gone down the hangfire path unsuccessfully, and what I can tell, BackgroundService does not support this. Spawning a thread does, but that seems like a no-no. Any input on a good design for this?

Hangfire throws null exceptions when accessing it, after queueing and firing the job. That also seems to be the case with BackgroundService and Thread spawning. Basically any background process seems to disconnect from HttpContext, presumably due to the separately-threaded nature.

Is the only option to pass all necessary HttpContext info into the job?

Jeremy
  • 9,023
  • 20
  • 57
  • 69
  • Look at https://stackoverflow.com/questions/36335345/web-api-fire-and-forget/36338190#36338190 answer. Pass all needed details from httpcontext that your background function will not refer to httpcontext anymore. In general, provide more details in your question, e.g why what you tried didn’t work – Michael Freidgeim May 22 '22 at 16:28
  • Does this answer your question? [Run a background task from a controller action in ASP.NET Core](https://stackoverflow.com/questions/49813628/run-a-background-task-from-a-controller-action-in-asp-net-core) – Fabio May 23 '22 at 09:22
  • @Fabio don't think so. I don't see that this addresses the need to be able to access "HttpContext.Current", unless I'm misunderstanding. The only way I've seen thus far is to be able to pass all the HttpContext information along when the Job is queued. – Jeremy May 24 '22 at 13:13
  • 1
    HttpContext in .Net Core background jobs not supported. Pass necessary details into job. https://github.com/dotnet/core/issues/1747 – Nayan May 24 '22 at 13:29
  • 1
    You can retrieve all required information from `HttpContext` into plain object and pass it to the background queue – Fabio May 25 '22 at 08:17

0 Answers0