question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cosmos client fails in Blazor WebAssembly app

See original GitHub issue

Describe the bug When instantiating an instance of CosmosClient in a Blazor WebAssembly app, the constructor throws an exception:

System.Threading.SynchronizationLockException: Cannot wait on monitors on this runtime.

To Reproduce Create a new Blazor WebAssembly project. I used the latest preview (3.2.0 preview 5). Install Microsoft.Azure.Cosmos (I used 3.7.0-preview). In Program.Main add the following code anywhere:

var client = new CosmosClient(
   "[CosmosEndpoint]",
  "[CosmosKey]",
  new CosmosClientOptions()
  {
     ConnectionMode = ConnectionMode.Gateway,
     LimitToEndpoint = true
   });

Replace the endpoint and key with an actual endpoint/key. The error will throw immediately.

Expected behavior I would expect to be able to use the Cosmos client SDK from a Blazor client in gateway mode as it is simply HTTP.

Actual behavior The client throws, likely due to using an invalid HttpClient instead of the one provided by Blazor via DI.

Environment summary SDK Version: 3.7.0-preview OS Version: Windows 10 Visual Studio Version: 16.6.0 preview 4.0 .NET Core Version: 5.0.100-preview.3.20216.6 Blazor Version: 3.2.0-preview5.20216.8

Additional context Full call stack from exception:

System.Threading.SynchronizationLockException: Cannot wait on monitors on this runtime.
blazor.webassembly.js:1 at (wrapper managed-to-native) System.Threading.Monitor.Monitor_wait(object,int)
blazor.webassembly.js:1 at System.Threading.Monitor.ObjWait (System.Boolean exitContext, System.Int32 millisecondsTimeout, System.Object obj) <0x2fd5d88 + 0x00046> in <filename unknown>:0 
blazor.webassembly.js:1 at System.Threading.Monitor.Wait (System.Object obj, System.Int32 millisecondsTimeout, System.Boolean exitContext) <0x2fd5aa8 + 0x00022> in <filename unknown>:0 
blazor.webassembly.js:1 at System.Threading.Monitor.Wait (System.Object obj, System.Int32 millisecondsTimeout) <0x2fd59e0 + 0x0000a> in <filename unknown>:0 
blazor.webassembly.js:1 at System.Threading.ManualResetEventSlim.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) <0x2fd5060 + 0x001be> in <filename unknown>:0 
blazor.webassembly.js:1 at System.Threading.Tasks.Task.SpinThenBlockingWait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) <0x2fd15f8 + 0x00072> in <filename unknown>:0 
blazor.webassembly.js:1 at System.Threading.Tasks.Task.InternalWait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) <0x2fd0f68 + 0x000ca> in <filename unknown>:0 
blazor.webassembly.js:1 at System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) <0x2fad5a0 + 0x00036> in <filename unknown>:0 
blazor.webassembly.js:1 at System.Threading.Tasks.Task.Wait () <0x2fad2e0 + 0x00018> in <filename unknown>:0 
blazor.webassembly.js:1 at System.Net.ServicePointScheduler+AsyncManualResetEvent.Set () <0x2facb90 + 0x0007a> in <filename unknown>:0 
blazor.webassembly.js:1 at System.Net.ServicePointScheduler.Run () <0x2fc0760 + 0x00034> in <filename unknown>:0 
blazor.webassembly.js:1 at System.Net.ServicePointScheduler.set_ConnectionLimit (System.Int32 value) <0x2fc0600 + 0x00030> in <filename unknown>:0 
blazor.webassembly.js:1 at System.Net.ServicePoint.set_ConnectionLimit (System.Int32 value) <0x2fc0490 + 0x00024> in <filename unknown>:0 
blazor.webassembly.js:1 at Microsoft.Azure.Cosmos.DocumentClient.Initialize (System.Uri serviceEndpoint, Microsoft.Azure.Cosmos.ConnectionPolicy connectionPolicy, System.Nullable`1[T] desiredConsistencyLevel, System.Net.Http.HttpMessageHandler handler, Microsoft.Azure.Documents.ISessionContainer sessionContainer, System.Nullable`1[T] enableCpuMonitor, Microsoft.Azure.Documents.IStoreClientFactory storeClientFactory) <0x2f8f418 + 0x00832> in <filename unknown>:0 
blazor.webassembly.js:1 at Microsoft.Azure.Cosmos.DocumentClient..ctor (System.Uri serviceEndpoint, System.String authKeyOrResourceToken, System.EventHandler`1[TEventArgs] sendingRequestEventArgs, Microsoft.Azure.Cosmos.ConnectionPolicy connectionPolicy, System.Nullable`1[T] desiredConsistencyLevel, Newtonsoft.Json.JsonSerializerSettings serializerSettings, Microsoft.Azure.Documents.ApiType apitype, System.EventHandler`1[TEventArgs] receivedResponseEventArgs, System.Net.Http.HttpMessageHandler handler, Microsoft.Azure.Documents.ISessionContainer sessionContainer, System.Nullable`1[T] enableCpuMonitor, System.Func`2[T,TResult] transportClientHandlerFactory, Microsoft.Azure.Documents.IStoreClientFactory storeClientFactory) <0x2f6d870 + 0x0015a> in <filename unknown>:0 
blazor.webassembly.js:1 at Microsoft.Azure.Cosmos.CosmosClient..ctor (System.String accountEndpoint, System.String authKeyOrResourceToken, Microsoft.Azure.Cosmos.CosmosClientOptions clientOptions) <0x2f0df48 + 0x00166> in <filename unknown>:0 
blazor.webassembly.js:1 at BlazorApp1.Program.Main (System.String[] args) [0x000cb] in c:\temp\BlazorApp1\BlazorApp1\Program.cs:28 
blazor.webassembly.js:1 at Microsoft.AspNetCore.Components.WebAssembly.Hosting.EntrypointInvoker.InvokeEntrypoint (System.String assemblyName, System.String[] args, Microsoft.AspNetCore.Components.WebAssembly.Hosting.SatelliteResourcesLoader satelliteResourcesLoader) <0x2c25360 + 0x001ec> in <filename unknown>:0 

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:12
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
ealsurcommented, May 1, 2020

Required code changes were merged, will be available in the next release.

2reactions
JeremyLiknesscommented, Apr 25, 2020

@j82w Yes, Blazor Server works fine. It’s Blazor WebAssembly that has the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Blazor WASM Static Web App communicate directly with ...
It seems like communicating directly with cosmos db from the browser would be ideal since it follows the whole faster serverless web approach ......
Read more >
EF Core and Cosmos DB with Blazor WebAssembly
With Blazor WebAssembly it is possible to use EF Core to access Cosmos DB directly from the browser. This post explores why, how,...
Read more >
Cryptographyc errors while saving data into Azure-Table- ...
I am developing a BLAZOR application (Core 5.0), but this errors keeps coming out! ... First I have use the COSMOS DB API...
Read more >
EF Core and Cosmos DB with Blazor WebAssembly
This post explores the idea of using connecting to Cosmos DB entirely from the browser using Entity Framework Core (EF Core) in a...
Read more >
Azure AD Secured Serverless Cosmos DB from Blazor ...
In this case, the client app is the Blazor WebAssembly app. To allow it to work with the functions app, you need to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found