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

Postgres end to end. #2063

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions Aspire.sln
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ParameterEndToEnd.AppHost",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ParameterEndToEnd.ApiService", "playground\ParameterEndToEnd\ParameterEndToEnd.ApiService\ParameterEndToEnd.ApiService.csproj", "{FD63D574-8512-421D-B7FC-310AFA974361}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PostgresEndToEnd", "PostgresEndToEnd", "{2A076AC1-9201-4E9B-BCB7-5AC4F8F4AADB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PostgresEndToEnd.AppHost", "playground\PostgresEndToEnd\PostgresEndToEnd.AppHost\PostgresEndToEnd.AppHost.csproj", "{A4C06329-56EA-4AE6-A698-D2B7B7ABE318}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PostgresEndToEnd.ApiService", "playground\PostgresEndToEnd\PostgresEndToEnd.ApiService\PostgresEndToEnd.ApiService.csproj", "{B7BC1137-25E2-4B5D-A45F-38F5BA4811A2}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "mysql", "mysql", "{621991F1-854A-4743-835B-10CAF11A0CFF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MySqlDb.AppHost", "Playground\mysql\MySqlDb.AppHost\MySqlDb.AppHost.csproj", "{7E2AD00B-60E0-46C2-8640-7217D678F312}"
Expand Down Expand Up @@ -605,6 +610,14 @@ Global
{FD63D574-8512-421D-B7FC-310AFA974361}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD63D574-8512-421D-B7FC-310AFA974361}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FD63D574-8512-421D-B7FC-310AFA974361}.Release|Any CPU.Build.0 = Release|Any CPU
{A4C06329-56EA-4AE6-A698-D2B7B7ABE318}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A4C06329-56EA-4AE6-A698-D2B7B7ABE318}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4C06329-56EA-4AE6-A698-D2B7B7ABE318}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4C06329-56EA-4AE6-A698-D2B7B7ABE318}.Release|Any CPU.Build.0 = Release|Any CPU
{B7BC1137-25E2-4B5D-A45F-38F5BA4811A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B7BC1137-25E2-4B5D-A45F-38F5BA4811A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B7BC1137-25E2-4B5D-A45F-38F5BA4811A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B7BC1137-25E2-4B5D-A45F-38F5BA4811A2}.Release|Any CPU.Build.0 = Release|Any CPU
{7E2AD00B-60E0-46C2-8640-7217D678F312}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7E2AD00B-60E0-46C2-8640-7217D678F312}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7E2AD00B-60E0-46C2-8640-7217D678F312}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -719,6 +732,9 @@ Global
{F1387494-34DE-4B31-B587-699B2E9A20CA} = {D173887B-AF42-4576-B9C1-96B9E9B3D9C0}
{54B66163-016D-4122-9364-409AB61BC36B} = {F1387494-34DE-4B31-B587-699B2E9A20CA}
{FD63D574-8512-421D-B7FC-310AFA974361} = {F1387494-34DE-4B31-B587-699B2E9A20CA}
{2A076AC1-9201-4E9B-BCB7-5AC4F8F4AADB} = {D173887B-AF42-4576-B9C1-96B9E9B3D9C0}
{A4C06329-56EA-4AE6-A698-D2B7B7ABE318} = {2A076AC1-9201-4E9B-BCB7-5AC4F8F4AADB}
{B7BC1137-25E2-4B5D-A45F-38F5BA4811A2} = {2A076AC1-9201-4E9B-BCB7-5AC4F8F4AADB}
{621991F1-854A-4743-835B-10CAF11A0CFF} = {D173887B-AF42-4576-B9C1-96B9E9B3D9C0}
{7E2AD00B-60E0-46C2-8640-7217D678F312} = {621991F1-854A-4743-835B-10CAF11A0CFF}
{F699F3AD-2AD9-454B-BA40-82AC3D6250FE} = {621991F1-854A-4743-835B-10CAF11A0CFF}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Components\Aspire.Npgsql.EntityFrameworkCore.PostgreSQL\Aspire.Npgsql.EntityFrameworkCore.PostgreSQL.csproj" />
<ProjectReference Include="..\..\Playground.ServiceDefaults\Playground.ServiceDefaults.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@CosmosEndToEnd.ApiService_HostAddress = http://localhost:5193

GET {{SqlServerEndToEnd.ApiService_HostAddress}}/weatherforecast/
Accept: application/json

###
149 changes: 149 additions & 0 deletions playground/PostgresEndToEnd/PostgresEndToEnd.ApiService/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// 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;

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();

