.Net Aspire framework is used to develop cloud and production-ready distributed applications. It consists of components to handle cloud-native concerns such as Redis, Postgres etc.
Learn how to create a starter project using .Net Aspire with the PGSQL EF Core component.
Github Sample: The solution structure is divided into the following projects
Install the following Nuget package into the subsequent project “DotnetAspireChallenge.AppHost”
dotnet add package Aspire.Hosting.PostgreSQL
In the above project, register a server database and customise the PGSQL connection using the following code.
var postgres = builder.AddPostgres("postgres")
.AddDatabase("pgsqldata");
var apiService = builder.AddProject<Projects.DotnetAspireChallenge_ApiService>("apiservice")
.WithReference(postgres);
Install the following Nuget package into the subsequent project “DotnetAspireChallenge.ApiService”
dotnet add package Aspire.Npgsql.EntityFrameworkCore.PostgreSQL
then register the context into the Program.cs file as follows
builder.AddNpgsqlDbContext<PgsqlDbContext>("pgsqldata");
public class Customer
{
public int Id { get; set; }
[Required]
public string Title { get; set; } = string.Empty;
[Required]
public string Description { get; set; } = string.Empty;
}
Create an extension class and register a minimal API get method to demonstrate the PGSQL context usage in the API Service
public static class AspirePgsqlExtension
{
public static void MapPgsqlAspireEndpoint(this WebApplication app)
{
app.MapGet("/pgsql", async (PgsqlDbContext pgsqlDbContext) =>
{
await pgsqlDbContext.CustomersPgsql.AddAsync(new Customer()
{
Title = "[email protected]",
Description = "sukh"
});
int rows = await pgsqlDbContext.SaveChangesAsync();
if (rows > 0)
{
return await pgsqlDbContext.CustomersPgsql.FirstOrDefaultAsync();
}
else
{
return null;
}
});
}
}
internal class PgsqlDbContext(DbContextOptions options) : DbContext(options)
{
public DbSet<Customer> CustomersPgsql => Set<Customer>();
}
and finally, register in the Program.cs file
app.MapPgsqlAspireEndpoint();
Finally, navigate to the GET URL in your browser, as shown below. It will insert the specified customer into the PGSQL database, retrieve the most recently inserted row, and display it as a response.
Add additional connection string properties using the JSON syntax
{
"Aspire": {
"Npgsql": {
"EntityFrameworkCore": {
"PostgreSQL": {
"ConnectionString": "YOUR_CONNECTIONSTRING",
"DbContextPooling": true,
"DisableHealthChecks": true,
"DisableTracing": true,
"AnotherDbContext": {
"ConnectionString": "AnotherDbContext_CONNECTIONSTRING",
"DisableTracing": false
}
}
}
}
}
}
Congratulations..!! You’ve successfully integrated the PGSQL component into the .Net Aspire project.
The .NET Aspire component will produce the following metrics using OpenTelemetry:
GitHub - ssukhpinder/DotnetAspireChallenge: 10 Day .Net Aspire Challenge
Thank you for being a part of the C# community! Before you leave:
Follow us: Youtube | X | LinkedIn | Dev.to Visit our other platforms: GitHub More content at C# Programming