User session in background task in ASP.NET Core

Jack Miller

I learned that the preferred ASP.NET-Core-way for background tasks is a hosted service.

Is there any way to pass the (user) session to this background worker? Usually the session is part of the HttpContext which will not be available in the background task...

I know that I could persist data by writing to the database but I'd also like to cache some data in memory.

poke

If this is all happening in-process, then you should be able to just pass the ISession object directly to your background service and have it interact with the user’s session without the user’s HttpContext having to be around.

The default session implementation is using the distributed cache to persist the information stored in the session. Each session object is then only interacted by with a session key that is stored in a user cookie. When the session object is being created, no direct or indirect reference to the HttpContext is being passed. The session store itself also does not access the HttpContext in some other way. The distributed cache itself is also independent of the context and also the DI service scope.

So at least in theory, this should work just fine. You can use the ISession object to modify the session and the next time the user makes a request with their session id, the updated information will be there waiting for them.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Start Background Task using ASP.Net Core Middleware

Asp.Net core long running/background task

Save user session in Redis with ASP.NET Core in Azure

asp.net core 2 web api task run background task

ASP.Net Core Background Service with Task Queue, does it need a Task.Delay?

.Net Core Authentication and user session

ASP Net Core 2.1 Session

Running background task on demand in asp.net core 3.x

How to create a background task in startup service in asp.net core 1.0 Web API?

Mysterious Behavior of .NET Core Background Task

Short running background task in .NET Core

Google Cloud Background Task with .Net Core

What are ai_user and ai_session cookies in ASP NET Core Identity and how to configure it?

ASP.NET Core 6.0 / Razor pages / add info about currently logged in user to the Session

Storing user data in session store or retrieving from database in each request in asp.net core?

Asp.Net core 6 session keys

Enable ASP.Net Core Session Locking?

ASP.NET Core session authentication

ASP.Net Core Identity with distributed session

Asp.net core session unit test

ASP.NET Core clear session issue

Creating a session in ASP.NET Core 3.1

session in asp.net core in memory

Session Timeout Notification on ASP.NET core

In ASP.NET Core 3.1, how can I schedule a background task (Cron Jobs) with hosted services for a specific date and time in the future?

How can I manually cancel a .NET core IHostedService background task?

.NET Core ASP.NET Controller with Session and tests

Asp.net Core User Selectable CSS

Asp net Core Get user Windows username