简体   繁体   中英

Can't write to blob storage from inside ASP.NET MVC

I have a portion of code for uploading text to Azure blob storage. It uses CloudBlobClient.GetContainerReference().GetBlobReference().UploadText() .

string connectionString = RoleEnvironment.GetConfigurationSettingValue( "StorageConnectionString" );
CloudStorageAccount account = CloudStorageAccount.Parse( connectionString );
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference( containerName );
container.CreateIfNotExist();
DateTime currentTime = DateTime.UtcNow;
String instanceId = RoleEnvironment.CurrentRoleInstance.Id;
int threadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
String blobPath = String.Format( "error-{0}-{1}-{2}", currentTime.Ticks, instanceId, threadId );
container.GetBlobReference( blobPath ).UploadText( textToUpload );

So far the code worked okay. Now I add invokation of that code into various places of an ASP.NET MVC2 application to debug the latter and the code silently does nothing - no exceptions, no new blobs, no anything. If I try to obtain

String justUploaded = container.GetBlobReference( blobPath ).DownloadText( textToUpload );

right after that and post the result on a web page - there's no exception again and the string is empty. If I try to obtain

String uri = container.GetBlobReference( blobPath ).Uri.ToString();

again there's no exception and the string is empty.

The very same upload code works okay from inside OnStart() of the same web role that contains the MVC application.

What's the likely reason for writing to blobs storage not working? How do I resolve this?

Okay, problem resolved. Now a bit of Toyota Five Whys.

Q Why would text not be uploaded to blob storage?
A It wouldn't because the code above was not invoked.

Q Why was that code not invoked?
A It wasn't because its invokation was dependent on loose coupling code that was using Reflection to detect whether Microsoft.WindowsAzure.ServiceRuntime was loaded into current application domain. The assembly was not loaded, so the code would not even try to access blob storage and would fall back into "I'm not on Azure" mode.

Q WTF??? Haven't you tested that code earlier?
A I have tested it, but not in MVC app and looks like behavior is different in MVC app compared to not in MVC app. The assembly was loaded in the configuration I was testing earlier.

Q How to force loading the assembly?
A Not sure, but looks like adding it under <system.web><compilation><assemblies> solves the problem. Here's a question specifically about that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM