1

I'm trying to set up an object to be a singleton using Unity, but the object is being created every time I make a request.

public class ShareTubeRoomRepository : IShareTubeRoomRepository
{
    public ShareTubeRoomRepository()
    {
        //load my base data
    }

Unity Configuration

    public static void RegisterTypes(IUnityContainer container)
    {
        container.RegisterInstance<IShareTubeRoomRepository>(new ShareTubeRoomRepository(), new ContainerControlledLifetimeManager());
    }

I have also tried the following:

container.RegisterType<IShareTubeRoomRepository, ShareTubeRoomRepository>(new ContainerControlledLifetimeManager());

I put a breakpoint at the constructor for the repository and it's being called every time a page loads in my MVC application. Every resource I've found has said that these methods of registering the type should create it as a singleton, so why is it recreating this instance every request?

I tried making the data that I want to store static so it won't get refreshed, and instantiating it in the static constructor, but even the static constructor is getting called multiple times. According to this answer: https://stackoverflow.com/a/5119154/526704 that shouldn't be happening, right?

Community
  • 1
  • 1
DLeh
  • 23,806
  • 16
  • 84
  • 128
  • Your premise is incorrect. These are instance types and, as such, when the container resolves the concrete type, it will create an instance of that type. In addition, why would you NOT want your Repository to be instantiated every time it is called? If you want to create a singleton, you should do so inside of the repository, but I wouldn't necessarily recommend that since that leads to its own set of issues. – David L Jul 14 '14 at 13:47
  • Okay, I wasn't sure if I was correct about that. In this case, i'm trying to create objects that are only populated while the application is running, and I want those objects to be accessible across all requests. – DLeh Jul 14 '14 at 13:49
  • So for my use, using a database to persist the data wouldn't be right because I don't want any of the data to stay after the application terminates. – DLeh Jul 14 '14 at 13:50
  • Considering that this is a MVC application, you want to try to treat it as stateless as possible unless you MUST have a singleton. If it is something like session data, store that in a cache system, sql server session or some other mechanic if you need to. – David L Jul 14 '14 at 13:50
  • I tried changing the data i want to store to static and added a static constructor, but even the static constructor is being called. see the last paragraph (from edit). Any ideas there? – DLeh Jul 14 '14 at 14:54

2 Answers2

1

I tried making the data that I want to store static so it won't get refreshed, and instantiating it in the static constructor, but even the static constructor is getting called multiple times.

That means you've got multiple AppDomains in your application. I suppose you're running it on IIS with multiple workers. In that case it will create a singleton per AppDomain. Make sure you use a single AppDomain.

Bartosz Wójtowicz
  • 1,321
  • 10
  • 18
  • That probably explains why I got this behavior when running on one machine, but not when running on another. Do you have any information about what setting needs to be changed? I'm looking at the IISExpress config file and I can't find anything about domains in it. – DLeh Jul 14 '14 at 16:45
  • That you will find under right click My Comupter->Manage->Services and Applications-> IIS should be there. – Bartosz Wójtowicz Jul 15 '14 at 07:58
0

I solved this by creating a property that relied on using the HttpContext.Current.Application dictionary (https://stackoverflow.com/a/3733549/526704)

System.Web.HttpContext.Current.Application.Lock();
System.Web.HttpContext.Current.Application["Name"] = "Value";
System.Web.HttpContext.Current.Application.UnLock();
Community
  • 1
  • 1
DLeh
  • 23,806
  • 16
  • 84
  • 128