We’ve been working with Shared Access Policies in Azure for the last week or so, and for the most part it was working. But it would only work for a while, then it would stop. Then it would start working again. It took some help from Steve Marx and Jai Haridas on the Azure Team to figure out what was going on.
This first piece of code is part of our storage manager that retrieves the Uri for the blob from Azure Blob Storage, and creates a Shared Access Policy for that blob so that our Silverlight Video Player can directly access the blob without going through a very slow web service call.
In our Silverlight Web Service, we call this manager class and return the Uri embedded in an XElement. Originally we did this to allow us to return other information with the Uri, but at this point, the Uri is all we are passing through.
On the Silverlight side, we take make a call to this web service, extract the Url from the XElement, and pass the Url into the MediaPlayer like so
This is all pretty simple code (and the above code works, by the way).
Where we ran into problems was in the Web Service, when populating the xml string. Originally, we used blobUri.ToString() instead of blobUri.AbsoluteUri. This caused big issues (403 errors returned from the Azure Storage Service), when the Video Player tried to retrieve the blob because the Url returned from the Shared Access Policy generator can have spaces in it. And Uri.ToString and Uri.AbsoluteUri work very differently when handling spaces. I did not know this until last night. Uri.ToString unescapes the Uri before returning it.
So why did it work sometimes, and not all the time? Simple. Sometimes the Uri from the Shared Access Policy Generator has spaces, and sometimes it does not. The former did not work, while the latter did. We were looking for a pattern in the number of times we called the web service, or the interval between calls, and the size of the video blob. But it was as simple as a single space.
I think we spent at least 8 hours over the last two days trying to track down this bug. Hopefully this saves someone else some grief.
This post is cross posted on http://www.palador.com/blog/Blog/Silverlight--Azure-Shared-Access-Policy-Issue intentionally.