Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8715121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:46:21+00:00 2026-06-13T05:46:21+00:00

I have created a custom caching provider for my MVC application. I will use

  • 0

I have created a custom caching provider for my MVC application. I will use this class to store/retrieve session data to an outside service (like memcached or Redis).

I would like to create the object instance once at application start so that I can reference the object from any controller, and only have to ‘new up’ the instance once. I was thinking that I would instantiate the class in the Global.asax Application_Start method. However, the instance does not seem to be accessible in any of the controllers.

What is the preferred way to instantiate and then access a (global) class in MVC?

Here is a copy of my ‘simplified’ class:

 public class PersistentSession : IPersistentSession
    {
        // prepare Dependency Injection
        public ICache cacheProvider { get; set; }

        public bool SetSessionValue(string key, string value)
        {
            return cacheProvider.PutToCache(key, value);
        }

        public bool SetSessionValue(string key, string value, TimeSpan expirationTimeSpan)
        {
            return cacheProvider.PutToCache(key, value, expirationTimeSpan);
        }

        public string FetchSessionValue(string key)
        {
            return cacheProvider.FetchFromCache(key);
        }
    }

I want to instantiate it one time so that I can access it from all controllers application wide, something like this:

 // setup PersistentSession object
 persistentSession = new PersistentSession();
 string memcachedAddress = WebConfigurationManager.AppSettings["MemcachedAddress"].ToString();
 string memcachedPort = WebConfigurationManager.AppSettings["MemcachedPort"].ToString();

 persistentSession.cacheProvider = new CacheProcessor.Memcached(memcachedAddress, memcachedPort);

Where/How in MVC should the object be instantiated to get global access from all controllers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:46:22+00:00Added an answer on June 13, 2026 at 5:46 am

    I can’t see the problem !!

    all you have to do is to add (static) keyword to the definition of the methods of PersistentSession class:

    public class PersistentSession : IPersistentSession
    {
        // prepare Dependency Injection
        public static ICache cacheProvider { get; set; }
    
        public static bool SetSessionValue(string key, string value)
        {
            return cacheProvider.PutToCache(key, value);
        }
    
        public static bool SetSessionValue(string key, string value, TimeSpan expirationTimeSpan)
        {
            return cacheProvider.PutToCache(key, value, expirationTimeSpan);
        }
    
        public static string FetchSessionValue(string key)
        {
            return cacheProvider.FetchFromCache(key);
        }
    }
    

    . and you can access them using the following code from anywhere :

    PersistentSession.SetSessionValue (key , value);
    

    you can also add a static constructor to initialize any fields before accessing any member, and the constructor is called before a member of static class is accessed for the first time, so you can be sure that your class is set before being used.

    public static PersistentSession ()
    {
    //Put your initializing code, for example:
    cacheProvider = new CacheProvider();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom method that will return unique items, together with the
I have created custom master page with custom elements included according to this link:
I have created custom exception class public class Web2PDFException : Exception { public Web2PDFException(string
i m writing one application in which i have created custom list field for
I have created custom menu in my application, i have already set its background,border
I have created custom View called Color . I use object of Color to
I have created custom posts and I want one page in my site to
I have created custom jQuery UI widget called uiPopover, very similar to UI-dialog (in
I have created custom cell in which there are n number of image views.
friends, i have created custom title bar using following titlebar.xml file with code <?xml

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.