Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lifetime of a Singleton

I'm building a WCF-Service which is hosted in a windows service. The WCF-Service is called by an ASP.Net application by using a proxy.

using (WcfServiceRef.MonitorClient proxy = new WcfServiceRef.MonitorClient())
{
    //WCF Service proxy
    proxy.Open();
    proxy.DoSomething();
}

Furthermore i have a Singleton class DataAdapter which is used in the WCF-service like this (most of the time it simply forwards the call to the DataAdapter, to be able to change the implementation of the actual data-access easily):

DataAdapter.GetInstance().DoSomething();

My DataAdapter looks like this and holds myData which should be kept alive:

public static DataAdapter DataAdapterInstance;
    private List<CustomObjects> myData;

    internal static ServiceAdapter GetInstance()
    {
        if (DataAdapterInstance == null)
        {
            DataAdapterInstance = new DataAdapter();
        }
        return DataAdapterInstance;
    }

Now here comes what im haveing trouble with: (im not sure if everything im writing here is correct)

  • The windows service' runs theoretically forever (as it should) The
  • WCF-service only runs when it is called/created by my proxy (from asp.net)

When does my DataAdapter (which contains all my data) "die" ... so what im asking here is what is the lifetime of an object when there is actually no reference on it. (i would say its collected whenever the garbage collector runs) - I think my approach isnt too good, i would appreciate any suggestions on how to store my data (preferred without using a DB or external file)

Thank you for looking into my problem!

like image 867
r3try Avatar asked Feb 24 '12 13:02

r3try


People also ask

What is a scoped lifetime?

Scoped lifetime actually means that objects will be the same instance within a created “scope” objects. ASP.NET Core wraps a request within a “scope”, but you can actually create scopes manually. While you can create scopes manually, ASP.NET Core wraps a request within a “scope”.

What is service lifetime in .NET Core?

What are service lifetimes? You can use three lifetimes with the default dependency injection framework. These lifetimes affect how the service is resolved and disposed of by the service provider. Transient: A new service instance is created each time a service is requested from the service provider.

What is difference between singleton and transient?

Singleton is a single instance for the lifetime of the application domain. Scoped is a single instance for the duration of the scoped request, which means per HTTP request in ASP.NET. Transient is a single instance per code request.

Should I use transient or scoped?

If you want an instance that lasts for the duration of a user request (that eg might hold details of the user), then use scoped. If you want a new instance every time the container is asked to provide one (a resource access request that needs disposing quickly for example), then use transient.


1 Answers

Static references are "root" references for GC, so they will "never" die (until AppDomain is unloaded)

like image 197
Alexey Raga Avatar answered Sep 28 '22 08:09

Alexey Raga