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

WaitFor: SQL Server #5669

Merged
merged 17 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Aspire.sln
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WaitForSandbox.DbSetup", "p
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WaitForSandbox.Common", "playground\waitfor\WaitForSandbox.Common\WaitForSandbox.Common.csproj", "{F0C976EF-EE26-4EA9-B324-0CD21DCEA140}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlServerEndToEnd.Common", "playground\SqlServerEndToEnd\SqlServerEndToEnd.Common\SqlServerEndToEnd.Common.csproj", "{1997067D-8EF2-43B3-AB13-9B2E12B52709}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlServerEndToEnd.DbSetup", "playground\SqlServerEndToEnd\SqlServerEndToEnd.DbSetup\SqlServerEndToEnd.DbSetup.csproj", "{125C081D-7E5B-4F35-B5CD-E2B56140380F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1590,6 +1594,14 @@ Global
{F0C976EF-EE26-4EA9-B324-0CD21DCEA140}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0C976EF-EE26-4EA9-B324-0CD21DCEA140}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0C976EF-EE26-4EA9-B324-0CD21DCEA140}.Release|Any CPU.Build.0 = Release|Any CPU
{1997067D-8EF2-43B3-AB13-9B2E12B52709}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1997067D-8EF2-43B3-AB13-9B2E12B52709}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1997067D-8EF2-43B3-AB13-9B2E12B52709}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1997067D-8EF2-43B3-AB13-9B2E12B52709}.Release|Any CPU.Build.0 = Release|Any CPU
{125C081D-7E5B-4F35-B5CD-E2B56140380F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{125C081D-7E5B-4F35-B5CD-E2B56140380F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{125C081D-7E5B-4F35-B5CD-E2B56140380F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{125C081D-7E5B-4F35-B5CD-E2B56140380F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1881,6 +1893,8 @@ Global
{C554C480-3DA7-4D62-A09A-3F3F743D7A66} = {3FF3F00C-95C0-46FC-B2BE-A3920C71E393}
{37BC5B4A-3F96-4BB0-92E6-81666F4324E4} = {3FF3F00C-95C0-46FC-B2BE-A3920C71E393}
{F0C976EF-EE26-4EA9-B324-0CD21DCEA140} = {3FF3F00C-95C0-46FC-B2BE-A3920C71E393}
{1997067D-8EF2-43B3-AB13-9B2E12B52709} = {2CA6AB88-21EF-4488-BB1B-3A5BAD5FE2AD}
{125C081D-7E5B-4F35-B5CD-E2B56140380F} = {2CA6AB88-21EF-4488-BB1B-3A5BAD5FE2AD}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6DCEDFEC-988E-4CB3-B45B-191EB5086E0C}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore;
using SqlServerEndToEnd.Common;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -21,39 +22,23 @@
await db1Context.Database.EnsureCreatedAsync();
await db2Context.Database.EnsureCreatedAsync();

var entry = new Entry();
await db1Context.Entries.AddAsync(entry);
var entry1 = new Entry();
await db1Context.Entries.AddAsync(entry1);
await db1Context.SaveChangesAsync();

var entries = await db1Context.Entries.ToListAsync();
var entry2 = new Entry();
await db2Context.Entries.AddAsync(entry2);
await db2Context.SaveChangesAsync();

var entries1 = await db1Context.Entries.ToListAsync();
var entries2 = await db2Context.Entries.ToListAsync();

return new
{
totalEntries = entries.Count,
entries = entries
totalEntries = entries1.Count + entries2.Count,
entries1 = entries1,
entries2 = entries2
};
});

app.Run();

public class MyDb1Context(DbContextOptions<MyDb1Context> options) : DbContext(options)
{
public DbSet<Entry> Entries { get; set; }
}

public class MyDb2Context(DbContextOptions<MyDb2Context> options) : DbContext(options)
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<Entry>().HasKey(e => e.Id);
}

public DbSet<Entry> Entries { get; set; }
}