builder.AddNpgsqlDbContext<MyDb1Context>("db1");
builder.AddNpgsqlDbContext<MyDb2Context>("db2");
builder.AddNpgsqlDbContext<MyDb3Context>("db3");
builder.AddNpgsqlDbContext<MyDb4Context>("db4");
builder.AddNpgsqlDbContext<MyDb5Context>("db5");
builder.AddNpgsqlDbContext<MyDb6Context>("db6");
builder.AddNpgsqlDbContext<MyDb7Context>("db7");
builder.AddNpgsqlDbContext<MyDb8Context>("db8");
var app = builder.Build();

app.MapGet("/", async (MyDb1Context db1Context, MyDb2Context db2Context, MyDb3Context db3Context, MyDb4Context db4Context, MyDb5Context db5Context, MyDb6Context db6Context, MyDb7Context db7Context, MyDb8Context db8Context) =>
{
// You wouldn't normally do this on every call,
// but doing it here just to make this simple.
db1Context.Database.EnsureCreated();
db2Context.Database.EnsureCreated();
db3Context.Database.EnsureCreated();
db4Context.Database.EnsureCreated();
db5Context.Database.EnsureCreated();
db6Context.Database.EnsureCreated();
db7Context.Database.EnsureCreated();
db8Context.Database.EnsureCreated();

// We only work with db1Context for the rest of this
// since we've proven connectivity to the others for now.
var entry = new Entry();
await db1Context.Entries.AddAsync(entry);
await db1Context.SaveChangesAsync();

var entries = await db1Context.Entries.ToListAsync();

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

app.Run();

public class MyDb1Context(DbContextOptions<MyDb1Context> 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 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 MyDb3Context(DbContextOptions<MyDb3Context> 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 MyDb4Context(DbContextOptions<MyDb4Context> 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 MyDb5Context(DbContextOptions<MyDb5Context> 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 MyDb6Context(DbContextOptions<MyDb6Context> 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 MyDb7Context(DbContextOptions<MyDb7Context> 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 MyDb8Context(DbContextOptions<MyDb8Context> 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
@@ -0,0 +1,14 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5180",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project>

<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />

<!-- NOTE: This line is only required because we are using P2P references, not NuGet. It will not exist in real apps. -->
<Import Project="../../../src/Aspire.Hosting/build/Aspire.Hosting.props" />

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project>

<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.targets', '$(MSBuildThisFileDirectory)../'))" />

<!-- NOTE: These lines are only required because we are using P2P references, not NuGet. They will not exist in real apps. -->
<Import Project="..\..\..\src\Aspire.Hosting\build\Aspire.Hosting.targets" />
<Import Project="..\..\..\src\Aspire.Hosting.Sdk\SDK\Sdk.targets" />

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(SharedDir)KnownResourceNames.cs" Link="KnownResourceNames.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Aspire.Dashboard\Aspire.Dashboard.csproj" />
<ProjectReference Include="..\..\..\src\Aspire.Hosting\Aspire.Hosting.csproj" IsAspireProjectResource="false" />

<ProjectReference Include="..\PostgresEndToEnd.ApiService\PostgresEndToEnd.ApiService.csproj" />
</ItemGroup>

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

var builder = DistributedApplication.CreateBuilder(args);

// Abstract resources.
var db1 = builder.AddPostgres("pg1").AddDatabase("db1");
var db2 = builder.AddPostgres("pg2").AddDatabase("db2");
var pg3 = builder.AddPostgres("pg3");
var db3 = pg3.AddDatabase("db3");
var db4 = pg3.AddDatabase("db4");

// Containerized resources.
var db5 = builder.AddPostgresContainer("pg4").AddDatabase("db5");
var db6 = builder.AddPostgresContainer("pg5").AddDatabase("db6");
var pg6 = builder.AddPostgresContainer("pg6");
var db7 = pg6.AddDatabase("db7");
var db8 = pg6.AddDatabase("db8");

builder.AddProject<Projects.PostgresEndToEnd_ApiService>("api")
.WithReference(db1)
.WithReference(db2)
.WithReference(db3)
.WithReference(db4)
.WithReference(db5)
.WithReference(db6)
.WithReference(db7)
.WithReference(db8);

// This project is only added in playground projects to support development/debugging
// of the dashboard. It is not required in end developer code. Comment out this code
// to test end developer dashboard launch experience. Refer to Directory.Build.props
// for the path to the dashboard binary (defaults to the Aspire.Dashboard bin output
// in the artifacts dir).
builder.AddProject<Projects.Aspire_Dashboard>(KnownResourceNames.AspireDashboard);

builder.Build().Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15888",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:16175",
"DOTNET_ASPIRE_SHOW_DASHBOARD_RESOURCES": "true"
}
},
"generate-manifest": {
"commandName": "Project",
"launchBrowser": true,
"dotnetRunMessages": true,
"commandLineArgs": "--publisher manifest --output-path aspire-manifest.json",
"applicationUrl": "http://localhost:15888",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:16175"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}
Loading