Cosmos client fails in Blazor WebAssembly app
See original GitHub issueDescribe 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:
- Created 3 years ago
- Reactions:12
- Comments:7 (3 by maintainers)
Top GitHub Comments
Required code changes were merged, will be available in the next release.
@j82w Yes, Blazor Server works fine. It’s Blazor WebAssembly that has the issue.