Skip to content
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

Error when referencing CosmosDB #1502

Closed
Kralizek opened this issue Dec 27, 2023 · 3 comments
Closed

Error when referencing CosmosDB #1502

Kralizek opened this issue Dec 27, 2023 · 3 comments
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication

Comments

@Kralizek
Copy link

I have this simple application

var builder = DistributedApplication.CreateBuilder(args);

var db = builder.AddAzureCosmosDB("db");

var api = builder.AddProject<Projects.API>("api")
    .WithReference(db);

builder.Build().Run();

When I launch it, I get the following error message:

info: Aspire.Dashboard.DashboardWebApplication[0]
      Now listening on: http://localhost:15080
info: Aspire.Dashboard.DashboardWebApplication[0]
      OTLP server running at: http://localhost:16085
fail: Microsoft.Extensions.Hosting.Internal.Host[11]
      Hosting failed to start
      Aspire.Hosting.DistributedApplicationException: A connection string for 'db' could not be retrieved.
         at Aspire.Hosting.ResourceBuilderExtensions.<>c__DisplayClass8_0`1.<WithReference>b__0(EnvironmentCallbackContext context) in /_/src/Aspire.Hosting/Extensions/ResourceBuilderExtensions.cs:line 176
         at Aspire.Hosting.Dcp.ApplicationExecutor.CreateExecutablesAsync(IEnumerable`1 executableResources, CancellationToken cancellationToken) in /_/src/Aspire.Hosting/Dcp/ApplicationExecutor.cs:line 431
         at Aspire.Hosting.Dcp.ApplicationExecutor.CreateContainersAndExecutablesAsync(CancellationToken cancellationToken) in /_/src/Aspire.Hosting/Dcp/ApplicationExecutor.cs:line 170
         at Aspire.Hosting.Dcp.ApplicationExecutor.RunApplicationAsync(CancellationToken cancellationToken) in /_/src/Aspire.Hosting/Dcp/ApplicationExecutor.cs:line 83
         at Aspire.Hosting.Dcp.DcpHostService.StartAsync(CancellationToken cancellationToken) in /_/src/Aspire.Hosting/Dcp/DcpHostService.cs:line 83
         at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>b__15_1(IHostedService service, CancellationToken token)
         at Microsoft.Extensions.Hosting.Internal.Host.ForeachService[T](IEnumerable`1 services, CancellationToken token, Boolean concurrent, Boolean abortOnFirstException, List`1 exceptions, Func`3 operation)
Could not stop project 'api': k8s.Autorest.HttpOperationException: Operation returned an invalid status code 'NotFound', response body {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"executablereplicasets.usvc-dev.developer.microsoft.com \"api\" not found","reason":"NotFound","details":{"name":"api","group":"usvc-dev.developer.microsoft.com","kind":"executablereplicasets"},"code":404}

   at k8s.Kubernetes.SendRequestRaw(String requestContent, HttpRequestMessage httpRequest, CancellationToken cancellationToken)
   at k8s.AbstractKubernetes.ICustomObjectsOperations_DeleteClusterCustomObjectWithHttpMessagesAsync[T](String group, String version, String plural, String name, V1DeleteOptions body, Nullable`1 gracePeriodSeconds, Nullable`1 orphanDependents, String propagationPolicy, String dryRun, IReadOnlyDictionary`2 customHeaders, CancellationToken cancellationToken)
   at k8s.AbstractKubernetes.k8s.ICustomObjectsOperations.DeleteClusterCustomObjectWithHttpMessagesAsync(String group, String version, String plural, String name, V1DeleteOptions body, Nullable`1 gracePeriodSeconds, Nullable`1 orphanDependents, String propagationPolicy, String dryRun, IReadOnlyDictionary`2 customHeaders, CancellationToken cancellationToken)
   at Aspire.Hosting.Dcp.KubernetesService.<>c__DisplayClass12_0`1.<<DeleteAsync>b__0>d.MoveNext() in /_/src/Aspire.Hosting/Dcp/KubernetesService.cs:line 109
--- End of stack trace from previous location ---
   at Aspire.Hosting.Dcp.KubernetesService.ExecuteWithRetry[TResult](DcpApiOperationType operationType, String resourceType, Func`2 operation, CancellationToken cancellationToken) in /_/src/Aspire.Hosting/Dcp/KubernetesService.cs:line 198
   at Aspire.Hosting.Dcp.ApplicationExecutor.DeleteResourcesAsync[RT](String resourceName, CancellationToken cancellationToken) in /_/src/Aspire.Hosting/Dcp/ApplicationExecutor.cs:line 699
Unhandled exception. System.AggregateException: One or more errors occurred. (A connection string for 'db' could not be retrieved.)
 ---> Aspire.Hosting.DistributedApplicationException: A connection string for 'db' could not be retrieved.
   at Aspire.Hosting.ResourceBuilderExtensions.<>c__DisplayClass8_0`1.<WithReference>b__0(EnvironmentCallbackContext context) in /_/src/Aspire.Hosting/Extensions/ResourceBuilderExtensions.cs:line 176
   at Aspire.Hosting.Dcp.ApplicationExecutor.CreateExecutablesAsync(IEnumerable`1 executableResources, CancellationToken cancellationToken) in /_/src/Aspire.Hosting/Dcp/ApplicationExecutor.cs:line 431
   at Aspire.Hosting.Dcp.ApplicationExecutor.CreateContainersAndExecutablesAsync(CancellationToken cancellationToken) in /_/src/Aspire.Hosting/Dcp/ApplicationExecutor.cs:line 170
   at Aspire.Hosting.Dcp.ApplicationExecutor.RunApplicationAsync(CancellationToken cancellationToken) in /_/src/Aspire.Hosting/Dcp/ApplicationExecutor.cs:line 83
   at Aspire.Hosting.Dcp.DcpHostService.StartAsync(CancellationToken cancellationToken) in /_/src/Aspire.Hosting/Dcp/DcpHostService.cs:line 83
   at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>b__15_1(IHostedService service, CancellationToken token)
   at Microsoft.Extensions.Hosting.Internal.Host.ForeachService[T](IEnumerable`1 services, CancellationToken token, Boolean concurrent, Boolean abortOnFirstException, List`1 exceptions, Func`3 operation)
   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Aspire.Hosting.DistributedApplication.RunAsync(CancellationToken cancellationToken) in /_/src/Aspire.Hosting/DistributedApplication.cs:line 147
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at Aspire.Hosting.DistributedApplication.Run() in /_/src/Aspire.Hosting/DistributedApplication.cs:line 155
   at Program.<Main>$(String[] args) in C:\Users\RenatoGolia\Development\Tobii\Vault\Services\src\AppHost\Program.cs:line 8

Process finished with exit code -532,462,766.

The documentation mentions here

// Service registration
var cosmosdbService = builder.AddAzureCosmosDB("cdb")
                             .AddDatabase("cosmosdb");

// Service consumption
var exampleProject = builder.AddProject<Projects.ExampleProject>()
                            .WithReference(cosmosdbService);

But in Preview 2, the AddDatabase method is not available (see dotnet/docs-aspire#154)

Note: I would appreciate if a sample using CosmosDB could be added to the aspire-samples repo.

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Dec 27, 2023
@josephaw1022
Copy link

It seems that you are missing a connection string. you need to provide the azure cosmos connection string for the cosmos resource to actually be created so that the app can then reference it.

Based on the logs that you provided

fail: Microsoft.Extensions.Hosting.Internal.Host[11]
      Hosting failed to start
      Aspire.Hosting.DistributedApplicationException: A connection string for 'db' could not be retrieved.

And this section in the cosmos documentation which mentions a connection string being required since you must provision it in azure for it to work currently (support for the local emulator is in progress right now, but for now using azure is the only way to do it).

https://learn.microsoft.com/en-us/dotnet/aspire/database/azure-cosmos-db-component?tabs=dotnet-cli

@josephaw1022
Copy link

image

@mitchdenny
Copy link
Member

mitchdenny commented Jan 11, 2024

Good news @Kralizek ... the AddDatabase method has been added back for Cosmos DB. note that Cosmos DB connection strings do not include a database name, so this is purely for deployment scenarios where we might want the deployment tool to create a database. The database name still either needs to be hard coded in the service or passed in via an environment/configuration setting.

Thanks for creating the sample request. I'll close this issue down since it should be resolved.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication
Projects
None yet
Development

No branches or pull requests

3 participants