In my program, I have multiple points where it connects to Blob storage to read/write files. They all work fine. However, in a different function but the same class as where another Blob storage reference is made, I am getting an ArgumentNullException stating “Value cannot be null” from the following line of code:
CloudStorageAccount account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
I know the StorageConnectionString is properly configured, because when I remove the line of code using this function, the same line of code works in other functions. Can anyone provide some insight as to why this one function is causing a problem, but not the others?
Not sure if this would be too helpful, but here’s the 4 lines of code I’m using. It’s pretty standard.
CloudStorageAccount account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient client = account.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference(CONTAINER);
CloudBlockBlob blob = container.GetBlockBlobReference(FilePath(null, imageId));
Based on your comment this could be related to the fact that you store the connection string in the web.config. In some cases it might look like your code is running in the same process, but in reality it isn’t. Take the WebRole.cs/WorkerRole.cs classes for example. Simply put, the code in these classes will run in a different process, which means they won’t have access to your web.config. The WebRole.cs will read its settings from the WaIISHost.exe.config file for example.
Could you remove the connection string from the web.config and add it to the service configuration instead? You don’t need to modify the code since the
CloudConfigurationManagerwill look in the service configuration first, and if it doesn’t find the setting here it will fall back to the web/app.configNote: In this answer I’m assuming you are using a Web Role and not a Web Site.