如何在Xamarin中使用Microsoft Azure存储模拟器?

时间:2016-07-31 10:08:15

标签: azure xamarin xamarin.android azure-storage azure-table-storage

如何在Xamarin.Android中使用Microsoft Azure存储模拟器? 我已下载Microsoft Azure存储模拟器和Microsoft Azure存储资源管理器。我试图在微软网站上关注教程,但我仍然遇到身份验证问题。当我在免费试用期间使用真实订阅时,一切正常。

我知道我必须使用SAS密钥。但是当我在存储资源管理器中创建表存储然后在那里生成SAS时,它仍然是错误的。也许有人已经这样做了,可以粘贴他的代码或步骤来重现与存储模拟器的连接。

const string connectionString = "SharedAccessSignature=sv=2015-04-05&sr=c&sig=91HcGtCMxWM6%2B13mRDH1kej5Wq1a8gRk4WCFd6CeLuk%3D&se=2016-08-01T12%3A16%3A55Z&sp=rwdl;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1";

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("Test");
bool exists = await table.ExistsAsync();
if (exists)
    System.Console.WriteLine("Exists");
else
    System.Console.WriteLine("Doesnt exist");

错误详细信息:

<RequestResult>
  <HTTPStatusCode>0</HTTPStatusCode>
  <HttpStatusMessage />
  <TargetLocation>Primary</TargetLocation>
  <ServiceRequestID />
  <ContentMd5 />
  <Etag />
  <RequestDate />
  <StartTime>Sun, 31 Jul 2016 13:43:44 GMT</StartTime>
  <EndTime>Sun, 31 Jul 2016 13:43:44 GMT</EndTime>
  <Error></Error>
  <ExceptionInfo>
    <Type />
    <HResult>-2146233088</HResult>
    <Message>Error: ConnectFailure (Connection refused)</Message>
    <Source />
    <StackTrace />
    <InnerExceptionInfo>
      <ExceptionInfo>
        <Type />
        <HResult>-2146233079</HResult>
        <Message>Error: ConnectFailure (Connection refused)</Message>
        <Source>mscorlib</Source>
        <StackTrace>  at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x0005e] in /Users/builder/data/lanes/2923/52635947/source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1005 
  at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (IAsyncResult iar, System.Func`2 endFunction, System.Action`1 endAction, System.Threading.Tasks.Task`1 promise, Boolean requiresSynchronization) [0x00014] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/threading/Tasks/FutureFactory.cs:550 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:535 
  at System.Net.Http.HttpClientHandler+&lt;SendAsync&gt;c__async0.MoveNext () [0x003d6] in /Users/builder/data/lanes/2923/52635947/source/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs:372 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:535 
  at System.Net.Http.HttpClient+&lt;SendAsyncWorker&gt;c__async0.MoveNext () [0x000a9] in /Users/builder/data/lanes/2923/52635947/source/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:276 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357 
  at Microsoft.WindowsAzure.Storage.Core.Executor.Executor+&lt;ExecuteAsyncInternal&gt;d__6`1[T].MoveNext () [0x003cf] in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\WindowsRuntime\Core\Executor\Executor.cs:128 </StackTrace>
        <InnerExceptionInfo>
          <ExceptionInfo>
            <Type />
            <HResult>-2147467259</HResult>
            <Message>Connection refused</Message>
            <Source>System</Source>
            <StackTrace>  at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000cb] in /Users/builder/data/lanes/2923/52635947/source/mono/mcs/class/System/System.Net.Sockets/Socket.cs:1313 
  at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x0019b] in /Users/builder/data/lanes/2923/52635947/source/mono/mcs/class/System/System.Net/WebConnection.cs:195 </StackTrace>
          </ExceptionInfo>
        </InnerExceptionInfo>
      </ExceptionInfo>
    </InnerExceptionInfo>
  </ExceptionInfo>
</RequestResult>

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

我相信您收到此错误是因为您在模拟器中运行此代码。由于模拟器的IP地址为127.0.0.1,因此请求实际上并未访问存储模拟器,而只是您的设备模拟器。这就是为什么你得到这个连接拒绝错误。有关详细信息,请参阅此主题:https://forums.xamarin.com/discussion/1199/how-to-make-our-remote-host-127-0-0-1-reachable-from-android-device-monoandroid-using-vs2010

我建议您使用实际的云存储帐户而不是存储模拟器来测试您的代码。当您使用实际存储帐户时,请使用下面的代码(显然适当地更改连接字符串)。它应该工作:

        var connectionString = "http://127.0.0.1/devstoreaccount1/Testowa?st=2015-07-31T11%3A50%3A00Z&se=2017-0‌​8-01T11%3A50%3A00Z&sp=raud&sv=2015-04-05&tn=testowa&sig=UEbinBTgpEGwy1ts0hwmSfELM‌​1lYCBItLHgWpWSgvE4%3D";
        CloudTable table = new CloudTable(new Uri(connectionString));
        bool exists = await table.ExistsAsync();
        if (exists)
            System.Console.WriteLine("Exists");
        else
            System.Console.WriteLine("Doesnt exist");