Add Aspire upgrade code samples

This commit is contained in:
fiodarsazanavets
2026-02-07 09:59:47 +00:00
parent 9312555269
commit e8d6decd1a
110 changed files with 2894 additions and 1 deletions
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Aspire.Microsoft.EntityFrameworkCore.SqlServer" Version="9.2.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AspireApp.ServiceDefaults\AspireApp.ServiceDefaults.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,19 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace AspireApp.ApiService.Models;
public class WeatherForecast
{
[Key]
public int Id { get; set; }
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public string? Summary { get; set; }
[NotMapped]
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
@@ -0,0 +1,49 @@
using AspireApp.ApiService;
using AspireApp.ApiService.Models;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
builder.Services.AddProblemDetails();
builder.AddSqlServerDbContext<WeatherDbContext>("sqldb");
var app = builder.Build();
app.UseExceptionHandler();
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
using (var scope = app.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<WeatherDbContext>();
context.Database.EnsureCreated();
if (!context.WeatherForecasts.Any())
{
foreach (var index in Enumerable.Range(1, 5))
{
context.WeatherForecasts.Add(new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
});
context.SaveChanges();
}
}
}
app.MapGet("/weatherforecast", ([FromServices] WeatherDbContext context) =>
{
return context.WeatherForecasts.ToArray();
});
app.MapDefaultEndpoints();
app.Run();
@@ -0,0 +1,25 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "weatherforecast",
"applicationUrl": "http://localhost:5423",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "weatherforecast",
"applicationUrl": "https://localhost:7598;http://localhost:5423",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
@@ -0,0 +1,13 @@
using AspireApp.ApiService.Models;
using Microsoft.EntityFrameworkCore;
namespace AspireApp.ApiService;
public class WeatherDbContext : DbContext
{
public WeatherDbContext(DbContextOptions<WeatherDbContext> options) : base(options)
{
}
public DbSet<WeatherForecast> WeatherForecasts { get; set; }
}
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}