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

GetConnectionStringAsync fails for AddConnectionString #7138

Open
1 task done
RicardoNiepel opened this issue Jan 17, 2025 · 1 comment
Open
1 task done

GetConnectionStringAsync fails for AddConnectionString #7138

RicardoNiepel opened this issue Jan 17, 2025 · 1 comment
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication
Milestone

Comments

@RicardoNiepel
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Adding a connection string in the AppHost doesn't allow to retrieve it in integration tests.

Sample:

  • Using an existing connection string or creating the resource in AppHost
  • In both ways a IResourceBuilder<IResourceWithConnectionString> is returned
  • Calling GetConnectionStringAsync inside tests throws exception, don't find it anymore
    if builder.AddConnectionString was used for adding IResourceWithConnectionString>

Code:

// AppHost
var exisitingOpenAi = !builder.Configuration.GetSection("ConnectionStrings")["openAi"].IsNullOrEmpty();
var openAi = !builder.ExecutionContext.IsPublishMode && exisitingOpenAi
    ? builder.AddConnectionString("openAi")
    : builder.AddAzureOpenAI("openAi")
        .AddDeployment(new AzureOpenAIDeployment(azureDeployment, "gpt-4o", "2024-05-13", "Standard", 10));

var backend = builder.AddProject<Projects.ChatApp_Backend>("backend")
    .WithReference(openAi)

// Integration Tests
var appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.ChatApp_AppHost>();
_app = await appHost.BuildAsync();
await _app.StartAsync();

// Throws 'System.ArgumentException' with "Resource 'openAi' does not expose a connection string." if `builder.AddConnectionString("openAi")` was executed
var openAi = await _app.GetConnectionStringAsync("openAi");

Insights:
Looking into DistributedApplicationModel.Resources of the app, there is no IResourceWithConnectionString, only a ParameterResource is now in the list:

Image

Expected Behavior

GetConnectionStringAsync returns a connection string in both situations

  • builder.AddConnectionString("openAi") was called
  • builder.AddAzureOpenAI("openAi") was called

Steps To Reproduce

No response

Exceptions (if any)

System.ArgumentException : Resource 'openAi' does not expose a connection string. (Parameter 'resourceName')
   at Aspire.Hosting.Testing.DistributedApplicationHostingTestingExtensions.GetConnectionStringAsync(DistributedApplication app, String resourceName, CancellationToken cancellationToken) in /_/src/Aspire.Hosting.Testing/DistributedApplicationHostingTestingExtensions.cs:line 46

.NET Version info

.NET SDK:
Version: 9.0.102
Commit: cb83cd4923
Workload version: 9.0.100-manifests.d67a1f3e
MSBuild version: 17.12.18+ed8c6aec5

OS Name: Windows
OS Version: 10.0.26100
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\9.0.102\

Anything else?

<PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0" />
<PackageReference Include="Aspire.Hosting.Azure" Version="9.0.0" />

<PackageReference Include="Aspire.Hosting.Testing" Version="9.0.0" />
@davidfowl davidfowl added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Jan 17, 2025
@davidfowl davidfowl added this to the 9.1 milestone Jan 17, 2025
@davidfowl
Copy link
Member

This is a valid bug but it has to do with how we implemented AddConnectionString

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication
Projects
None yet
Development

No branches or pull requests

2 participants