Skip to content

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

@user72356

Description

@user72356

This is in a Blazor WebAssembly app. Trying to load DynamoDB items with the object persistence model, I receive a SynchronizationLockException on LoadAsync(). However, the same operation works fine with the low-level API.

Expected Behavior

I should receive a "Book" object.

Current Behavior

Doing:

Book book = await dynamoDBContext.LoadAsync<Book>(1);

Throws:

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

See details below.

Steps to Reproduce (for bugs)

Create a test DynamoDB table called Book with an index called "Id" (number).

Create a new Blazor WebAssembly project in Visual Studio (client side template). This will create the default Blazor app for you. Then replace/create the following files. Don't forget to replace "XXX" by your own credentials to access the table of course.

Replace Program.cs with:

using Amazon;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.Model;
using Amazon.Runtime;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace AWSTest1
{
    class HttpInjectorFactory : HttpClientFactory
    {
        private readonly HttpClient _injectedClient;

        public HttpInjectorFactory(HttpClient injectedClient)
        {
            _injectedClient = injectedClient;
        }

        public override HttpClient CreateHttpClient(IClientConfig clientConfig)
        {
            return _injectedClient;
        }

        public override bool DisposeHttpClientsAfterUse(IClientConfig clientConfig)
        {
            return false;
        }

        public override bool UseSDKHttpClientCaching(IClientConfig clientConfig)
        {
            return false;
        }
    }

    public class Program
    {
        public static async Task Main(string[] args)
        {
            var builder = WebAssemblyHostBuilder.CreateDefault(args);
            builder.RootComponents.Add<App>("app");
            builder.Services.AddSingleton(new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
            var host = builder.Build();

            AWSConfigs.RegionEndpoint = RegionEndpoint.USEast2;
            AWSConfigs.HttpClientFactory = new HttpInjectorFactory(host.Services.GetRequiredService<HttpClient>());
            AWSConfigs.UseAlternateUserAgentHeader = true;

            // Request Book id 1 using low-level API, no error

            var dynamoDBClient = new AmazonDynamoDBClient("XXX", "XXX");

            Dictionary<string, AttributeValue> key = new Dictionary<string, AttributeValue>
            {
                { "Id", new AttributeValue { N = "1" } }
            };

            GetItemRequest request = new GetItemRequest
            {
                TableName = "Book",
                Key = key,
            };

            var result = await dynamoDBClient.GetItemAsync(request);

            // Request Book id 1 using object persistence model, throws SynchronizationLockException
            var dynamoDBContext = new DynamoDBContext(dynamoDBClient);
            Book book = await dynamoDBContext.LoadAsync<Book>(1);

            await host.RunAsync();
        }
    }
}

Create Book.cs:

using Amazon.DynamoDBv2.DataModel;

namespace AWSTest1
{
    [DynamoDBTable("Book")]
    public class Book
    {
        [DynamoDBHashKey]
        public int Id { get; set; }
    }
}

Your Environment

  • AWSSDK.Core version used: 3.3.106.17
  • Service assembly and version used: AWSSDK.DynamoDBv2 3.3.105.39
  • Operating System and version: Windows 10.0.18363
  • Visual Studio version: 16.6.0 Preview 5.0
  • Targeted .NET platform: .NET Standard 2.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions