microsoft / microsoft/aspire

CosmosDB Emulator Hangs When Connecting via Connection String

Open
#5,364 10 comments 1 reaction 0 assignees View on GitHub
area-integrations azure azure-cosmosdb
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

**Description:**
I have been using Aspire to locally test a service with Azure CosmosDB. Following the tutorial [here](https://learn.microsoft.com/en-us/dotnet/aspire/database/azure-cosmos-db-component?tabs=dotnet-cli) and using `RunAsEmulator()`.

**Issue:**
My service uses IHostBuilder which is incompatible with the `AddAzureCosmosClient` method from `Aspire.Microsoft.Azure.Client`. Therefore, I use a callback to pass the connection string to the CosmosDB Emulator as an environment variable to my service. This is the same connection string that Aspire generates and uses in 'AddAzureCosmosClient'.

I have been able to successfully connect to the Storage Emulators and EventHub Emulator the same way.

**Symptoms:**
- I can connect to the instance and perform some functions, such as reading account properties.
- **When attempting to create a database (`CreateDatabaseAsync()`, `CreateDatabaseIfNotExistsAsync()`, etc.), the application hangs (no exception thrown, no timeout).**

**Attempts to Resolve:**
- Tried using these methods synchronously with `GetAwaiter().GetResult()`.
- Tried using these methods asynchronously with `await`.
- Attempted to use the Azure CosmosDB Emulator directly (without Aspire) which works with the same code.

This is the relevant part of my AppHost Program.cs file:

![image](https://github.com/user-attachments/assets/72b53efa-b8f4-4854-a1d9-8b7700c57d2a)

This is the relevant part of my Service Program.cs file:

![image](https://github.com/user-attachments/assets/5b9a61e9-5639-43a4-bd64-2fd043597b50)

### Expected Behavior

The application should create the database without hanging.

### Steps To Reproduce

I have used the 'CosmosEndToEnd' example from the Aspire repository and modified the 'Program.cs' file of the API project. The only change I made was to create the Cosmos Client manually with the connection string within an endpoint mapping, instead of adding it in the builder configuration with builder.AddAzureCosmosClient("cosmos"). However, the application hangs when attempting to create a database this way.

Here are the relevant files of code:

[CosmosEndToEnd.ApiService](https://github.com/sami-sharma/CosmosDbEmulatorTest/blob/main/playground/CosmosEndToEnd/CosmosEndToEnd.ApiService/Program.cs)

[CosmosEndToEnd.AppHost](https://github.com/sami-sharma/CosmosDbEmulatorTest/blob/main/playground/CosmosEndToEnd/CosmosEndToEnd.AppHost/Program.cs)

This is where I have forked the Aspire code: https://github.com/sami-sharma/CosmosDbEmulatorTest. Please view the CosmosEndToEnd folder within playground.

### Exceptions (if any)

_No response_

### .NET Version info

Aspire: 8.1.1-preview.1.24373

.NET SDK : 8.0.400

### Anything else?

I have used the EventHub Emulator and Storage emulators connecting via connection strings manually and have not faced issues connecting.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.