locked
unable to use System.Web.Caching.Cache in server RRS feed

  • Question

  • User-1471881183 posted

    hello all,

    i wanna add some data into cache so, i used below code. its working perfectly from development computer when i publish into server then its throwing exception "Object reference not set to an instance of an object. Inner Exception is ", using log i found the place of issue, its at exactly if (cache["Key1"] == null) 

    System.Web.Caching.Cache cache = new System.Web.Caching.Cache();

    if (cache["Key1"] == null)

    {
       cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);

    }

    the reason why im using System.Web.Caching.Cache is, i have another vb.net project, its like Public Shared Function GetContentString(ByRef cache As Cache) there its expecting object of System.Web.Caching.Cache

    really i dont know the reason of why its not working only from server? do i need to change anything in IIS? experts can you please help me!!

    Thank you

    Wednesday, October 10, 2018 10:02 AM

Answers

  • User-1471881183 posted

    its resolved using System.Web.Caching.Cache cache = HttpRuntime.Cache;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 15, 2018 10:50 AM

All replies

  • User475983607 posted

    This could be a design bug but you have not explained the design clearly and there's no source code.   There's no way to provide accurate assistance.

    Can you explain the design?  Are you trying to pass data between applications hosted on IIS?

    Wednesday, October 10, 2018 11:31 AM
  • User61956409 posted

    Hi winseealn@hotmail.com,

    its throwing exception "Object reference not set to an instance of an object.

    I suspect that in elsewhere you get cache["Key1"] with null and convert it to other type or assign it to another variable etc, which might cause the issue.

    Besides, if you'd like to share data between multiple applications built with different languages (or platform), you can use Redis Cache etc. Or store the data in a temporary table in your database.

    With Regards, 

    Fei Han

    Thursday, October 11, 2018 7:00 AM
  • User-1471881183 posted

    its resolved using System.Web.Caching.Cache cache = HttpRuntime.Cache;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 15, 2018 10:50 AM