Singleton class with session timespan (for each browsing client) .NET CORE

402 Views Asked by At

I'm trying to create singleton class but only for session. So I want to have one instance for each user that enters website (it's for JMS server). Therefore - when session will be closed I have to invoke close connection from library.

So:

  1. how to create class that will be one for each session?
  2. how to catch event that session is not inactive and invoke some method?

Tried with AddSingleton/AddTransient/AddScoped but probably it's not proper way for the job (or I'm using it wrong).

To explain more - "Business Case" is web application, where user can check messages on queues on JMS server. User enters connection parameters on welcome page (login) - so when somebody uses app (and is connected to some JMS server), another User can enter page - see login and this another user creates his own connection to another JMS server.

1

There are 1 best solutions below

1
Skylerdw On

If you're sure you want them per session, you might consider creating instances on session creation using:

_myClass = serviceProvider
          .GetRequiredService<IMyInterface>();