Blazor Server Template - Refactoring With Extension Method
2022. 5. 13. 00:00ㆍASPNET/BlazorServerTemplate
반응형
Serilog 관련 설정 Refactroing
Extensions 폴더 생성
Extensions/IHostBuilderExtension.cs 생성
using Serilog;
namespace BlazorWebApp.Extensions;
public static class IHostBuilderExtension
{
public static void AddSerilog(this IHostBuilder self)
{
self.UseSerilog((ctx, lc) => lc.ReadFrom.Configuration(ctx.Configuration));
}
}
Program.cs 수정
...
using BlazorWebApp.Extensions;
...
//builder.Host.UseSerilog((context, configuration) => configuration
// .ReadFrom
// .Configuration(context.Configuration));
...
builder.Host.AddSerilog();
MediatR 관련 설정 Refactoring
Extensions 폴더 생성
Extensions/IServiceCollectionExtension.cs 생성
using BlazorWebApp.Behaviors;
using BlazorWebApp.Helper;
using MediatR;
namespace BlazorWebApp.Extensions;
public static class IServiceCollectionExtension
{
public static void InitMediatR(this IServiceCollection self)
{
self.AddMediatR(AssemblyHelper.GetAllAssemblies().ToArray());
self.AddTransient(typeof(IPipelineBehavior<,>), typeof(MediatRLoggingBehavior<,>));
}
}
Program.cs 수정
...
//builder.Services.AddMediatR(AssemblyHelper.GetAllAssemblies().ToArray());
//builder.Services.AddTransient(typeof(IPipelineBehavior<,>), typeof(MediatRLoggingBehavior<,>));
...
builder.Services.InitMediatR();
Scrutor 관련 설정 Refactoring
Extensions/IServiceCollectionExtension.cs 수정
...
using BlazorWebApp.Injectables;
...
public static void InitScrutor(this IServiceCollection self)
{
self.Scan(scan => scan
.FromAssemblies(AssemblyHelper.GetAllAssemblies())
.AddClasses(classes => classes.AssignableTo<ITransientService>())
.AsImplementedInterfaces()
.WithTransientLifetime()
.AddClasses(classes => classes.AssignableTo<IScopedService>())
.AsImplementedInterfaces()
.WithScopedLifetime()
.AddClasses(classes => classes.AssignableTo<ISingletonService>())
.AsImplementedInterfaces()
.WithSingletonLifetime()
);
}
Program.cs 수정
//builder.Services.Scan(scan => scan
// .FromAssemblies(AssemblyHelper.GetAllAssemblies())
// .AddClasses(classes => classes.AssignableTo<ITransientService>())
// .AsImplementedInterfaces()
// .WithTransientLifetime()
// .AddClasses(classes => classes.AssignableTo<IScopedService>())
// .AsImplementedInterfaces()
// .WithScopedLifetime()
// .AddClasses(classes => classes.AssignableTo<ISingletonService>())
// .AsImplementedInterfaces()
// .WithSingletonLifetime()
// );
builder.Services.InitScrutor();
파일정리
Extensions/IHostBuilderExtension.cs
using Serilog;
namespace BlazorWebApp.Extensions;
public static class IHostBuilderExtension
{
public static void AddSerilog(this IHostBuilder self)
{
self.UseSerilog((ctx, lc) => lc.ReadFrom.Configuration(ctx.Configuration));
}
}
Extensions/IServiceCollectionExtension.cs
using BlazorWebApp.Behaviors;
using BlazorWebApp.Helper;
using BlazorWebApp.Injectables;
using MediatR;
namespace BlazorWebApp.Extensions;
public static class IServiceCollectionExtension
{
public static void InitMediatR(this IServiceCollection self)
{
self.AddMediatR(AssemblyHelper.GetAllAssemblies().ToArray());
self.AddTransient(typeof(IPipelineBehavior<,>), typeof(MediatRLoggingBehavior<,>));
}
public static void InitScrutor(this IServiceCollection self)
{
self.Scan(scan => scan
.FromAssemblies(AssemblyHelper.GetAllAssemblies())
.AddClasses(classes => classes.AssignableTo<ITransientService>())
.AsImplementedInterfaces()
.WithTransientLifetime()
.AddClasses(classes => classes.AssignableTo<IScopedService>())
.AsImplementedInterfaces()
.WithScopedLifetime()
.AddClasses(classes => classes.AssignableTo<ISingletonService>())
.AsImplementedInterfaces()
.WithSingletonLifetime()
);
}
}
Program.cs
using BlazorWebApp.Extensions;
using Serilog;
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.Development.json", optional: true)
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateBootstrapLogger();
try
{
var builder = WebApplication.CreateBuilder(args);
builder.Host.AddSerilog();
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.InitMediatR();
builder.Services.InitScrutor();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days.
// You may want to change this for production scenarios,
// see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();
}
catch (Exception ex)
{
string type = ex.GetType().Name;
if (type.Equals("StopTheHostException", StringComparison.OrdinalIgnoreCase)) throw;
Log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
관련영상
반응형
'ASPNET > BlazorServerTemplate' 카테고리의 다른 글
Blazor Server Template - Custom Logger (0) | 2022.05.12 |
---|---|
Blazor Server Template - MediatR (0) | 2022.05.11 |
Blazor Server Template - Scrutor (0) | 2022.05.10 |
Blazor Server Template - Serilog (0) | 2022.05.09 |