【问题标题】:The type initializer for 'Microsoft.WindowsAzure.Storage.CloudStorageAccount' threw an exception error in Xamarin.forms“Microsoft.WindowsAzure.Storage.CloudStorageAccount”的类型初始化程序在 Xamarin.forms 中引发异常错误
【发布时间】:2018-01-26 04:51:24
【问题描述】:

我正在开发 Xamarin 提供的 AzureStorage 云解决方案。 https://developer.xamarin.com/guides/xamarin-forms/cloud-services/storage/azure-storage/

这就是我在 GetContainer() 方法中得到的。

类型初始化器 'Microsoft.WindowsAzure.Storage.CloudStorageAccount' 抛出了一个 异常

我在示例项目中应用了我的连接字符串并且它有效,但在我自己的项目中却没有。

有人遇到过这个问题吗?请帮我。

提前致谢。

错误信息:

System.TypeInitializationException: The type initializer for 'Microsoft.WindowsAzure.Storage.CloudStorageAccount' threw an exception. ---> System.NotImplementedException: The method or operation is not implemented.
 at Microsoft.WindowsAzure.Storage.CloudStorageAccount.Setting (System.String name, System.String[] validValues) [0x00000] in <667a5fa37f124e50ab7a68ecb3437b13>:0
 at Microsoft.WindowsAzure.Storage.CloudStorageAccount..cctor () [0x00000] in <667a5fa37f124e50ab7a68ecb3437b13>:0
 --- End of inner exception stack trace ---
 at MyTenantWorld.AzureStorage.GetContainer (MyTenantWorld.ContainerType containerType) [0x00020] in <9816076ee17d42efaf1050c5169a4310>:0
 at MyTenantWorld.AzureStorage+<UploadFileAsync>d__3.MoveNext () [0x0001a] in <9816076ee17d42efaf1050c5169a4310>:0
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3fd174ff54b146228c505f23cf75ce71>:0
 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <3fd174ff54b146228c505f23cf75ce71>:0
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <3fd174ff54b146228c505f23cf75ce71>:0
 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <3fd174ff54b146228c505f23cf75ce71>:0
 at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
 at MyTenantWorld.SettingsPage+<SaveData>d__20.MoveNext () [0x00309] in <9816076ee17d42efaf1050c5169a4310>:0
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3fd174ff54b146228c505f23cf75ce71>:0
 at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
 at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in <d855bac285f44dda8a0d8510b679b1e2>:0
 at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <d855bac285f44dda8a0d8510b679b1e2>:0
 at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in <d855bac285f44dda8a0d8510b679b1e2>:0
 at at (wrapper dynamic-method) System.Object:a4fed05a-cd7a-4756-85bb-5dd975374042 (intptr,intptr)

【问题讨论】:

  • 在不知道异常是什么的情况下,很难知道发生了什么。您可以添加完整的崩溃信息吗?例如单击 xamarin 为您提供的崩溃窗口中的复制按钮并粘贴到此处。对于它的价值,documentation for the constructor you're using 列出了它可以抛出的可能异常。
  • @BergQuester,我刚刚添加了错误消息,谢谢。
  • System.NotImplementedException,这里的方法似乎没有在 Mono 框架(用于 Xamarin)或您链接的库版本中实现。
  • @LasseV.Karlsen,你能澄清一下吗?

标签: c# xamarin xamarin.forms azure-storage azure-blob-storage


【解决方案1】:

AzureStorage.cs下定义的GetContainer方法如下:

static CloudBlobContainer GetContainer(ContainerType containerType)
{
     var account = CloudStorageAccount.Parse(Constants.StorageConnection);
     var client = account.CreateCloudBlobClient();
     return client.GetContainerReference(containerType.ToString().ToLower());
}

似乎是CloudStorageAccount.Parse抛出的错误,但是从BergQuester关于CloudStorageAccount.Parse的链接中,我们可以发现这个错误不是来自可预测的异常。

从您的堆栈跟踪中,我们可以发现错误是从CloudStorageAccount.csSetting(string name, params string[] validValues) 抛出的。我使用WindowsAzure.Storage 8.3.0 并在我的 BruceChen_Mobile.UWP 项目和 PCL 项目下对其进行了测试,它可以与我的 azure 存储连接字符串按预期工作。

我在示例项目中应用了我的连接字符串并且它有效,但在我自己的项目中却没有。

我建议您检查您的应用程序和xamarin-forms-samples 之间的包。或者您可以为我们提供一个可重现的示例项目来缩小此问题的范围。

【讨论】:

    【解决方案2】:

    我通过从我的本地项目(不仅仅是在 PCL 项目中)添加对 WindowsAzure.Storage nuget 包的引用解决了我的这个问题,因为它需要在这些引用中定义的一些本地实现。

    【讨论】:

    • 也许需要更多的细节来回答这个问题。
    • 没有太多要补充的。我遇到了与 OP 相同的错误,我通过在我的 android 本机和 iOS 本机项目中添加对 WindowsAzure.Storage 的 Nuget 引用来修复它。此错误可能还有其他原因,但没有正确的参考资料对我来说是造成它的原因。也许有人和我有同样的问题,在这种情况下,我希望阅读这篇文章可以为他们节省几个小时的挫败感。
    • 我错过了来自 android 项目的 WindowsAzure.Storage nugate 包,添加了一个并开始工作
    猜你喜欢
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2013-05-15
    • 2013-11-08
    相关资源
    最近更新 更多