Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated .NET6 program.cs and startup.cs #424

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/Server/Extensions/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Globalization;
using System.Linq;
using System. Linq;
using BlazorHero.CleanArchitecture.Application.Interfaces.Services;
using BlazorHero.CleanArchitecture.Server.Hubs;
using BlazorHero.CleanArchitecture.Server.Middlewares;
Expand All @@ -12,6 +12,10 @@
using BlazorHero.CleanArchitecture.Shared.Constants.Application;
using BlazorHero.CleanArchitecture.Application.Configurations;
using Microsoft.Extensions.Configuration;
using BlazorHero.CleanArchitecture.Infrastructure.Contexts;
using Microsoft.EntityFrameworkCore;
using System;
using Microsoft.Extensions.Logging;

namespace BlazorHero.CleanArchitecture.Server.Extensions
{
Expand Down Expand Up @@ -82,8 +86,26 @@ internal static IApplicationBuilder Initialize(this IApplicationBuilder app, Mic
{
using var serviceScope = app.ApplicationServices.CreateScope();

var initializers = serviceScope.ServiceProvider.GetServices<IDatabaseSeeder>();
var services = serviceScope.ServiceProvider;

try
{
var context = services.GetRequiredService<BlazorHeroContext>();

if (context.Database.IsSqlServer())
{
context.Database.Migrate();
}
}
catch (Exception ex)
{
var logger = serviceScope.ServiceProvider.GetRequiredService<ILogger<Program>>();

logger.LogError(ex, "An error occurred while migrating or seeding the database.");

throw;
}
var initializers = serviceScope.ServiceProvider.GetServices<IDatabaseSeeder>();
foreach (var initializer in initializers)
{
initializer.Initialize();
Expand Down
56 changes: 8 additions & 48 deletions src/Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,14 @@
using System;
using System.Threading.Tasks;
using BlazorHero.CleanArchitecture.Infrastructure.Contexts;
using BlazorHero.CleanArchitecture.Server.Extensions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using BlazorHero.CleanArchitecture.Server;
using Microsoft.AspNetCore.Builder;

namespace BlazorHero.CleanArchitecture.Server
{
public class Program
{
public async static Task Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
var builder = WebApplication.CreateBuilder(args);

using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
var startup = new Startup(builder.Configuration);

try
{
var context = services.GetRequiredService<BlazorHeroContext>();
startup.ConfigureServices(builder.Services);

if (context.Database.IsSqlServer())
{
context.Database.Migrate();
}
}
catch (Exception ex)
{
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
var app = builder.Build();

logger.LogError(ex, "An error occurred while migrating or seeding the database.");
startup.Configure(app, app.Environment);

throw;
}
}

await host.RunAsync();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStaticWebAssets();
webBuilder.UseStartup<Startup>();
});
}
}
app.Run();
4 changes: 2 additions & 2 deletions src/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddLazyCache();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStringLocalizer<Startup> localizer)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseForwarding(_configuration);
app.UseExceptionHandling(env);
Expand All @@ -86,7 +86,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStringL
app.UseAuthorization();
app.UseHangfireDashboard("/jobs", new DashboardOptions
{
DashboardTitle = localizer["BlazorHero Jobs"],
DashboardTitle = "BlazorHero Jobs",
Authorization = new[] { new HangfireAuthorizationFilter() }
});
app.UseEndpoints();
Expand Down