You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin.Forms部署iOS后Microsoft.Azure.Cosmos异常求助

问题描述

我正在开发Xamarin.Forms应用,使用Microsoft.Azure.Cosmos数据库进行数据存储与读取。在iOS和Android模拟器的Debug及Release模式下,应用均可正常访问并读写Cosmos DB数据。但将应用部署至Google Play和App Store Connect后,Android端仍可正常访问数据库,iOS端读取数据时却时常崩溃或无响应。

我的问题:为何将应用部署至App Store Connect进行测试后,iOS端的Cosmos DB NuGet包无法正常工作?

捕获到的异常

Lealtd.MyPages.MyAuthentification.LeafProfileParameter.MyParamPage() in <aa0ef9de4404408f83484774998ac15a#c3f844596a550d3270e6f8cf3e1cb01a>
Lealtd.MyPages.MyAuthentification.LeafProfileParameter..ctor() in <aa0ef9de4404408f83484774998ac15a#c3f844596a550d3270e6f8cf3e1cb01a>
Lealtd.MyPages.MyAuthentification.LeafInPage.OnUserLeafIn() in <aa0ef9de4404408f83484774998ac15a#c3f844596a550d3270e6f8cf3e1cb01a>
System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0(System.Object state) in <66c50f4174694147beb2fbedff89d188#c3f844596a550d3270e6f8cf3e1cb01a>
Foundation.NSAsyncSynchronizationContextDispatcher.Apply() in <c07d1d2ff2fb499fb8eca9a33aeb3c2e#c3f844596a550d3270e6f8cf3e1cb01a>
(wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
UIKit.UIApplication.Main(System.String[] args, System.Type principalClass, System.Type delegateClass) in <c07d1d2ff2fb499fb8eca9a33aeb3c2e#c3f844596a550d3270e6f8cf3e1cb01a>
Lealtd.iOS.Application.Main(System.String[] args) in <3eed6dbecd10466db73945963e82109c#c3f844596a550d3270e6f8cf3e1cb01a>

堆栈跟踪信息

System.Runtime.Remoting.Messaging.CallContext.LogicalGetData(System.String ) in <66c50f4174694147beb2fbedff89d188#c3f844596a550d3270e6f8cf3e1cb01a>
System.Diagnostics.CorrelationManager.get_ActivityId() in <9346215e3717414fb21df612b5dd4279#c3f844596a550d3270e6f8cf3e1cb01a>
Microsoft.Azure.Cosmos.ActivityScope..ctor(System.Guid activityId) in <b59e9dd3c16f4591867b773a543b41c5#c3f844596a550d3270e6f8cf3e1cb01a>
Microsoft.Azure.Cosmos.ClientContextCore.RunWithDiagnosticsHelperAsync[TResult](Microsoft.Azure.Cosmos.Tracing.ITrace trace, System.Func`2[T,TResult] task, System.Func`2[T,TResult] openTelemetry, System.String operationName, Microsoft.Azure.Cosmos.RequestOptions requestOptions) in <b59e9dd3c16f4591867b773a543b41c5#c3f844596a550d3270e6f8cf3e1cb01a>
Microsoft.Azure.Cosmos.ClientContextCore+<>c__DisplayClass31_0`1[TResult].<OperationHelperWithRootTraceWithSynchronizationContextAsync>b__0() in <b59e9dd3c16f4591867b773a543b41c5#c3f844596a550d3270e6f8cf3e1cb01a>
Lealtd.MyPages.WordPage.MyWordPage.ExecuteRefreshCommandAsyncPulled() in <aa0ef9de4404408f83484774998ac15a#c3f844596a550d3270e6f8cf3e1cb01a>
System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0(System.Object state) in <66c50f4174694147beb2fbedff89d188#c3f844596a550d3270e6f8cf3e1cb01a>
Foundation.NSAsyncSynchronizationContextDispatcher.Apply() in <c07d1d2ff2fb499fb8eca9a33aeb3c2e#c3f844596a550d3270e6f8cf3e1cb01a>
(wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
UIKit.UIApplication.Main(System.String[] args, System.Type principalClass, System.Type delegateClass) in <c07d1d2ff2fb499fb8eca9a33aeb3c2e#c3f844596a550d3270e6f8cf3e1cb01a>
Lealtd.iOS.Application.Main(System.String[] args) in <3eed6dbecd10466db73945963e82109c#c3f844596a550d3270e6f8cf3e1cb01a>

尝试的解决方法及遇到的新错误

曾尝试将Linker behaviour设置为“don't link”来解决Cosmos DB问题,但设置后出现版本冲突错误:

Found conflicts between different versions of "System.Numerics.Vectors" that could not be resolved.
There was a conflict between "System.Numerics.Vectors, Version=2.0.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" and "System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
    "System.Numerics.Vectors, Version=2.0.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was chosen because it was primary and "System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was not.
    References which depend on "System.Numerics.Vectors, Version=2.0.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" [/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.Numerics.Vectors.dll].
        /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.Numerics.Vectors.dll
          Project file item includes which caused reference "/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.Numerics.Vectors.dll".
            System.Numerics.Vectors
    References which depend on "System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" [].
        /Users/x/.nuget/packages/microsoft.azure.cosmos/3.31.1/lib/netstandard2.0/Microsoft.Azure.Cosmos.Client.dll
          Project file item includes which caused reference "/Users/x/.nuget/packages/microsoft.azure.cosmos/3.31.1/lib/netstandard2.0/Microsoft.Azure.Cosmos.Client.dll".
            /Users/x/.nuget/packages/microsoft.azure.cosmos/3.31.1/lib/netstandard2.0/Microsoft.Azure.Cosmos.Client.dll
            /Users/x/Documents/Entreprise /MyApps/Leaf/lealtd/lealtd/bin/Release/netstandard2.0/lealtd.dll
        /Users/x/.nuget/packages/microsoft.azure.cosmos/3.31.1/lib/netstandard2.0/Microsoft.Azure.Cosmos.Core.dll
          Project file item includes which caused reference "/Users/x/.nuget/packages/microsoft.azure.cosmos/3.31.1/lib/netstandard2.0/Microsoft.Azure.Cosmos.Core.dll".
            /Users/x/.nuget/packages/microsoft.azure.cosmos/3.31.1/lib/netstandard2.0/Microsoft.Azure.Cosmos.Core.dll
            /Users/x/.nuget/packages/microsoft.azure.cosmos/3.31.1/lib/netstandard2.0/Microsoft.Azure.Cosmos.Client.dll
            /Users/x/Documents/Entreprise /MyApps/Leaf/lealtd/lealtd/bin/Release/netstandard2.0/lealtd.dll
            /Users/x/.nuget/packages/microsoft.azure.cosmos/3.31.1/lib/netstandard2.0/Microsoft.Azure.Cosmos.Serialization.HybridRow.dll
        /Users/x/.nuget/packages/system.text.json/4.6.0/lib/netstandard2.0/System.Text.Json.dll
          Project file item includes which caused reference "/Users/x/.nuget/packages/system.text.json/4.6.0/lib/netstandard2.0/System.Text.Json.dll".
            /Users/x/.nuget/packages/system.text.json/4.6.0/lib/netstandard2.0/System.Text.Json.dll
            /Users/x/.nuget/packages/azure.core/1.19.0/lib/netstandard2.0/Azure.Core.dll
            /Users/x/.nuget/packages/microsoft.azure.cosmos/3.31.1/lib/netstandard2.0/Microsoft.Azure.Cosmos.Client.dll
            /Users/x/Documents/Entreprise /MyApps/Leaf/lealtd/lealtd/bin/Release/netstandard2.0/lealtd.dll
            /Users/x/.nuget/packages/system.memory.data/1.0.2/lib/netstandard2.0/System.Memory.Data.dll
解决方案

1. 优化链接器配置避免Cosmos DB崩溃

直接设置“don't link”会引发版本冲突,建议改为仅链接SDK程序集,并在iOS项目的Linker.xml文件中添加Cosmos DB相关程序集的豁免配置:

<linker>
  <assembly fullname="Microsoft.Azure.Cosmos" />
  <assembly fullname="Microsoft.Azure.Cosmos.Core" />
  <assembly fullname="Microsoft.Azure.Cosmos.Client" />
  <assembly fullname="Azure.Core" />
</linker>

既保留链接优化,又防止Cosmos DB关键代码被误删。

2. 解决System.Numerics.Vectors版本冲突

在iOS项目的.csproj文件中添加绑定重定向配置,强制使用Xamarin.iOS提供的版本:

<PropertyGroup>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
<ItemGroup>
  <RuntimeHostConfigurationOption Include="System.Numerics.Vectors.Version" Value="2.0.5.0" />
</ItemGroup>

也可手动创建app.config文件添加绑定重定向:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.3.0" newVersion="2.0.5.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

3. 升级Cosmos DB NuGet包

当前使用的3.31.1版本存在iOS兼容性问题,建议升级到最新稳定版(如3.x系列最新版或4.x版本),新版本通常已修复链接器和依赖版本冲突问题。

4. 修正异步代码执行上下文

从堆栈跟踪看,异常发生在异步上下文切换环节,需确保Cosmos DB操作在后台线程执行,避免阻塞UI线程:

  • 使用ConfigureAwait(false)脱离UI同步上下文:
    var result = await cosmosContainer.GetItemQueryIterator<MyModel>(queryDefinition)
                                     .ReadNextAsync()
                                     .ConfigureAwait(false);
    

内容的提问来源于stack exchange,提问作者hugokis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 02:20:54