public class Entry
{
public Guid Id { get; set; } = Guid.NewGuid();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<AspireProjectOrPackageReference Include="Aspire.Microsoft.EntityFrameworkCore.SqlServer" />
<ProjectReference Include="..\..\Playground.ServiceDefaults\Playground.ServiceDefaults.csproj" />
<ProjectReference Include="..\SqlServerEndToEnd.Common\SqlServerEndToEnd.Common.csproj" />
</ItemGroup>

</Project>
20 changes: 16 additions & 4 deletions playground/SqlServerEndToEnd/SqlServerEndToEnd.AppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@

var builder = DistributedApplication.CreateBuilder(args);

var db1 = builder.AddSqlServer("sql1").PublishAsAzureSqlDatabase().AddDatabase("db1");
var db2 = builder.AddSqlServer("sql2").PublishAsContainer().AddDatabase("db2");
var sql1 = builder.AddSqlServer("sql1")
.PublishAsAzureSqlDatabase();

var db1 = sql1.AddDatabase("db1");

var sql2 = builder.AddSqlServer("sql2")
.PublishAsContainer();

var db2 = sql2.AddDatabase("db2");

var dbsetup = builder.AddProject<Projects.SqlServerEndToEnd_DbSetup>("dbsetup")
.WithReference(db1).WaitFor(sql1)
.WithReference(db2).WaitFor(sql2);

builder.AddProject<Projects.SqlServerEndToEnd_ApiService>("api")
.WithExternalHttpEndpoints()
.WithReference(db1)
.WithReference(db2);
.WithReference(db1).WaitFor(db1)
.WithReference(db2).WaitFor(db2)
.WaitForCompletion(dbsetup);

#if !SKIP_DASHBOARD_REFERENCE
// This project is only added in playground projects to support development/debugging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<UserSecretsId>209ffcfc-80c8-470f-87d4-ef96525f2cdc</UserSecretsId>
</PropertyGroup>

<ItemGroup>
Expand All @@ -18,6 +19,8 @@
<AspireProjectOrPackageReference Include="Aspire.Hosting.AppHost" />

<ProjectReference Include="..\SqlServerEndToEnd.ApiService\SqlServerEndToEnd.ApiService.csproj" />

<ProjectReference Include="..\SqlServerEndToEnd.DbSetup\SqlServerEndToEnd.DbSetup.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace SqlServerEndToEnd.Common;

public class Entry
{
public Guid Id { get; set; } = Guid.NewGuid();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore;

namespace SqlServerEndToEnd.Common;

public class MyDb1Context(DbContextOptions<MyDb1Context> options) : DbContext(options)
{
public DbSet<Entry> Entries { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore;

namespace SqlServerEndToEnd.Common;

public class MyDb2Context(DbContextOptions<MyDb2Context> options) : DbContext(options)
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<Entry>().HasKey(e => e.Id);
}

public DbSet<Entry> Entries { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
mitchdenny marked this conversation as resolved.
Show resolved Hide resolved
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions playground/SqlServerEndToEnd/SqlServerEndToEnd.DbSetup/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore;
using SqlServerEndToEnd.Common;

var builder = WebApplication.CreateBuilder(args);
builder.AddSqlServerDbContext<MyDb1Context>("db1");
builder.AddSqlServerDbContext<MyDb2Context>("db2");
using var app = builder.Build();
using var scope = app.Services.CreateScope();
using var db1 = scope.ServiceProvider.GetRequiredService<MyDb1Context>();
using var db2 = scope.ServiceProvider.GetRequiredService<MyDb2Context>();

foreach (var db in new DbContext[] { db1, db2 })
{
var created = await db.Database.EnsureCreatedAsync();
if (created)
{
Console.WriteLine("Database created!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": {
"SqlServerEndToEnd.DbSetup": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:49994;http://localhost:49995"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
mitchdenny marked this conversation as resolved.
Show resolved Hide resolved
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\SqlServerEndToEnd.Common\SqlServerEndToEnd.Common.csproj" />
</ItemGroup>

<ItemGroup>
<AspireProjectOrPackageReference Include="Aspire.Microsoft.EntityFrameworkCore.SqlServer" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion playground/waitfor/WaitForSandbox.AppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var builder = DistributedApplication.CreateBuilder(args);

var pg = builder.AddPostgres("pg")
// .AsAzurePostgresFlexibleServer()
.PublishAsAzurePostgresFlexibleServer()
.WithPgAdmin();

var db = pg.AddDatabase("db");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ItemGroup>
<AspireProjectOrPackageReference Include="Aspire.Hosting.AppHost" />
<AspireProjectOrPackageReference Include="Aspire.Hosting.PostgreSQL" />
<AspireProjectOrPackageReference Include="Aspire.Hosting.SqlServer" />
<AspireProjectOrPackageReference Include="Aspire.Hosting.Azure.PostgreSQL" />

<ProjectReference Include="..\WaitForSandbox.ApiService\WaitForSandbox.ApiService.csproj" />
Expand Down
3 changes: 2 additions & 1 deletion playground/waitfor/WaitForSandbox.AppHost/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
"Aspire.Hosting.Dcp": "Warning",
"Aspire.Hosting": "Trace"
}
}
}
10 changes: 10 additions & 0 deletions src/Aspire.Hosting.PostgreSQL/PostgresBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public static IResourceBuilder<PostgresServerResource> AddPostgres(this IDistrib
{
connectionString = await postgresServer.GetConnectionStringAsync(ct).ConfigureAwait(false);

if (connectionString == null)
{
throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{postgresServer}' resource but the connection string was null.");
}

var lookup = builder.Resources.OfType<PostgresDatabaseResource>().ToDictionary(d => d.Name);

foreach (var databaseName in postgresServer.Databases)
Expand Down Expand Up @@ -133,6 +138,11 @@ public static IResourceBuilder<PostgresDatabaseResource> AddDatabase(this IResou
builder.ApplicationBuilder.Eventing.Subscribe<ConnectionStringAvailableEvent>(postgresDatabase, async (@event, ct) =>
{
connectionString = await postgresDatabase.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false);

if (connectionString == null)
{
throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{postgresDatabase}' resource but the connection string was null.");
}
});

var healthCheckKey = $"{name}_check";
Expand Down
5 changes: 5 additions & 0 deletions src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public static IResourceBuilder<RedisResource> AddRedis(this IDistributedApplicat
builder.Eventing.Subscribe<ConnectionStringAvailableEvent>(redis, async (@event, ct) =>
{
connectionString = await redis.GetConnectionStringAsync(ct).ConfigureAwait(false);

if (connectionString == null)
{
throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{redis}' resource but the connection string was null.");
}
});

var healthCheckKey = $"{name}_check";
Expand Down
4 changes: 4 additions & 0 deletions src/Aspire.Hosting.SqlServer/Aspire.Hosting.SqlServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<Compile Include="$(SharedDir)VolumeNameGenerator.cs" Link="Utils\VolumeNameGenerator.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Aspire.Hosting\Aspire.Hosting.csproj" />
</ItemGroup>
Expand Down
Loading
Loading