From 48db8db8aed44d7da31b70df4d1084a245c2b9aa Mon Sep 17 00:00:00 2001 From: Andre Beging Date: Thu, 27 Mar 2025 05:48:39 +0100 Subject: [PATCH] net9 Update --- .idea/.idea.FoodsharingSiegen/.idea/misc.xml | 8 --- .../FoodsharingSiegen.Contracts.csproj | 4 +- FoodsharingSiegen.Server/Extensions.cs | 59 +++++++++++++++++++ .../FoodsharingSiegen.Server.csproj | 12 ++-- FoodsharingSiegen.Server/Pages/_Layout.cshtml | 11 ++-- FoodsharingSiegen.Server/Program.cs | 15 +++-- .../Properties/launchSettings.json | 1 + .../config/appsettings.json | 9 +++ .../FoodsharingSiegen.Shared.csproj | 16 ++--- 9 files changed, 102 insertions(+), 33 deletions(-) delete mode 100644 .idea/.idea.FoodsharingSiegen/.idea/misc.xml create mode 100644 FoodsharingSiegen.Server/Extensions.cs create mode 100644 FoodsharingSiegen.Server/config/appsettings.json diff --git a/.idea/.idea.FoodsharingSiegen/.idea/misc.xml b/.idea/.idea.FoodsharingSiegen/.idea/misc.xml deleted file mode 100644 index 283b9b4..0000000 --- a/.idea/.idea.FoodsharingSiegen/.idea/misc.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/FoodsharingSiegen.Contracts/FoodsharingSiegen.Contracts.csproj b/FoodsharingSiegen.Contracts/FoodsharingSiegen.Contracts.csproj index 445c813..1ebf973 100644 --- a/FoodsharingSiegen.Contracts/FoodsharingSiegen.Contracts.csproj +++ b/FoodsharingSiegen.Contracts/FoodsharingSiegen.Contracts.csproj @@ -1,13 +1,13 @@ - net6.0 + net9.0 enable enable - + diff --git a/FoodsharingSiegen.Server/Extensions.cs b/FoodsharingSiegen.Server/Extensions.cs new file mode 100644 index 0000000..becd649 --- /dev/null +++ b/FoodsharingSiegen.Server/Extensions.cs @@ -0,0 +1,59 @@ +using FoodsharingSiegen.Server.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration.Json; + +namespace FoodsharingSiegen.Server +{ + public static class Extensions + { + #region Public Method AddDatabaseContext + + /// + /// Configures the application's data access layer by setting up and registering a database context using SQLite. + /// Ensures the database file is stored within the application's base directory under a "data" folder. + /// + /// An instance of used to configure the application's services and resources. + public static void AddDatabaseContext(this WebApplicationBuilder builder) + { + var dataDirectory = Path.Combine(AppContext.BaseDirectory, "data"); + if (!Directory.Exists(dataDirectory)) Directory.CreateDirectory(dataDirectory); + + var dbPath = Path.Combine(dataDirectory, "app.db"); + var connectionString = $"Data Source={dbPath}"; + + builder.Services.AddDbContextFactory(options => + options.UseSqlite(connectionString)); + } + + #endregion + + #region Public Method LoadAppSettings + + /// + /// Loads application settings from JSON configuration files located in a designated "config" directory. + /// Clears existing JSON configuration files and ensures settings from the specified files are applied. + /// + /// An instance of used to configure and load application settings. + public static void LoadAppSettings(this WebApplicationBuilder builder) + { + // Clear loaded json files + foreach (var configurationSource in builder.Configuration.Sources.Where(x => x is JsonConfigurationSource).ToList()) + builder.Configuration.Sources.Remove(configurationSource); + + // Define the directory where your appsettings files reside + var configDir = Path.Combine(builder.Environment.ContentRootPath, "config"); + + // Check if the directory exists + if (Directory.Exists(configDir)) + { + // Get all JSON files that start with "appsettings" in the directory and its subdirectories + var configFiles = Directory.EnumerateFiles(configDir, "appsettings*.json", SearchOption.AllDirectories); + + // Add each file to the configuration + foreach (var file in configFiles) builder.Configuration.AddJsonFile(file, true, true); + } + } + + #endregion + } +} \ No newline at end of file diff --git a/FoodsharingSiegen.Server/FoodsharingSiegen.Server.csproj b/FoodsharingSiegen.Server/FoodsharingSiegen.Server.csproj index 07349be..420c8e1 100644 --- a/FoodsharingSiegen.Server/FoodsharingSiegen.Server.csproj +++ b/FoodsharingSiegen.Server/FoodsharingSiegen.Server.csproj @@ -1,7 +1,7 @@ - net6.0 + net9.0 enable enable @@ -19,14 +19,14 @@ - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + diff --git a/FoodsharingSiegen.Server/Pages/_Layout.cshtml b/FoodsharingSiegen.Server/Pages/_Layout.cshtml index 6d22dbd..226eb84 100644 --- a/FoodsharingSiegen.Server/Pages/_Layout.cshtml +++ b/FoodsharingSiegen.Server/Pages/_Layout.cshtml @@ -20,11 +20,12 @@ - - - + + + - + + @@ -43,6 +44,6 @@ - + \ No newline at end of file diff --git a/FoodsharingSiegen.Server/Program.cs b/FoodsharingSiegen.Server/Program.cs index 630013a..c149ed9 100644 --- a/FoodsharingSiegen.Server/Program.cs +++ b/FoodsharingSiegen.Server/Program.cs @@ -1,6 +1,7 @@ using Blazorise; using Blazorise.Icons.Material; using Blazorise.Material; +using FoodsharingSiegen.Server; using FoodsharingSiegen.Server.Auth; using FoodsharingSiegen.Server.Data; using FoodsharingSiegen.Server.Data.Service; @@ -9,14 +10,15 @@ using Microsoft.AspNetCore.Components.Authorization; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); +builder.LoadAppSettings(); + builder.WebHost.UseUrls("http://+:8700"); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); builder.Services.AddSingleton(); -builder.Services.AddDbContextFactory(opt => - opt.UseSqlite($"Data Source=app.db")); +builder.AddDatabaseContext(); // DI builder.Services.AddScoped(); @@ -28,8 +30,13 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); - -builder.Services.AddBlazorise( options => { options.Immediate = true; }).AddMaterialProviders().AddMaterialIcons(); +builder.Services + .AddBlazorise( options => + { + options.Immediate = true; + } ) + .AddMaterialProviders() + .AddMaterialIcons(); var app = builder.Build(); diff --git a/FoodsharingSiegen.Server/Properties/launchSettings.json b/FoodsharingSiegen.Server/Properties/launchSettings.json index 275b836..434a3c8 100644 --- a/FoodsharingSiegen.Server/Properties/launchSettings.json +++ b/FoodsharingSiegen.Server/Properties/launchSettings.json @@ -12,6 +12,7 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, + "applicationUrl": "http://localhost:8700", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/FoodsharingSiegen.Server/config/appsettings.json b/FoodsharingSiegen.Server/config/appsettings.json new file mode 100644 index 0000000..3a82afb --- /dev/null +++ b/FoodsharingSiegen.Server/config/appsettings.json @@ -0,0 +1,9 @@ +{ + "Kestrel": { + "Endpoints": { + "Http": { + "Url": "http://+:56000" + } + } + } +} \ No newline at end of file diff --git a/FoodsharingSiegen.Shared/FoodsharingSiegen.Shared.csproj b/FoodsharingSiegen.Shared/FoodsharingSiegen.Shared.csproj index ad6f994..1982e88 100644 --- a/FoodsharingSiegen.Shared/FoodsharingSiegen.Shared.csproj +++ b/FoodsharingSiegen.Shared/FoodsharingSiegen.Shared.csproj @@ -1,19 +1,19 @@ - net6.0 + net9.0 enable enable - - - - - - - + + + + + + +