2

I am using MVC5 and Unity. I am a little confused reading about thread safe singletons. Do multiple users share the same singleton object? My understanding is that a singleton object would be created for every request and two users would each have their own singleton. Is this correct?

[UPDATE]

Let say I have a Singleton class with a property Instance to get the instance. Will two users accessing the website at the same time be accessing the same singleton object when calling Instance? What is the scope of an object? Is the scope shared among multiple users or does every user have their own scope? Will Instance return the same object for both users?

1 Answer 1

3

It depends on what exact lifetime manager you use. If by "singleton" you mean the container controlled lifetime manager, then the instance will be shared by all users and will not be created for a lifetime of a request.

On the other hand, per http context lifetime managers do exactly what you expect.

Neither has the "singleton" in its name, though.

http://msdn.microsoft.com/en-us/library/ff647854.aspx

http://msdn.microsoft.com/en-us/library/microsoft.practices.unity.perrequestlifetimemanager(v=pandp.30).aspx

Sign up to request clarification or add additional context in comments.

1 Comment

You still don't say how this is related to Unity. If it is not, a singleton is a singleton, shared between all requests of all users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.