4 Replies - 453 Views - Last Post: 02 May 2017 - 12:08 PM

#1 andrewsw  Icon User is online

  • lashings of ginger beer
  • member icon

Reputation: 6340
  • View blog
  • Posts: 25,570
  • Joined: 12-December 12

Shared Session (AddSingleton)

Posted 02 May 2017 - 02:30 AM

(ASP.NET Core MVC)

There is a method AddSingleton that I'm using to retain a Guid throughout an application process. AddSingleton maintains the value (a property of a class) throughout the application process, but if I start a different browser the same Guid number appears.

Shouldn't each different browser constitute a different run of the application, a different session? That is, I want each person making an application to obtain a different Guid.

Or perhaps this is just happening because I'm still in development and using the same localhost port (and the same person/computer)?

Is This A Good Question/Topic? 0
  • +

Replies To: Shared Session (AddSingleton)

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • Posts: 53,479
  • Joined: 12-June 08

Re: Shared Session (AddSingleton)

Posted 02 May 2017 - 07:01 AM

I want to say that 'addsingleton' is a longer life than one instance of the page session. Have you looked at 'addscoped'?
Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: Shared Session (AddSingleton)

Posted 02 May 2017 - 08:00 AM

Working as intended, I'm guessing. You'll get the same thing if you make your own singleton or use static. It's within the context or the server, not the client.

I never, ever, use static in web apps for this reason. Yes, I have gotten burnt by this.

However, that's really what sessions are for, if you want a different instance based on session? I tend to write of a lot of lazy initializers that check the session for a value and load it if not found.
Was This Post Helpful? 1
  • +
  • -

#4 andrewsw  Icon User is online

  • lashings of ginger beer
  • member icon

Reputation: 6340
  • View blog
  • Posts: 25,570
  • Joined: 12-December 12

Re: Shared Session (AddSingleton)

Posted 02 May 2017 - 11:16 AM

Thank you both.

It seemed a little odd to me that the singleton would persist throughout the lifetime of the running server application. (Even if people log on and off, and at some point everyone is logged off, the Guid would refer to the running server-instance of the application.)

Still, as you say, "by design".

Yes, we are currently writing and reading a session value for each page/request and it is a little cumbersome. [It is for a multi-page form.]

I believe that ASP.NET Core maintains a session variable throughout the current user's session, and we were hoping to either replicate or take advantage of such a value. It doesn't seem likely with available options.

Anyway, thanks again for the guidance ;)

@modi Looked at Scoped but it doesn't quite cut it.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: Shared Session (AddSingleton)

Posted 02 May 2017 - 12:08 PM

You can't use session? Need some homegrown solution?

A session is really just a cookie and some server side memory. However, you can do it without a cookie. This looks like it might explain newer mechanisms. https://andrewlock.n...n-asp-net-core/

Now, for homegrown, you're still going to generated guid and offer it to the user somehow. They will make all future requests with this a reference, because http is stateless. On your side, usually in a database, but you could use that singleton now, you track the information you choose to associate with that key. A last used timestamp is good to track, to age the id out. Also IP, if you just want to lock that key down a little more.

I've used something like this to authenticate to a Windows domain and then bounce the user out to somewhere else, the uuid and authenticated state being made available through various apis.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1