0

I am using System.Runtime.Caching to persist variables in cache, but later i cannot access to them because are disposed and the program throws a ObjectDisposedException with the message: DbContext has been disposed

I didn't execute Object.Dispose(), so the class ObjectCacheare disposing it in background.

In what moment at what moment does it happen? after doing cache.add?

It can be avoided?

How i made it:

Making the list:

using (GesBrokerSuiteEntities ctx = new GesBrokerSuiteEntities())
                {
                   
                    rel_tareas_orquestador nuevoregistroOrquestador = new rel_tareas_orquestador();

                    nuevoregistroOrquestador.id_robot_hija = idProyindividual;
                    nuevoregistroOrquestador.id_robot_padre = 2;
                    nuevoregistroOrquestador.id_robot_tarea_padre = "2";
                    nuevoregistroOrquestador.id_robot_tarea_hija = idProyindividual - 1+"";
                    nuevoregistroOrquestador.id_tarea_hija = Int32.Parse(id2);
                    nuevoregistroOrquestador.id_tarea_padre = 2;
                    nuevoregistroOrquestador.sincronizada = 2;
                    nuevoregistroOrquestador.esperar_padre_completa = 2;

                    ctx.rel_tareas_orquestador.Add(nuevoregistroOrquestador);
                    listaRelTareasOrquestador.Add(ctx);

1-Adding the list to cache

CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddHours(1.0);
                    
cache.Add("listaOrquestador", listObject, cacheItemPolicy);

2-Then i get it in another method

listaRelTareasOrquestador = (List<GesBrokerSuiteEntities>)cache.Get("listaOrquestador");

After this, i cannot access to the content of the list because it disposed.

UPDATE: withing the using() works, the elements aren't disposed.

now the problem us this, when trying to get the saved values, it obtains the database rows:

nuevoregistroOrquestador.id_robot_hija = listaRelTareasOrquestador[0].rel_tareas_orquestador.FirstOrDefault().id_robot_hija;

I know it is executing a query in my DB, any way to obtain the data i saved previously? Like a local variable.

UPDATE 2

Now when i'm trying to access the values into the list,i am not receving the previous ones:

rel_tareas_orquestador nuevoregistroOrquestador = new rel_tareas_orquestador();

nuevoregistroOrquestador.id_robot_hija = listaRelTareasOrquestador[0].rel_tareas_orquestador.FirstOrDefault().id_robot_hija;
nuevoregistroOrquestador.id_robot_padre = registro.rel_tareas_orquestador.FirstOrDefault().id_robot_padre;
nuevoregistroOrquestador.id_robot_tarea_padre = registro.rel_tareas_orquestador.FirstOrDefault().id_robot_tarea_padre;
nuevoregistroOrquestador.id_robot_tarea_hija = registro.rel_tareas_orquestador.FirstOrDefault().id_robot_tarea_hija;

I know the methods are returning database values due to querys, how i can access to the list values i saved previously?

Visual Studio shows me that in debugging:

enter image description here

Angel Gonzalez Pena
  • 145
  • 1
  • 1
  • 10
  • We know nothing about your cache. Please add config/setup and maybe some code (usage example)? – Fildor Sep 08 '20 at 11:14
  • I suspect, you are using some Linq with [deferred execution](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/deferred-execution-example). Then you add the _query_ to the cache. That means, the dbContext can be disposed while the query is sitting in cache and by the time you execute the query, it will fail. Execute the query at once and cache its result. – Fildor Sep 08 '20 at 11:26
  • Please add how you construct your `listObject`. – Fildor Sep 08 '20 at 11:27
  • `so the class ObjectCache are disposing it in background.` No it isn't. Unless you are doing something like https://stackoverflow.com/a/32706574/34092 . – mjwills Sep 08 '20 at 11:33
  • 1
    ^^ By using `using` you explicitly dispose it. That's how that works. – Fildor Sep 08 '20 at 11:38
  • @mjwills seeing the duplicate and your comments i think i know how solve the problem, thanks! – Angel Gonzalez Pena Sep 08 '20 at 11:40
  • @Fildor ok, then i would try to persist the list without the using, right? Gonna try, thanks! – Angel Gonzalez Pena Sep 08 '20 at 11:41
  • 2
    Can you explain to us _why_ you want to keep the `ctx`? We can help you do it - sure. But it is _weird_ to want to do it. It is like asking how to light matches with the side of your face - possible, but probably suggesting you are attacking the wrong problem. – mjwills Sep 08 '20 at 11:42
  • @mjwills i don't want to keep the ctx anymore, the previous problem is solved, thx! Now one little doubt. Would it be possible to access the values ​​that I stored manually in the list, and not those of the database? I updated the question, i know it's executing query against the table, but i want to know if it possible to store only the values I want, without obtain all the rows. Like a local variable. – Angel Gonzalez Pena Sep 08 '20 at 12:45
  • 1
    Yes you can do that. – mjwills Sep 08 '20 at 12:55
  • @mjwills I added an "update 2" due to I can't access that way – Angel Gonzalez Pena Sep 09 '20 at 06:57

1 Answers1

0

It is disposed immediately after the closing bracket (if you mean a construction like this):

using (var reader = new StringReader(manyLines))
{
    string? item;
    do {
        item = reader.ReadLine();
        Console.WriteLine(item);
    } while(item != null);
}
// reader disposed here
Roman Ryzhiy
  • 1,540
  • 8
  • 5
  • I updated the post and wrote how i did it, the problem is when i call the Get method, before the using, the list elements are already disposed. – Angel Gonzalez Pena Sep 08 '20 at 11:26