How to fix problems with data cacheing in ASP.net MVC 4, SignalR and Entity Framework How to fix problems with data cacheing in ASP.net MVC 4, SignalR and Entity Framework

How to fix problems with data cacheing in ASP.net MVC 4, SignalR and Entity Framework

The Problem

I was using the wonderful SignalR libraries to create a status window (I have a VERY long running process in my new Data Hammer project, it takes about 15 minutes to run) and I need to inform the user on where the process is at any given time.  My original code looked like this

public class Chat : Hub
{
public void Send(int id)
{

DataHammerContext ctx = new DataHammerContext();

while (true)
{

string strName=ctx.ProcessSteps.Find(id).Name;
Clients.All.addMessage(“Status: ” + strName);
Thread.Sleep(1000);
}
}
}

And this would always display the same status, quite maddening.

The Cause

After much investigation I realized that my model of how Signal R works was flawed.  For whatever reason (by which I mean too long to go into here) the data context does not update with the while loop.

The Solution

The fix is simple, just recreate the data context every time and everything works like it should – here is a sample of the updated code.

 

public class Chat : Hub
{
public void Send(int id)
{

 

while (true)
{

DataHammerContext ctx = new DataHammerContext();
string strName=ctx.ProcessSteps.Find(id).Name;
Clients.All.addMessage(“Status: ” + strName);
Thread.Sleep(1000);

}
}
}

 

Written By Steve French

 

Leave a Reply

Your email address will not be published. Required fields are marked *






Copyright 2011 Digital Tool Factory. All Rights Reserved. Powered by raw technical talent. And in this case, WordPress.