为啥我的 Xamarin Forms iOS 应用程序在使用 Microsoft Azure 移动服务时崩溃

Posted

技术标签:

【中文标题】为啥我的 Xamarin Forms iOS 应用程序在使用 Microsoft Azure 移动服务时崩溃【英文标题】:Why is my Xamarin Forms iOS app crashing when using Microsoft Azure Mobile Services为什么我的 Xamarin Forms iOS 应用程序在使用 Microsoft Azure 移动服务时崩溃 【发布时间】:2017-08-03 16:04:38 【问题描述】:

我的 Xamarin Forms 应用程序遇到了一个奇怪的问题,我似乎无法解决,我似乎在绕圈子。它似乎专门与我的 ios 项目相关联,它围绕着程序集引用 Microsoft.WindowsAzure.Mobile。当我在 iPhone 模拟器上运行我的应用程序,然后尝试查看从 Azure 获取数据的任何部分时,会触发以下消息。

System.InvalidOperationException:Microsoft Azure 移动服务 未找到当前平台的程序集。确保 当前项目同时引用了 Microsoft.WindowsAzure.Mobile 和 以下特定于平台的程序集: Microsoft.WindowsAzure.Mobile.Ext.

我觉得这很奇怪,因为我安装了我认为所有正确的参考资料:

使用了整个解决方案Xamarin.Forms v2.3.4.247

MyApp(便携式)

WindowsAzure.MobileServices 1.3.2 WindowsAzure.MobileService.SQLiteStore 1.0.1

MyApp.iOS

Microsoft.Azure.Mobile.Client 4.0.1 Microsoft.Azure.Mobile.Client.SQLiteStore 4.0.1

当我安装这些 nuget 包时,我认为 iOS 项目的 app.config 可能已损坏或丢失了一些信息,所以我快速浏览了一下,一切看起来都应该如此。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.0.0" newVersion="1.5.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="PInvoke.BCrypt" publicKeyToken="9e300f9f87f04a7a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-0.5.0.0" newVersion="0.5.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Validation" publicKeyToken="2fc06f0d701809a7" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.4.0.0" newVersion="2.4.0.0" />
      </dependentAssembly>
     <dependentAssembly>
        <assemblyIdentity name="Microsoft.WindowsAzure.Mobile" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.29.0" newVersion="4.2.29.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

如果需要,我可以提供更多代码,但我希望将问题定位于我所看到的内容以及报告问题所在的位置。有谁知道这可能是什么原因或我需要做些什么来解决它?可能是导致问题的版本,我应该考虑回滚吗?

【问题讨论】:

【参考方案1】:

AFAIK,WindowsAzure.MobileServices 现已弃用,它曾经与移动服务(如 your-app-name.azure-mobile.net 之类的域)一起使用。而Microsoft.Azure.Mobile.Client 用于处理移动应用(应用服务,域如 your-app-name.azurewebsites.net)。

有谁知道这可能是什么原因或我需要做些什么来解决它?可能是导致问题的版本,我应该考虑回滚吗?

我注意到您的 iOS 项目的 app.config 有 dependentAssembly about Microsoft.WindowsAzure.Mobile 和 newVersion 1.3.0.0。

根据我的理解,您已经安装了 WindowsAzure.MobileServices 和 Microsoft.Azure.Mobile.Client 包,我建议您使用相同的包来缩小此问题。另外,在使用MobileServiceClient之前,您需要通过以下代码初始化azure mobile:

Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();

此外,我检查了已安装 Microsoft.Azure.Mobile.Client 3.1.0 的 xamarin.forms 应用程序,它可以按预期工作。这是关于azure-mobile-apps-quickstarts的git示例,你可以参考一下。 另外,这里有一个类似的git问题,你可以参考here。

【讨论】:

我降级了我的Microsoft.Azure.Mobile.Client,以便我可以将它安装在我的共享项目中,这似乎可以工作。这将不得不让我度过难关,直到 Xamarin 更新他们的文档以反映这些已弃用的库。

以上是关于为啥我的 Xamarin Forms iOS 应用程序在使用 Microsoft Azure 移动服务时崩溃的主要内容,如果未能解决你的问题,请参考以下文章