Saving the cache control property in a file when it is returned as a stream from VirtualPathProvider

I implemented VirtualPathProvider to return theme files (images, css) for an Azure website with an Azure CDN. It works fine, except for one thing: files that come from the CDN have a caching property set to "private" and therefore are never cached.

Actual drops have their properties correctly, and if I get access to it with direct URLs (i.e. not via VPP), then cache management will be correct.

The problem is the Open () method of the VirtualFile class, which I have to implement in order to return the file as a stream?

public override Stream Open() { CloudBlobClient client = new CloudBlobClient(cdnURL); CloudBlob blob = client.GetBlobReference(blobURL); blob.FetchAttributes(); MemoryStream stream = new MemoryStream(); BlobRequestOptions options = new BlobRequestOptions(); options.BlobListingDetails = BlobListingDetails.Metadata; blob.DownloadToStream(stream,options); stream.Seek(0, SeekOrigin.Begin); return stream; } 

Search on this subject, and I believe that most people have a problem in a different way - i.e. files are cached when they do not want them to be. However, none of the examples I can find refers to a file from a different URL. It seems that they all use databases or just different physical paths.

0
source share
2 answers

Thanks to this answer on the asp.net forum http://forums.asp.net/post/4716700.aspx

I solved the problem by adding to my Open () method:

 HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public); HttpContext.Current.Response.Cache.AppendCacheExtension("max-age=86400"); 
+2
source

I think you might have missed an important point in how CDN takes advantage. CDN helps by placing resources closer to the client requesting the file. that is, when a client requests a file, it goes directly to the CDN URL. What seems to be happening here, you load the file from the CDN into the code that starts the web server, and then return the stream to the client from there.

Please correct me if I am wrong.

It is also worth noting that the cache properties are not part of the stream of files that you return, these are additional properties that can be found in CloudBlob.Properties.CacheControl

0
source

All Articles