0

I have a Singleton Class, It will Initial in every request, But I just want it Initial only 1 time in hole application life. which mean only first request need Initial it.:

public class TestUtilitys
    {
        #region Instance
        private TestUtilitys(){
            // Execute in every request
            Log.Debug("Initial");
        }

        private static TestUtilitys_instance;
        public static TestUtilitys Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new TestUtilitys();
                return _instance;
            }
        }

        #endregion Instance

    private string Pro;
    }

And then I can access it in every request when Action get it in Controller.

public class TestAction(){


   if (null == TestUtilitys.Instance.Pro)
   {
        // execute in every request
        TestUtilitys.Instance.Pro = "test";
    }
}

In my Web.config, I just add these different with my another project

<!-- cache -->
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <clear />
          <!-- 8 Hour -->
          <add varyByCustom="NavStatic" varyByParam="*" duration="28800" name="NavStatic" location="Server" />
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>

    <compilation targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />



    <sessionState mode="InProc" timeout="43200" /> 

As you see, the 2 position will execute every time when the Action get a request. I don't know Why The Singlton always need to ReInitial in every request. I'm not clear any cache or something. I just use DevTrends.MvcDonutCaching for my project. I'm using IIS8.5 on Windows Server 2012 R2. the .Net version is : 4.5.1 But on my Local machine it's all fine on my WIN8.1(use Visual Studio 2013) when I debug.

Update

In the Singleton class method, I also find after I set the memory cache in last request, next request can't get it from memory cache. it will be null,

// In a method for set Cache
// set in last request
 MemoryCache.Default.Set("key", obj, DateTime.Now);

    ``             ``

// In an another method for get Cache
// get it in next request, but obj will be null
object obj = MemoryCache.Default.Get("key");

What's wrong my website. How do I fixed it?

Update 2

I search a lot, I saw some people said: Static fields are shared across the whole AppDomain. But what's going on with my website??

Answer: Static property always null in every request after set value on ASP.NET MVC5

Community
  • 1
  • 1
qakmak
  • 1,287
  • 9
  • 31
  • 62
  • 4
    This Singleton implementation is not thread-safe. You better use `Lazy`. See http://csharpindepth.com/Articles/General/Singleton.aspx#lazy – haim770 Feb 08 '15 at 15:14
  • @haim770, but my problem is, I need to let it ``initial only 1 time``. not every request initial – qakmak Feb 08 '15 at 15:50
  • From my experience, you _can_ have a singleton spanning an `AppDomain` when hosting in IIS. I do that with all my IIS-hosted WCF services. Is it possible your MVC app is spawning **multiple** `AppDomains`? If so that would explain the multiple singletons. Is there an IIS AppPool config you can try? Singletons can be made thread-safe in any event –  Feb 08 '15 at 16:34
  • @MickyDuncan, Sorry, Maybe my English not very well. what do you want me to try? thank. – qakmak Feb 08 '15 at 17:12
  • Better initialize it in Application_Start event. With you current code instance is initialized per request basis and destroyed once request is served. I think Application_Start event is right place so that it wont get instantiated for every request. – Pankaj Kapare Feb 08 '15 at 17:27
  • @PankajKapare, But Why...? That's a static property which the singlton used, I also let the Application_Start() access it, after the first request. but the result is same. – qakmak Feb 08 '15 at 17:33
  • Even though property is static but its scope is limited to request and its not scoped at application level. If it would have been scoped at application level it wont get reinitialized for every request and instance will be reused. – Pankaj Kapare Feb 08 '15 at 17:40
  • @PankajKapare, So How do I fixed it. even try in Application_Start() it still not working, I also ``Update`` my problem – qakmak Feb 09 '15 at 02:48
  • Even if you do manage to create a singleton, it'll still get reset by any application pool recycle. If you need something that retains state *persistently*, use a database (or a file, etc) – Damien_The_Unbeliever Feb 09 '15 at 09:17
  • @Damien_The_Unbeliever, It's ok with reset by application pool. But currently problem not about recycle. – qakmak Feb 09 '15 at 09:52

0 Answers0