-
Notifications
You must be signed in to change notification settings - Fork 548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AddAzureCosmosDB is not working with AddCosmosDbContext #7785
Comments
I have a working example here: |
Thanks a lot. I will try to find where the difference is. Maybe because I'm using a function app instead of a web app. |
We made a change in 9.1 that databases and containers act like SQL databases. They are child resources, which have names. var cosmos = builder.AddAzureCosmosDB("cosmos")
.RunAsPreviewEmulator(p => p.WithDataExplorer().WithDataVolume().WithLifetime(ContainerLifetime.Persistent))
.AddCosmosDatabase("db")
.AddContainer("users", "/id"); the Change this to: var cosmos = builder.AddAzureCosmosDB("cosmos")
.RunAsPreviewEmulator(p => p.WithDataExplorer().WithDataVolume().WithLifetime(ContainerLifetime.Persistent));
cosmos.AddCosmosDatabase("db")
.AddContainer("users", "/id"); |
I think the problem here is that we missed the CosmosDB EF integration in Support named configuration providers for Aspire resources (dotnet/aspire#5826). Looking at that issue all the EF integrations are still unchecked, but the issue is closed. @captainsafia - was this an oversight? |
I checked it and if I start your sample, I see this in the env section of the api: So the connection string starts with However, for my function project it gets populated as: and |
Looking into this, I think there are 2 problems here.
aspire/src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBResource.cs Lines 68 to 76 in 5830c5a
The EF client integration looks in Line 23 in 5830c5a
aspire/src/Components/Common/EntityFrameworkUtils.cs Lines 21 to 27 in 5830c5a
|
@mwiedemeyer - to workaround this problem, I was able to make your app work with changing the AppHost code to: var builder = DistributedApplication.CreateBuilder(args);
var nosecret = builder.AddParameter("NoSecret");
var apiKey = builder.AddParameter("OpenAIApiKey", secret: true);
var cosmos = builder.AddAzureCosmosDB("cosmos")
.RunAsPreviewEmulator(p => p.WithDataExplorer().WithDataVolume().WithLifetime(ContainerLifetime.Persistent));
cosmos.AddCosmosDatabase("db")
.AddContainer("users", "/id");
var sb = builder.AddAzureServiceBus("sb")
.RunAsEmulator(p => p.WithLifetime(ContainerLifetime.Persistent))
.AddServiceBusTopic("mytopic")
.AddServiceBusSubscription("sub1", "sub1");
builder.AddAzureFunctionsProject<Projects.AspireDemo>("func")
.WithReference(cosmos)
.WaitFor(cosmos)
.WithReference(sb)
.WaitFor(sb)
.WithEnvironment("NoSecret", nosecret)
.WithEnvironment("OpenAIApiKey", apiKey)
.WithEnvironment(context =>
{
if (cosmos.Resource.IsEmulator || cosmos.Resource.UseAccessKeyAuthentication)
{
context.EnvironmentVariables["Aspire__Microsoft__EntityFrameworkCore__Cosmos__AppDbContext__ConnectionString"] = cosmos.Resource.ConnectionStringExpression;
}
else
{
context.EnvironmentVariables["Aspire__Microsoft__EntityFrameworkCore__Cosmos__AppDbContext__AccountEndpoint"] = cosmos.Resource.ConnectionStringExpression;
}
});
builder.Build().Run(); |
Awesome. Thank you. |
Is there an existing issue for this?
Describe the bug
I have a very simple demo app here: /~https://github.com/mwiedemeyer/AspireDemo
Updated to Aspire 9.1
In the AppHost I add a CosmosDB resource with
In my client a try to add a cosmos EF context with:
builder.AddCosmosDbContext<AppDbContext>("cosmos", "db");
However it fails, because it cannot find the connection string.
In the env variables of my client it shows only these connection strings mapping to the cosmos db:
Aspire__Microsoft__Azure__Cosmos__users__ConnectionString
users
The client fails with this error message:
'A DbContext could not be configured. Ensure valid connection information was provided in 'ConnectionStrings:cosmos' or either ConnectionString or AccountEndpoint must be provided in the 'Aspire:Microsoft:EntityFrameworkCore:Cosmos' or 'Aspire:Microsoft:EntityFrameworkCore:Cosmos:AppDbContext' configuration section.'
So the names do not match. Is there anything I need to do to fix it?
I also tried this one, but it fails either:
builder.AddCosmosDbContext<AppDbContext>("users", "db");
Expected Behavior
I expect the documented version to work like in the description above
Steps To Reproduce
Run the demo project from here /~https://github.com/mwiedemeyer/AspireDemo
Then open the console logs for the func and click on the link (something like
http://localhost:7184/api/Function1
)Exceptions (if any)
InvalidOperationException
'A DbContext could not be configured. Ensure valid connection information was provided in 'ConnectionStrings:cosmos' or either ConnectionString or AccountEndpoint must be provided in the 'Aspire:Microsoft:EntityFrameworkCore:Cosmos' or 'Aspire:Microsoft:EntityFrameworkCore:Cosmos:AppDbContext' configuration section.'
.NET Version info
.NET SDK:
Version: 9.0.200
Commit: 90e8b202f2
Workload version: 9.0.200-manifests.c4f6226a
MSBuild version: 17.13.8+cbc39bea8
Anything else?
Aspire 9.1 just updated after the release yesterday. Working with Visual Studio.
The text was updated successfully, but these errors were encountered: