Search code examples
c#azureazure-blob-storageazure-in-role-cache

Serializing CloudBlockBlob for Caching


I am implementing Azure In-Role Caching for a Web API Web Role that I have. I would like to be able to Cache the BlobReference for specific blobs that do not change often. Here is the code I am using to get the BlobReference. The items I am getting are zip files that are of type CloudBlockBlob

public ICloudBlob GetBlob(string containerName, string blobName)
{
   CloudBlobContainer container = GetBlockBlobContainer(containerName);
   ICloudBlob blob = container.GetBlobReferenceFromServer(blobName);
   return blob;
}

However when I try to Put the item in Cache, it says that CloudBlockBlob is not serializable. Since I cannot modify that class to be serializable, is there a work-around for this? If not, what is the preferred method to cache the blob metadata for blobs that do not change often?


Solution

  • Copy out what you need into a DTO. It's generally not a good pattern to put complex objects into appdomain global data structures. You never know what kind of object graph is behind them and whether they are thread-safe. If the classes come from a 3rd party library you can't know what's inside. You need to control the objects yourself.

    Global state in long running processes is very critical resource. If you mess up the application might be unavailable until manual process restart which is catastrophic.