Azure / azure-cosmos-dotnet-v3

.NET SDK for Azure Cosmos DB for the core SQL API
MIT License
737 stars 493 forks source link

Mobile: Xamarin App using Microsoft.Azure.CosmosDB NuGet v 3.22.1 works on Emulator, crashes on Devices "Method Not Found" #2859

Open mmendelow-eqr opened 2 years ago

mmendelow-eqr commented 2 years ago

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"

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

{System.MissingMethodException: Method not found: Microsoft.Azure.Cosmos.Core.Utf8.Utf8Span Microsoft.Azure.Cosmos.Core.Utf8.Utf8Span.UnsafeFromUtf8BytesNoValidation(System.ReadOnlySpan`1) 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 }

Environment summary SDK Version: Microsoft.Azure.CosmosDB NuGet v 3.22.1 OS Version (e.g. Windows, Linux, MacOSX); Windows

ealsur commented 2 years ago

Do the deployed bits contain all the Nuget DLLs?

j82w commented 2 years ago

There is security concerns with using the Cosmos DB SDK directly on a user's device. If they get access to the Cosmos DB account key it would be a big problem. The recommended way to use Cosmos DB with Xamarin is to have a middle tier service. The middle tier service would talk to Cosmos DB. The xamarin app would talk to the middle tier service. This way the middle tier service can throttle, cache, validate, etc.. when talking to Cosmos DB.

https://docs.microsoft.com/azure/architecture/solution-ideas/articles/gaming-using-cosmos-db

mmendelow-eqr commented 2 years ago

@ealsur I would assume so? The system build and deploys the entire bundle to the device. Is there some way I could verify?

@j82w Yes I have read about this and am planning to try and tokenize in order to mitigate security concerns, or add an Azure function. I'd still very much like to get this iteration working as the app needs to be distributed for algorithm testing. Any suggestions would be greatly appreciated!

ealsur commented 2 years ago

The folder where the SDK is deployed to needs to contain all DLLs that come in the Nuget package:

image

mmendelow-eqr commented 2 years ago

@ealsur The system creates a .ipa for deployment to Apple devices. I unzipped it to make sure and yes, the DLLs all seem to be there:

image (5)

mmendelow-eqr commented 2 years ago

@ealsur Any other ideas I can try? I am at a loss.....

ealsur commented 2 years ago

The error apparently is saying that the method: Microsoft.Azure.Cosmos.Core.Utf8.Utf8Span Microsoft.Azure.Cosmos.Core.Utf8.Utf8Span.UnsafeFromUtf8BytesNoValidation, is not found.

Which is part of the Microsoft.Azure.Cosmos.Core.dll.

These type of errors are runtime errors that mean that the DLL was present at compile time but on runtime, it cannot find the DLL that is supposed to include them. I don't know how Xamarin or in this case the .ipa deployment works with path resolution, but it sounds like it is not able to either load or find the DLL on runtime.

mmendelow-eqr commented 2 years ago

@ealsur Agree. Really strange right? Are you aware of any build settings that might affect this behavior? I just don't understand why it works perfectly when deployed to the emulator but not when deployed to the device - I can't figure out what would be different about the two deployments

ealsur commented 2 years ago

Sadly I have no experience working with Xamarin to know which are the different build or deploy options available.

mmendelow-eqr commented 2 years ago

@ealsur Understood - thanks for looking.

Anyone else have any ideas? Any other info I can gather to help diagnose this?