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.Execute() +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?