Skip to content

Commit

Permalink
Refactor: BlazorServer upgrade to net8.0
Browse files Browse the repository at this point in the history
BlazorServer: Remove Startup

BlazorServer: use InteractiveServerRenderMode with Routes
  • Loading branch information
Xian55 committed Nov 15, 2023
1 parent 9c99388 commit 5cb442c
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 158 deletions.
34 changes: 17 additions & 17 deletions BlazorServer/Pages/_Host.cshtml → BlazorServer/App.razor
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
@page "/"
@namespace BlazorServer.Pages
@using Frontend
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
@using Frontend

@inject IHostEnvironment Env

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>BlazorServer</title>
<base href="~/" />
<base href="/" />

<link rel="stylesheet" href="_content/Frontend/css/bootstrap/bootstrap.icons.min.css" />
<!--<link rel="stylesheet" href="_content/Frontend/css/bootstrap/bootstrap-theme.min.css" />-->
Expand All @@ -38,20 +34,24 @@
</head>
<body>
<app>
<component type="typeof(App)" render-mode="ServerPrerendered" />
<Routes @rendermode="InteractiveServer" />
</app>

<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
@if (Env.IsDevelopment())
{
<text>
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</text>
}
else
{
<text>
An error has occurred. This app may no longer respond until reloaded.
</text>
}
</div>

<script src="_framework/blazor.server.js"></script>
<script src="_framework/blazor.web.js"></script>
</body>
</html>
8 changes: 6 additions & 2 deletions BlazorServer/BlazorServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.3.0" />
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.2" />
<PackageReference Include="Serilog.Expressions" Version="4.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
Expand Down
128 changes: 115 additions & 13 deletions BlazorServer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
using System;
using System.Threading;

using Core;

using Frontend;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

using Serilog;
using Serilog.Events;
using Serilog.Templates.Themes;
using Serilog.Templates;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders;
using System.IO;

namespace BlazorServer;

public static class Program
{
private const string hostUrl = "http://0.0.0.0:5000";

public static void Main(string[] args)
{
while (true)
{
Log.Information($"[{nameof(Program),-15}] Starting blazor server");
try
{
IHost host = CreateHostBuilder(args).Build();
IHost host = CreateApp(args);
var logger = host.Services.GetRequiredService<Microsoft.Extensions.Logging.ILogger>();

AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs args) =>
Expand All @@ -35,18 +45,110 @@ public static void Main(string[] args)
{
Log.Information($"[{nameof(Program),-15}] {ex.Message}");
Log.Information("");
System.Threading.Thread.Sleep(3000);

Thread.Sleep(3000);
}
}
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseUrls(hostUrl);
webBuilder.ConfigureLogging(logging =>
logging.ClearProviders().AddSerilog());
webBuilder.UseStartup<Startup>();
});
private static WebApplication CreateApp(string[] args)
{
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders().AddSerilog();

ConfigureServices(builder.Configuration, builder.Services);

return ConfigureApp(builder, builder.Environment);
}

private static void ConfigureServices(IConfiguration configuration, IServiceCollection services)
{
ILoggerFactory logFactory = LoggerFactory.Create(builder =>
{
builder.ClearProviders().AddSerilog();
});

services.AddLogging(builder =>
{
LoggerSink sink = new();
builder.Services.AddSingleton(sink);

const string outputTemplate = "[{@t:HH:mm:ss:fff} {@l:u1}] {#if Length(SourceContext) > 0}[{Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1),-15}] {#end}{@m}\n{@x}";

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Sink(sink)
.WriteTo.File(new ExpressionTemplate(outputTemplate),
"out.log",
rollingInterval: RollingInterval.Day)
.WriteTo.Debug(new ExpressionTemplate(outputTemplate))
.WriteTo.Console(new ExpressionTemplate(outputTemplate, theme: TemplateTheme.Literate))
.CreateLogger();

builder.Services.AddSingleton<Microsoft.Extensions.Logging.ILogger>(logFactory.CreateLogger(string.Empty));
});

Microsoft.Extensions.Logging.ILogger log = logFactory.CreateLogger("Program");

log.LogInformation(
$"{Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName} " +
$"{DateTimeOffset.Now}");

services.AddStartupConfigurations(configuration);

services.AddWoWProcess(log);

services.AddCoreBase();

if (AddonConfig.Exists() && FrameConfig.Exists())
{
services.AddCoreNormal(log);
}
else
{
services.AddCoreConfiguration(log);
}

services.AddFrontend();

services.AddCoreFrontend();

services.BuildServiceProvider(
new ServiceProviderOptions { ValidateOnBuild = true });
}

private static WebApplication ConfigureApp(WebApplicationBuilder builder, IWebHostEnvironment env)
{
WebApplication app = builder.Build();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}

app.UseStaticFiles();

DataConfig dataConfig = app.Services.GetRequiredService<DataConfig>();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, dataConfig.Path)),
RequestPath = "/path"
});

app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode()
.AddAdditionalAssemblies(typeof(Frontend._Imports).Assembly);

app.UseAntiforgery();

return app;
}

}
123 changes: 0 additions & 123 deletions BlazorServer/Startup.cs

This file was deleted.

2 changes: 2 additions & 0 deletions BlazorServer/_Imports.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@using Microsoft.Extensions.Hosting
@using static Microsoft.AspNetCore.Components.Web.RenderMode
2 changes: 1 addition & 1 deletion BlazorServer/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"Id": -1
},
"Reader": {
"Type": "DXGI" // from Win7 'GDI' - from Win8 'DXGI' - from Win7 background 'GDIBlit'
"Type": "DXGI"
},
"Diagnostics": {
"Enabled": false
Expand Down
5 changes: 5 additions & 0 deletions BlazorServer/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sdk": {
"version": "8.0.100"
}
}
6 changes: 5 additions & 1 deletion Frontend/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ public static class DependencyInjection
public static IServiceCollection AddFrontend(this IServiceCollection services)
{
services.AddMatBlazor();

services.AddRazorPages();
services.AddServerSideBlazor();

services.AddRazorComponents()
.AddInteractiveServerComponents();

services.AddBlazorTable();

return services;
Expand Down
File renamed without changes.
Loading

0 comments on commit 5cb442c

Please sign in to comment.