using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; using Microsoft.IdentityModel.Tokens; using System.Text; using FamilyTreeAPI.Authorization; using FamilyTreeAPI.Entities; using FamilyTreeAPI.GraphQL.Query; using FamilyTreeAPI.Interface; using FamilyTreeAPI.Models; using FamilyTreeAPI.Repository; var builder = WebApplication.CreateBuilder(args); builder.Logging.ClearProviders(); builder.Logging.AddConsole(); builder.Logging.AddDebug(); // Explicitly register the base paths for Linux container execution builder.Configuration .SetBasePath(AppContext.BaseDirectory) // <-- Pulls from wherever the binary sits .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables(); // Add services to the container. #region Services var services = builder.Services; services.AddCors(); services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle services.AddEndpointsApiExplorer(); services.AddSwaggerGen(); string skey = "Super Secret SIGN AND VERIFY JWT TOKENS"; var appSettingsSection = builder.Configuration.GetSection("AppSettings"); if (appSettingsSection.Exists()) { services.Configure(appSettingsSection); var appSettings = appSettingsSection.Get(); if ( appSettings != null) skey = appSettings.Secret; else { string tkey = builder.Configuration.GetValue("AppSettings:Secret") ?? string.Empty; if (!string.IsNullOrEmpty(tkey)) skey = tkey; } } var key = Encoding.ASCII.GetBytes(skey); services.AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(x => { x.RequireHttpsMetadata = false; x.SaveToken = true; x.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(key), ValidateIssuer = false, ValidateAudience = false }; }); services.AddHttpContextAccessor(); services.AddScoped(); services.AddScoped(); services.AddDbContext(options => { // options.LogTo(s => Console.WriteLine(s)); //options.UseNpgsql(appSettings.SQLConnectionString); string? conn = builder.Configuration.GetValue("AppSettings:SQLConnectionString"); // options.LogTo(s => Console.WriteLine(s)); if (conn != null) options.UseNpgsql(conn); }); services.AddGraphQLServer() .AddFiltering() .AddSorting() .AddProjections() .RegisterDbContextFactory() .AddQueryType(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); /* public FamilyTreeDBContext(DbContextOptions options) : base(options) { AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); } services.AddScoped(); services.AddScoped(); */ #endregion #region app var app = builder.Build(); using (var scope = app.Services.CreateScope()) { try { var context = scope.ServiceProvider.GetRequiredService(); var db = context.Database; if (db != null) { var staff = scope.ServiceProvider.GetService(); int id = staff.InsertOneUser(); if (id < 0) { var databaseCreator = (context.GetService() as RelationalDatabaseCreator); if (databaseCreator != null) { //if (!databaseCreator.Exists()) try { // Check if database exists if (!databaseCreator.Exists()) { databaseCreator.Create(); } if (!databaseCreator.HasTables()) { databaseCreator.CreateTables(); } } catch { //continue } } id = staff.InsertOneUser(); } } } catch (Exception e) { Console.WriteLine(e.ToString()); } } app.UseMiddleware(); // Configure the HTTP request pipeline. //if (app.Environment.IsDevelopment())// //{ app.UseSwagger(); app.UseSwaggerUI(); //} // global cors policy app.UseCors(x => x .SetIsOriginAllowed(origin => true) .AllowAnyMethod() .AllowAnyHeader() .WithExposedHeaders("Content-Disposition") .AllowCredentials()); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.MapGraphQL(); app.MapControllers(); app.Run(); #endregion /* Always Always https://www.youtube.com/watch?v=WQFx2m5Ub9M https://csharptotypescript.azurewebsites.net/ \ services.AddDbContext(options => options.UseNpgsql(Configuration.GetConnectionString("BloggingContext"))); dotnet ef dbcontext scaffold "host=postgresdb;port=5432;Database=FamilyTreeDB;Username=postgres;password=Positive~1" Npgsql.EntityFrameworkCore.PostgreSQL -Schemas schema1 --output-dir Models PM> Scaffold-DbContext "Host=postgresdb;Port=5432;database=FamilyTreeDB;user id=postgres;Password=Positive~1" Npgsql.EntityFrameworkCore.PostgreSQL -OutputDir Models */ /* graphql https://www.youtube.com/watch?v=HnXA8RI7Tvc query { persons { nodes{ id firstName sex email } } } dotnet --list-sdks In the VS Code Explorer, click and open your .csproj file. Locate the tag inside the block. Change the value to your desired version (e.g., change net11.0 or net9.0 to net10.0 now run to see out of date dotnet list package --outdated dotnet tool uninstall --global dotnet-outdated dotnet tool install --global dotnet-outdated-tool //update the outdate dotnet outdated -u update the package dotnet add package Open the .vscode folder in your workspace and select launch.json. Look for lines containing your old framework directory string (e.g., /bin/Debug/net8.0/). Update those paths to reflect your new version (e.g., /bin/Debug/net9.0/). clear and rebuild dotnet clean dotnet restore dotnet build */