0

I have a C# asp.net MVC application hosted in Azure. Not always, but sometimes, after a deploy, I face an odd error, that I believe is caused by TempData. Restarting the site fixes the issue.

At the end of a controller method, I have the following pattern:

TempData["sampleData"] = sampleData;

return RedirectToAction("DownloadSampleData".... 

Then, at the start of DownloadSampleData I have:

 var sampleData = TempData["sampleData"] as SampleDataDto;

The error that is received is:

Object reference not set to an instance of an object.

And the line it points to being the issue is:

var sampleData = TempData["sampleData"] as SampleDataDto;

If the site is restarted, the issue seems to go away. This does not happen every time a deploy takes place.

What I am hoping to learn more about here is, how is TempData initiated? Would I be correct in assuming the error is because TempData is not available at that point, and therefore causes the Object Reference error?

Any thoughts on what could cause this are appreciated as I cannot seem to find anything relevant through Google searches.

Keith
  • 1,008
  • 10
  • 19
  • I think tempdata can only be accessed in one consecutive request, if you want to keep it you need to use tempdata.keep – Muhammad Waqas Aziz Oct 09 '20 at 11:12
  • The code above does work. It is just that sometimes, after a deploy, it errors when it tries to extract the data from TempData. Restarting the site fixes it. Also, the error is specific to the line where it tries to extract the data from it, not the lines after where it then tries to use the extracted object, which makes me think the error is with TempData itself not being initialised, or similar. – Keith Oct 09 '20 at 11:14
  • Tempdata was kept only up to one consecutive request after that it is disposed. that could be the reason for null reference exception try using TempData.Keep("sampleData"), and check if it generate error again, but then you need to manually clear it when its not needed – Muhammad Waqas Aziz Oct 09 '20 at 11:18
  • I can try that, but with the error being quite sporadic, it is hard to test. Would you not expect though, that if a key didn't exist in TempData, it would not produce the error it is? Would it not fail after, with the same error, when it tried to use the null object? – Keith Oct 09 '20 at 11:19
  • For example, if I remove the addition of sampleData to TempData, it errors, but not on the line mentioned in the question, it fails after, because there is nothing to extract from TempData with the name sampleData, so the variable is null. – Keith Oct 09 '20 at 11:21
  • You are trying to save `sampleData` to `TempData["sampleDate"]` and retrieving it from `sampleData`. It is completely a different key, hence null reference error. – Harshita Singh Oct 12 '20 at 08:32
  • @HarshitaSingh-MSFT sorry - a typo. I have updated. As I mentioned, a restart with no code changes fixes this issue. The code is not the problem here, at least, not on its own. – Keith Oct 12 '20 at 08:54

1 Answers1

0

Hoping that this is the actual code that you are using. Please note that you are trying to save variable sampleData value to TempData["sampleDate"] and retrieving it from sampleData. It is completely a different key, hence the null reference error.

Harshita Singh
  • 4,590
  • 1
  • 10
  • 13
  • Sorry, a typo on my part. As the questions states, a restart of the site fixes the issue. As far as I can see, the code is not the problem here, as it works just fine after a restart. To add, if you mock this up, the null reference would not be hit on that line, it would be hit when you tried to use something on the variable, that was null. So I am not sure why trying to extract from TempData would cause that error. – Keith Oct 12 '20 at 08:54
  • Have a look at the answer provided here, can you try returning View? - https://stackoverflow.com/questions/45356403/null-tempdata-when-passing-data-from-controller-to-view-mvc – Harshita Singh Oct 12 '20 at 09:59
  • What I am confused about really is, why is this a temporary issue after a deploy? Why does restarting the site stop it? And, why is the error returned on the line it is? – Keith Oct 12 '20 at 10:46
  • Can you try returning a view once? Then we will see what is the issue. Also, add complete code of line `RedirectToAction`. – Harshita Singh Oct 13 '20 at 06:53