Blazor app with cosmos sdk goes non responsive
See original GitHub issueWe are continuously addressing and improving the SDK, if possible, make sure the problem persist in the latest SDK version.
Describe the bug A clear and concise description of what the bug is.
To Reproduce
- Following this url for blazor setup : https://docs.microsoft.com/en-us/aspnet/core/blazor/get-started?view=aspnetcore-3.0&tabs=visual-studio
- Create a Blazor Client project
- Install nuget package - Install-Package Microsoft.Azure.Cosmos -Version 3.2.0
- Run following code snippet in any btn handler click or may be startup
var client = new CosmosClient(Endpoint, key);
- Blazor app will freeze and ultimately error out.
Expected behavior Should be able to create a cosmos client and then do other operation afterwards
Actual behavior Blazor app will freezes and nothing happens, have stop the debugging session
Environment summary .NET Core SDK (reflecting any global.json): Version: 3.0.100-rc1-014190 Commit: c4d43f672d
Runtime Environment: OS Name: Windows OS Version: 10.0.17134 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\3.0.100-rc1-014190\
Host (useful for support): Version: 3.0.0-rc1-19456-20 Commit: 8f5d7b1ba4
.NET Core SDKs installed: 2.0.2 [C:\Program Files\dotnet\sdk] 2.1.202 [C:\Program Files\dotnet\sdk] 2.1.502 [C:\Program Files\dotnet\sdk] 2.1.508 [C:\Program Files\dotnet\sdk] 2.1.509 [C:\Program Files\dotnet\sdk] 3.0.100-rc1-014190 [C:\Program Files\dotnet\sdk]
.NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.0.0-rc1.19457.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.0.0-rc1-19456-20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.0.0-rc1-19456-20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Additional context Add any other context about the problem here (for example, complete stack traces or logs).
Issue Analytics
- State:
- Created 4 years ago
- Comments:13 (6 by maintainers)
Top GitHub Comments
If you need a service that generates instances of the CosmosClient based on some condition, then create such a service and register it as Singleton during Startup. Our recommendation is that instances should be kept and reused (https://docs.microsoft.com/en-us/azure/cosmos-db/performance-tips#sdk-usage).
Even if you wrap the CosmosClient in a using, which will Dispose of the memory, the other problem you have is that connections are not released immediately (https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/), so if you do that many times in a small slice of time, your application might end up consuming a lot of connections (that are still in TIME_WAIT) and depending on where you run the App (some hosting environments have limits to the # of connections), you might face socket starvation.
If creation CosmosClient instances is based on the click of a button, you need to make that process thread-safe and possibly use contention (for example, ConcurrentDictionary or locks), otherwise, bringing your app down is just a matter of a user clicking a button constantly.
The approach for a Web API would be the same, I have recently answered a similar question here with some sample code 😄
https://stackoverflow.com/a/58774933/5641598