首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# + Mono:运行Azure-Storage时出错

C# + Mono:运行Azure-Storage时出错
EN

Stack Overflow用户
提问于 2017-08-08 18:27:15
回答 1查看 236关注 0票数 1

我有一个在Windows环境下运行良好的应用程序。应用程序使用C# 4.6.1编写,并使用Azure上传\下载blobs。

我想在linux服务器(v16)上运行相同的应用程序。在服务器上安装mono之后,我尝试运行应用程序。程序异常崩溃。

因此,我添加了日志,发现问题开始于Azure-Storage (v7.0.0,通过nu-get安装)。

这是我的相关代码:

代码语言:javascript
复制
CloudBlockBlob blob = container.GetBlockBlobReference(string.Join("", blob_path));
using (MemoryStream ms = new MemoryStream())
{
    blob.DownloadToStream(ms); // <--- BOOM
    //...
}

这是我在使用Mono运行时遇到的例外:

代码语言:javascript
复制
Microsoft.WindowsAzure.Storage.StorageException: read failed 
---> System.IO.IOException: read failed 
---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
  at Mono.Net.Security.MobileAuthenticatedStream.CheckThrow (System.Boolean authSuccessCheck) [0x0001b] in <2b0d86369d72459baed0cee98a8e578a>:0 
  at Mono.Net.Security.MobileAuthenticatedStream.get_Context () [0x00000] in <2b0d86369d72459baed0cee98a8e578a>:0 
  at Mono.Net.Security.MobileAuthenticatedStream.ProcessRead (Mono.Net.Security.AsyncProtocolRequest asyncRequest, Mono.Net.Security.AsyncOperationStatus status) [0x00011] in <2b0d86369d72459baed0cee98a8e578a>:0 
  at Mono.Net.Security.AsyncProtocolRequest.ProcessOperation (Mono.Net.Security.AsyncOperationStatus status) [0x0006b] in <2b0d86369d72459baed0cee98a8e578a>:0 
  at Mono.Net.Security.AsyncProtocolRequest.ProcessOperation () [0x0000d] in <2b0d86369d72459baed0cee98a8e578a>:0 
  at Mono.Net.Security.AsyncProtocolRequest.StartOperation () [0x00000] in <2b0d86369d72459baed0cee98a8e578a>:0 
   --- End of inner exception stack trace ---
  at Mono.Net.Security.MobileAuthenticatedStream.EndReadOrWrite (System.IAsyncResult asyncResult, Mono.Net.Security.AsyncProtocolRequest& nestedRequest) [0x00055] in <2b0d86369d72459baed0cee98a8e578a>:0 
  at Mono.Net.Security.MobileAuthenticatedStream.EndRead (System.IAsyncResult asyncResult) [0x00000] in <2b0d86369d72459baed0cee98a8e578a>:0 
  at System.Net.WebConnection.EndRead (System.Net.HttpWebRequest request, System.IAsyncResult result) [0x000d4] in <2b0d86369d72459baed0cee98a8e578a>:0 
  at System.Net.WebConnectionStream.EndRead (System.IAsyncResult r) [0x000af] in <2b0d86369d72459baed0cee98a8e578a>:0 
  at Microsoft.WindowsAzure.Storage.Core.ByteCountingStream.EndRead (System.IAsyncResult asyncResult) [0x00000] in <1a6da444611441028237826045bb8126>:0 
  at Microsoft.WindowsAzure.Storage.Core.Util.StreamExtensions.WriteToSync[T] (System.IO.Stream stream, System.IO.Stream toStream, System.Nullable`1[T] copyLength, System.Nullable`1[T] maxLength, System.Boolean calculateMd5, System.Boolean syncRead, Microsoft.WindowsAzure.Storage.Core.Executor.ExecutionState`1[T] executionState, Microsoft.WindowsAzure.Storage.Core.Util.StreamDescriptor streamCopyState) [0x002c6] in <1a6da444611441028237826045bb8126>:0 
  at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T] (Microsoft.WindowsAzure.Storage.Core.Executor.RESTCommand`1[T] cmd, Microsoft.WindowsAzure.Storage.RetryPolicies.IRetryPolicy policy, Microsoft.WindowsAzure.Storage.OperationContext operationContext) [0x003c7] in <1a6da444611441028237826045bb8126>:0 
   --- End of inner exception stack trace ---
  at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T] (Microsoft.WindowsAzure.Storage.Core.Executor.RESTCommand`1[T] cmd, Microsoft.WindowsAzure.Storage.RetryPolicies.IRetryPolicy policy, Microsoft.WindowsAzure.Storage.OperationContext operationContext) [0x006b5] in <1a6da444611441028237826045bb8126>:0 
  at Microsoft.Wind… Read more                        
  at Microsoft.WindowsAzure.Storage.Core.ByteCountingStream.EndRead (System.IAsyncResult asyncResult) [0x00000] in <1a6da444611441028237826045bb8126>:0 
  at Microsoft.WindowsAzure.Storage.Core.Util.StreamExtensions.WriteToSync[T] (System.IO.Stream stream, System.IO.Stream toStream, System.Nullable`1[T] copyLength, System.Nullable`1[T] maxLength, System.Boolean calculateMd5, System.Boolean syncRead, Microsoft.WindowsAzure.Storage.Core.Executor.ExecutionState`1[T] executionState, Microsoft.WindowsAzure.Storage.Core.Util.StreamDescriptor streamCopyState) [0x002c6] in <1a6da444611441028237826045bb8126>:0
  at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T] (Microsoft.WindowsAzure.Storage.Core.Executor.RESTCommand`1[T] cmd, Microsoft.WindowsAzure.Storage.RetryPolicies.IRetryPolicy policy, Microsoft.WindowsAzure.Storage.OperationContext operationContext) [0x003c7] in <1a6da444611441028237826045bb8126>:0 
   --- End of inner exception stack trace ---
  at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T] (Microsoft.WindowsAzure.Storage.Core.Executor.RESTCommand`1[T] cmd, Microsoft.WindowsAzure.Storage.RetryPolicies.IRetryPolicy policy, Microsoft.WindowsAzure.Storage.OperationContext operationContext) [0x006b5] in <1a6da444611441028237826045bb8126>:0 
  at Microsoft.WindowsAzure.Storage.Blob.CloudBlob.DownloadRangeToStream (System.IO.Stream target, System.Nullable`1[T] offset, System.Nullable`1[T] length, Microsoft.WindowsAzure.Storage.AccessCondition accessCondition, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options, Microsoft.WindowsAzure.Storage.OperationContext operationContext) [0x00033] in <1a6da444611441028237826045bb8126>:0 
  at Microsoft.WindowsAzure.Storage.Blob.CloudBlob.DownloadToStream (System.IO.Stream target, Microsoft.WindowsAzure.Storage.AccessCondition accessCondition, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options, Microsoft.WindowsAzure.Storage.OperationContext operationContext) [0x00000] in <1a6da444611441028237826045bb8126>:0 
  at ***.DownloadFile (System.Uri url, System.IO.FileInfo file) [0x000e8] in <1a85ad8f477c4d6f8a3af08c9e99e976>:0 
  at ***.TimedCache+<Add>d__32.MoveNext () [0x00160] in <1a85ad8f477c4d6f8a3af08c9e99e976>:0 
