Xamarin App using Azure Cosmos DB Won't Connect on Device

Mike Mendelow 1 Reputation point
2021-11-07T15:12:43.83+00:00

Our very simple Xamarin App that connects to an Azure Cosmos DB and it works fine in Debug and Release configs on the emulators. When we deploy to a device (or to TestFlight) it crashes on the attempt to connect to the Azure Cosmos DB. The error is a simple "Method not found" which is really strange.

The app uses Microsoft.Azure.CosmosDB NuGet package v 3.22.1

This is the simple code block on initialization

    public async Task ConnectCloudDB()
    {
        try
        {
            cosmosClient = new CosmosClient(AppConfig.EndpointUri, AppConfig.PrimaryKey, new CosmosClientOptions() { ApplicationName = "MySimpleApp" });
            database = await cosmosClient.CreateDatabaseIfNotExistsAsync(databaseId);
            container = await database.CreateContainerIfNotExistsAsync(containerId, "/partitionKey");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

The error occurs on the first line of the function, new CosmosClient

This is the full error text

The error I get is {System.MissingMethodException: Method not found: Microsoft.Azure.Cosmos.Core.Utf8.Utf8Span Microsoft.Azure.Cosmos.Core.Utf8.Utf8Span.UnsafeFromUtf8BytesNoValidation(System.ReadOnlySpan`1<byte>)
at Microsoft.Azure.Cosmos.Tracing.TraceData.ClientConfigurationTraceDatum.GetSerializedDatum () <0x12d1809a8 + 0x00028> in <52e81403ebce4034958e1bd4a74f9405>:0
at Microsoft.Azure.Cosmos.Tracing.TraceData.ClientConfigurationTraceDatum..ctor (Microsoft.Azure.Cosmos.CosmosClientContext cosmosClientContext, System.DateTime startTime) <0x12ccc3798 + 0x00166> in <52e81403ebce4034958e1bd4a74f9405>:0
at Microsoft.Azure.Cosmos.CosmosClient..ctor (System.String accountEndpoint, Microsoft.Azure.Cosmos.AuthorizationTokenProvider authorizationTokenProvider, Microsoft.Azure.Cosmos.CosmosClientOptions clientOptions) <0x12cb298f8 + 0x000a0> in <52e81403ebce4034958e1bd4a74f9405>:0
at Microsoft.Azure.Cosmos.CosmosClient..ctor (System.String accountEndpoint, System.String authKeyOrResourceToken, Microsoft.Azure.Cosmos.CosmosClientOptions clientOptions) <0x12cb1e628 + 0x00014> in <52e81403ebce4034958e1bd4a74f9405>:0
at UIPlayground.MainPage.ConnectCloudDB () [0x00027] in C:\Users\drewm\source\repos\UIPlayground\UIPlayground\UIPlayground\MainPage.xaml.cs:117 }

Again, it is very strange since it works perfectly on emulators, but says "method not found" when on the device itself. Any help would be greatly appreciated!

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,380 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,892 questions
{count} votes

1 answer

Sort by: Most helpful
  1. cristopheB 551 Reputation points
    2021-11-09T15:29:38.68+00:00

    Hello,
    i'm not expert but do you add the permission INTERNET in the manifest ?
    I suppose yes but you need to ACCEPT on run time

    https://www.geeksforgeeks.org/android-how-to-request-permissions-in-android-application/#:~:text=Run%2DTime%20Permissions%3A%20If%20the,the%20app%20requests%20permission%20again.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.