You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.5 KiB
71 lines
2.5 KiB
2 years ago
|
using System;
|
||
|
using System.Globalization;
|
||
2 years ago
|
using System.Net.Http;
|
||
2 years ago
|
using System.Reflection;
|
||
|
using Microsoft.AspNetCore.Builder;
|
||
2 years ago
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||
|
using Microsoft.Extensions.Configuration;
|
||
2 years ago
|
using Microsoft.Extensions.DependencyInjection;
|
||
|
using Microsoft.Extensions.Hosting;
|
||
|
using Serilog;
|
||
|
using Serilog.Extensions.Hosting;
|
||
|
using Volo.Abp.Modularity;
|
||
|
|
||
|
namespace Win_in.Sfs.Shared.Host;
|
||
|
|
||
2 years ago
|
public static class HostBuilderExtensions
|
||
2 years ago
|
{
|
||
|
public static int BuildAndRun<TModule>(this WebApplicationBuilder builder) where TModule : IAbpModule
|
||
|
{
|
||
|
Serilog.Debugging.SelfLog.Enable(Console.WriteLine);
|
||
|
Log.Logger = new LoggerConfiguration()
|
||
|
.Enrich.FromLogContext()
|
||
|
.WriteTo.Console(formatProvider: CultureInfo.InvariantCulture)
|
||
|
.CreateBootstrapLogger();
|
||
|
|
||
|
try
|
||
|
{
|
||
|
Log.Information($"Starting web host ${Assembly.GetEntryAssembly().GetName().Name}");
|
||
2 years ago
|
builder.Host.ConfigureAppConfiguration((hc, cb) =>
|
||
|
{
|
||
|
var configUrl = cb.Build().GetValue("", "http://localhost:21093/settings/").TrimEnd('/');
|
||
|
AddJsonByUrl(cb, $"{configUrl}/appsettings.json");
|
||
|
if (builder.Environment.IsDevelopment())
|
||
2 years ago
|
{
|
||
2 years ago
|
AddJsonByUrl(cb, $"{configUrl}/appsettings.{builder.Environment.EnvironmentName}.json");
|
||
|
}
|
||
|
});
|
||
|
builder.Host.UseAutofac();
|
||
2 years ago
|
builder.AddApplicationAsync<TModule>().Wait();
|
||
|
builder.Host.UseSerilog((hostingContext, services, configBuilder) =>
|
||
|
{
|
||
2 years ago
|
configBuilder
|
||
|
.ReadFrom.Configuration(hostingContext.Configuration)
|
||
|
.ReadFrom.Services(services)
|
||
|
.Enrich.FromLogContext()
|
||
|
.WriteTo.Console(formatProvider: CultureInfo.InvariantCulture);
|
||
2 years ago
|
}, writeToProviders: false);
|
||
|
var app = builder.Build();
|
||
|
app.UseSerilogRequestLogging();
|
||
|
app.InitializeApplicationAsync().Wait();
|
||
|
app.RunAsync().Wait();
|
||
|
return 0;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
Log.Fatal(ex, "Host terminated unexpectedly!");
|
||
|
return 1;
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
Log.CloseAndFlush();
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
|
private static void AddJsonByUrl(IConfigurationBuilder configurationBuilder, string url)
|
||
|
{
|
||
|
var stream = new HttpClient().GetStreamAsync(url).Result;
|
||
|
configurationBuilder.AddJsonStream(stream);
|
||
|
}
|
||
2 years ago
|
}
|