From bf2bdec819e50e64f8b75f9d7dbba95f0fd49ef0 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 1 Oct 2024 12:03:04 -0700 Subject: [PATCH] template: misc minor improvements --- .../Coalesce.Starter.Vue.Data/AppDbContext.cs | 2 +- .../Coalesce.Starter.Vue.Web/Program.cs | 110 +++++++++--------- 2 files changed, 56 insertions(+), 56 deletions(-) diff --git a/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data/AppDbContext.cs b/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data/AppDbContext.cs index 19acaeabd..141b8d5bf 100644 --- a/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data/AppDbContext.cs +++ b/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data/AppDbContext.cs @@ -116,7 +116,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) .FormatType(ShaString) .Exclude() #if TrackingBase - .ExcludeProperty(x => new { x.CreatedBy, x.CreatedById, x.CreatedOn, x.ModifiedBy, x.ModifiedById, x.ModifiedOn }) + .ExcludeProperty(x => new { x.CreatedById, x.CreatedOn, x.ModifiedById, x.ModifiedOn }) #endif #if Identity .Format(x => x.PasswordHash, x => "") diff --git a/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Web/Program.cs b/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Web/Program.cs index b533df061..f9405195a 100644 --- a/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Web/Program.cs +++ b/templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Web/Program.cs @@ -27,20 +27,20 @@ var builder = WebApplication.CreateBuilder(new WebApplicationOptions { - Args = args, - // Explicit declaration prevents ASP.NET Core from erroring if wwwroot doesn't exist at startup: - WebRootPath = "wwwroot" + Args = args, + // Explicit declaration prevents ASP.NET Core from erroring if wwwroot doesn't exist at startup: + WebRootPath = "wwwroot" }); builder.Logging - .AddConsole() - // Filter out Request Starting/Request Finished noise: - .AddFilter("Microsoft.AspNetCore.Hosting.Diagnostics", LogLevel.Warning); + .AddConsole() + // Filter out Request Starting/Request Finished noise: + .AddFilter("Microsoft.AspNetCore.Hosting.Diagnostics", LogLevel.Warning); builder.Configuration - .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) - .AddJsonFile("appsettings.localhost.json", optional: true, reloadOnChange: true) - .AddEnvironmentVariables(); + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .AddJsonFile("appsettings.localhost.json", optional: true, reloadOnChange: true) + .AddEnvironmentVariables(); #region Configure Services @@ -49,12 +49,12 @@ #if AppInsights services.AddApplicationInsightsTelemetry(b => { - b.ConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"]; + b.ConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"]; }); services.AddSingleton(); services.ConfigureTelemetryModule((module, o) => { - module.EnableSqlCommandTextInstrumentation = true; + module.EnableSqlCommandTextInstrumentation = true; }); // App insights filters all logs to Warning by default. We want to include our own logging. builder.Logging.AddFilter("Coalesce.Starter.Vue", LogLevel.Information); @@ -62,27 +62,27 @@ services.AddDbContext(options => options - .UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"), opt => opt - .EnableRetryOnFailure() - .UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery) - ) - // Ignored because it interferes with the construction of Coalesce IncludeTrees via .Include() - .ConfigureWarnings(warnings => warnings.Ignore(CoreEventId.NavigationBaseIncludeIgnored)) + .UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"), opt => opt + .EnableRetryOnFailure() + .UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery) + ) + // Ignored because it interferes with the construction of Coalesce IncludeTrees via .Include() + .ConfigureWarnings(warnings => warnings.Ignore(CoreEventId.NavigationBaseIncludeIgnored)) ); services.AddCoalesce(); services.AddDataProtection() - .PersistKeysToDbContext(); + .PersistKeysToDbContext(); services - .AddMvc() - .AddJsonOptions(options => - { - options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; - options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; - options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; - }); + .AddMvc() + .AddJsonOptions(options => + { + options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; + options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; + options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; + }); #if Identity builder.ConfigureAuthentication(); @@ -91,8 +91,8 @@ #if OpenAPI services.AddSwaggerGen(c => { - c.AddCoalesce(); - c.SwaggerDoc("current", new OpenApiInfo { Title = "Current API", Version = "current" }); + c.AddCoalesce(); + c.SwaggerDoc("current", new OpenApiInfo { Title = "Current API", Version = "current" }); }); #endif @@ -104,9 +104,9 @@ services.AddSingleton(); services.AddScoped(x => { - var actionContext = x.GetRequiredService().ActionContext; - var factory = x.GetRequiredService(); - return factory.GetUrlHelper(actionContext!); + var actionContext = x.GetRequiredService().ActionContext; + var factory = x.GetRequiredService(); + return factory.GetUrlHelper(actionContext!); }); services.AddScoped(); @@ -121,14 +121,14 @@ if (app.Environment.IsDevelopment()) { - app.UseDeveloperExceptionPage(); + app.UseDeveloperExceptionPage(); - app.UseViteDevelopmentServer(c => - { - c.DevServerPort = 5002; - }); + app.UseViteDevelopmentServer(c => + { + c.DevServerPort = 5002; + }); - app.MapCoalesceSecurityOverview("coalesce-security"); + app.MapCoalesceSecurityOverview("coalesce-security"); #if (!Identity) // TODO: Dummy authentication for initial development. @@ -155,30 +155,30 @@ var containsFileHashRegex = new Regex(@"[.-][0-9a-zA-Z-_]{8}\.[^\.]*$", RegexOptions.Compiled); app.UseStaticFiles(new StaticFileOptions { - OnPrepareResponse = ctx => - { - // vite puts 8-char hashes before the file extension. - // Use this to determine if we can send a long-term cache duration. - if (containsFileHashRegex.IsMatch(ctx.File.Name)) - { - ctx.Context.Response.GetTypedHeaders().CacheControl = new() { Public = true, MaxAge = TimeSpan.FromDays(30) }; - } - } + OnPrepareResponse = ctx => + { + // vite puts 8-char hashes before the file extension. + // Use this to determine if we can send a long-term cache duration. + if (containsFileHashRegex.IsMatch(ctx.File.Name)) + { + ctx.Context.Response.GetTypedHeaders().CacheControl = new() { Public = true, MaxAge = TimeSpan.FromDays(30) }; + } + } }); // For all requests that aren't to static files, disallow caching by default. // Individual endpoints may override this. app.Use(async (context, next) => { - context.Response.GetTypedHeaders().CacheControl = new() { NoCache = true, NoStore = true }; - await next(); + context.Response.GetTypedHeaders().CacheControl = new() { NoCache = true, NoStore = true }; + await next(); }); #if OpenAPI app.MapSwagger(); app.UseSwaggerUI(c => { - c.SwaggerEndpoint("/swagger/current/swagger.json", "Current API"); + c.SwaggerEndpoint("/swagger/current/swagger.json", "Current API"); }); #endif @@ -197,18 +197,18 @@ // Initialize/migrate database. using (var scope = app.Services.CreateScope()) { - var serviceScope = scope.ServiceProvider; + var serviceScope = scope.ServiceProvider; - // Run database migrations. - using var db = serviceScope.GetRequiredService(); - db.Database.SetCommandTimeout(TimeSpan.FromMinutes(10)); + // Run database migrations. + using var db = serviceScope.GetRequiredService(); + db.Database.SetCommandTimeout(TimeSpan.FromMinutes(10)); #if KeepTemplateOnly - db.Database.EnsureDeleted(); - db.Database.EnsureCreated(); + db.Database.EnsureDeleted(); + db.Database.EnsureCreated(); #else db.Database.Migrate(); #endif - new DatabaseSeeder(db).Seed(); + ActivatorUtilities.GetServiceOrCreateInstance(serviceScope).Seed(); } app.Run();