Request Information
RequestID:2025b933-0001-0031-2325-10951e000000
RequestDate:Tue, 08 Aug 2017 09:08:58 GMT
StatusMessage:Partial Content

Linux机器在Google下运行。解决这一问题的一个方向是Goolge防火墙。也许防火墙阻止了与Azure-Storage的通信?

我的代码基本上是从存储中下载一个blob并在本地保存它。奇怪的是,当我检查保存文件的目录时,我可以找到一些文件。什么意思呢,这是时代的一部分。

如何解决这个问题?

更新

根据@Amor的说法,我试着去了解问题出在哪里。我们有两个选择:

  • 问题在于C#代码或Mono。
  • 问题在于环境(Linux \google\.)

我在此页上下载了名为"Storage_REST_CS.zip“的代码。根据需要更新代码(插入我的可信度),并在Windows环境中测试它--运行良好。我取下了二进制文件,并将它们移到Linux机器上进行测试。我有错误:

代码语言:javascript
复制
System.Net.WebException: The remote server returned an error: (403) Server failed to authenticate the request. Make sure the value of Authorization headers formed correctly including the signature.
    at StorageSampleREST.RESTHelper.Retry[T] (StorageSampleREST.RESTHelper+RetryDelegate`1[T] del, System.Int32 numberOfRetries, System.Int32 msPause) [0x0001d] in <033ea4d1a16740f3b711001b7c2f09f5>:0 
    at StorageSampleREST.RESTHelper.Retry[T] (StorageSampleREST.RESTHelper+RetryDelegate`1[T] del) [0x00001] in <033ea4d1a16740f3b711001b7c2f09f5>:0 
    at StorageSampleREST.BlobHelper.ListContainers () [0x00001] in <033ea4d1a16740f3b711001b7c2f09f5>:0 
    at StorageSampleREST.Program.TestBlobStorage () [0x00023] in <033ea4d1a16740f3b711001b7c2f09f5>:0

现在,我试图了解问题出在哪里--在我的代码(c#)或环境()中。因此,我提取了执行的HTTP请求(在Mono上失败),并试图使用cURL直接运行它:

代码语言:javascript
复制
curl -i -H "x-ms-date: Wed, 09 Aug 2017 09:03:46 GMT
x-ms-version: 2009-09-19
Authorization: SharedKey userfiles:***" -X GET https://***.blob.core.windows.net/?comp=list

结果是200 OK。最重要的是,这不是任何防火墙、Linux环境或连接的问题。

现在,重点应该放在Mono设置上,因为使用cURL成功地执行相同的HTTP请求(200)。这不取决于我的特定代码,因为类似的行为也发生在上面提到的示例代码中。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2017-08-09 07:17:01

将使用Https与Storage通信,您的客户端将验证从服务器端收到的证书。迁移到MONO后,请在代码顶部添加以下代码以验证证书。

代码语言:javascript
复制
ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;

public static bool  MyRemoteCertificateValidationCallback(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    bool isOk = true;
    // If there are errors in the certificate chain,
    // look at each error to determine the cause.
    if (sslPolicyErrors != SslPolicyErrors.None)
    {
        for (int i = 0; i < chain.ChainStatus.Length; i++)
        {
            if (chain.ChainStatus[i].Status == X509ChainStatusFlags.RevocationStatusUnknown)
            {
                continue;
            }
            chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
            chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
            chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0);
            chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
            bool chainIsValid = chain.Build((X509Certificate2)certificate);
            if (!chainIsValid)
            {
                isOk = false;
                break;
            }
        }
    }
    return isOk;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45575550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档