【问题标题】:Can't write to blob storage from inside ASP.NET MVC无法从 ASP.NET MVC 内部写入 blob 存储
【发布时间】:2011-12-05 08:28:09
【问题描述】:

我有一部分用于将文本上传到 Azure blob 存储的代码。它使用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 );

到目前为止,代码运行良好。现在,我将该代码的调用添加到 ASP.NET MVC2 应用程序的各个位置以调试后者,并且代码静默不执行任何操作 - 没有异常,没有新的 blob,没有任何东西。如果我尝试获取

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

然后将结果发布到网页上 - 再次没有异常,字符串为空。如果我尝试获取

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

再次没有异常,字符串为空。

同样的上传代码可以在包含 MVC 应用程序的同一网络角色的 OnStart() 内部正常工作。

写入 blob 存储不工作的可能原因是什么?我该如何解决这个问题?

【问题讨论】:

  • 您在 Global.asax Application_Start 事件处理程序中有 Microsoft.WindowsAzure.CloudStorageAccount.SetConfigurationSettingPublisher 方法调用吗?
  • @Yuriy Rozhovetskiy:现在我确实有,但没有帮助。
  • 你是如何创建 CloudStorageAccount 实例的?
  • 你能在你的问题中加入一些真实的代码吗?
  • @Yuriy Rozhovetskiy:我解析了连接字符串 - 添加了代码。

标签: .net asp.net-mvc windows iis azure


【解决方案1】:

好的,问题解决了。现在有点丰田五个为什么。

Q 为什么文本不能上传到 Blob 存储?
A 不会,因为上面的代码没有被调用。

Q 为什么没有调用该代码?
A这并不是因为它的调用依赖于使用反射检测Microsoft.WindowsAzure.ServiceRuntime 是否已加载到当前应用程序域中的松散耦合代码。程序集未加载,因此代码甚至不会尝试访问 blob 存储,并会退回到“我不在 Azure”模式。

WTF???您之前没有测试过该代码吗?
A 我已经测试过了,但不是在 MVC 应用程序中,而且在 MVC 应用程序中的行为似乎有所不同与不在 MVC 应用程序中相比。该程序集是在我之前测试的配置中加载的。

Q 如何强制加载程序集?
A不确定,但看起来像在<system.web><compilation><assemblies> 下添加它可以解决问题。 Here's a question specifically about that.

【讨论】:

    猜你喜欢
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 2019-09-05
    • 2013-08-15
    • 1970-01-01
    相关资源
    最近更新 更多