microsoft / microsoft/aspire

Not able to get Azure Functions + Cosmos DB working when using emulator

Open
#9,831 7 comments 1 reaction 0 assignees View on GitHub
area-integrations azure azure-functions internal-customer-impact
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

I am fairly new to Aspire and trying to leverage Azure Functions + Cosmos DB.
I have the following AppHost.cs code...

```
var builder = DistributedApplication.CreateBuilder(args);

// Cosmos DB
var cosmos = builder.AddAzureCosmosDB("cosmos")
.RunAsPreviewEmulator(emulator =>
{
emulator.WithDataExplorer();
});
var database = cosmos.AddCosmosDatabase("database");
var container = database.AddContainer("container", "/id");

// Functions Storage
var hostStorage = builder.AddAzureStorage("hoststorage").RunAsEmulator();

// Add Azure Functions project
builder.AddAzureFunctionsProject("function")
.WithHostStorage(hostStorage)
.WithReference(container);

builder.Build().Run();
```

and the following Program.cs code in my Functions app project...

```
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = FunctionsApplication.CreateBuilder(args);

builder.AddServiceDefaults();

builder.AddAzureCosmosContainer("container");

builder.ConfigureFunctionsWebApplication();

builder.Services
.AddApplicationInsightsTelemetryWorkerService()
.ConfigureFunctionsApplicationInsights();

builder.Build().Run();

```

~Note: I have also tried just using builder.AddAzureCosmosClient in the above Program.cs and get similar error.~
Update: CosmosClient does passthough - it's database and container that does not.

![Image](https://github.com/user-attachments/assets/3b6b6dd9-2d6d-4c6c-9b10-5f28c12a61c3)

Everything starts up fine and everything looks healthy in the AppHost, but as soon as I run the httptrigger function I get the following error message...

`An exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'The connection string 'container' does not exist or is missing the container name or database name.'`

This seems to match the Aspire docs for Cosmos with Web API but I'm clearly missing something on the function app side?!... do I need to define something on MicrosoftAzureCosmosSettings and pass into AddAzureCosmosContainer?

Please note that this is all on Mac (ARM) with Visual Studio Code and Docker Desktop. Also note that I'm using the Linux-based emulator for cosmos rather than the normal .RunAsEmulator as it didn't seem to be working for me? - although I don't know if this is emulator related as it probably doesn't work when deployed to Azure either).

### Expected Behavior

I simply expect the function httptrigger to run and not throw an exception.

### Steps To Reproduce

Create Aspire project and add a basic HttpTrigger function app and use code above.

### Exceptions (if any)

An exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'The connection string 'container' does not exist or is missing the container name or database name.'

### .NET Version info

.NET SDK:
Version: 9.0.300
Commit: 15606fe0a8
Workload version: 9.0.300-manifests.19ff842e
MSBuild version: 17.14.5+edd3bbf37

Runtime Environment:
OS Name: Mac OS X
OS Version: 15.5
OS Platform: Darwin
RID: osx-arm64
Base Path: /usr/local/share/dotnet/sdk/9.0.300/

.NET workloads installed:
[maui-ios]
Installation Source: SDK 9.0.300
Manifest Version: 9.0.51/9.0.100
Manifest Path: /usr/local/share/dotnet/sdk-manifests/9.0.100/microsoft.net.sdk.maui/9.0.51/WorkloadManifest.json
Install Type: FileBased

[maui-android]
Installation Source: SDK 9.0.300
Manifest Version: 9.0.51/9.0.100
Manifest Path: /usr/local/share/dotnet/sdk-manifests/9.0.100/microsoft.net.sdk.maui/9.0.51/WorkloadManifest.json
Install Type: FileBased

[maui]
Installation Source: SDK 9.0.300
Manifest Version: 9.0.51/9.0.100
Manifest Path: /usr/local/share/dotnet/sdk-manifests/9.0.100/microsoft.net.sdk.maui/9.0.51/WorkloadManifest.json
Install Type: FileBased

Configured to use loose manifests when installing new manifests.

Host:
Version: 10.0.0-preview.3.25171.5
Architecture: arm64
Commit: 0a33e18a0b

.NET SDKs installed:
9.0.300 [/usr/local/share/dotnet/sdk]

### Anything else?

Apple M1 Max - MacOS 15.5
Docker Desktop 4.41.2
.NET 9.0.300
ms-azuretools.vscode-azurefunctions 1.17.3
ms-dotnettools.csdevkit 1.20.35

AppHost Project Packages:
```

```

Function Project Packages:
```

```

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.