3

In my mvc 4 application i have a method which is streaming a file from a azure blob.

My code is pretty simple, all im doing is fetching the blob i wanna stream and then return it as FileStreamResult with.

return new FileStreamResult(blob.OpenRead(), contentType);

This code works fine. But I also want to set cache headers, so before I return the file im setting the headers like this :

Response.Cache.SetLastModified(DateTime.Now);
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetMaxAge(new TimeSpan(0, 0, 0, 80000));
Response.Cache.SetSlidingExpiration(true);

Strangely modifying the Response.Cache doesn't work.

All im getting is an error:

Object reference not set to an instance of an object.

I have really tried to find the source of the exception but it always happens after Application_EndRequest.

I really can't understand why setting the Response.Cache would cause it all to break.

From the stacktrace :

[NullReferenceException: Object reference not set to an instance of an object.] System.Web.HttpContext.RequestRequiresAuthorization() +30
System.Web.Caching.OutputCacheModule.OnLeave(Object source, EventArgs eventArgs) +9778761 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Exec‌​ute() +136
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

I guess that something that has to do with the "Response.Cache" object is disposed too early. Maybe I should try the set the cache-headers manually?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Krika
  • 453
  • 7
  • 20
  • Set the debugger to break on all exceptions and post the stack trace of the exception. Enable "Show external code". Maybe you're disposing the "blob" before it is being read by ASP.NET?! – usr May 11 '14 at 16:37
  • Updated with stacktrace. – Krika May 11 '14 at 16:51
  • And when you uncomment these 4 cache lines (just those!) the exception goes away?; Is `User.Identity` null when you check it in the debugger? – usr May 11 '14 at 17:51

0 Answers0