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.
186 lines
7.2 KiB
186 lines
7.2 KiB
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Hosting.Server.Features;
|
|
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
|
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using Serilog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Runtime.Loader;
|
|
using System.Text.Encodings.Web;
|
|
using System.Text.Unicode;
|
|
using WinFormNet5;
|
|
|
|
namespace TestServer
|
|
{
|
|
public class Startup
|
|
{
|
|
public IWebHostEnvironment Env { get; }
|
|
public IConfiguration Configuration { get; }
|
|
private readonly string _origins = "AllowAllHeaders";
|
|
private string _url;
|
|
private Dictionary<string, Assembly> assemblyList = new Dictionary<string, Assembly>();
|
|
|
|
public Startup(IWebHostEnvironment env, IConfiguration configuration)
|
|
{
|
|
this.Env = env;
|
|
this.Configuration = configuration;
|
|
this.Init();
|
|
}
|
|
|
|
private void Init()
|
|
{
|
|
var pluginsPath = Path.Combine(this.Env.WebRootPath, "plugin");
|
|
var pluginsPathCopy = Path.Combine(this.Env.WebRootPath, "plugin", "copy");
|
|
Directory.CreateDirectory(pluginsPath);
|
|
Directory.CreateDirectory(pluginsPathCopy);
|
|
var files = Directory.GetFiles(pluginsPath);
|
|
foreach (var file in files)
|
|
{
|
|
var fileName = Path.GetFileName(file);
|
|
var copyFile = Path.Combine(pluginsPathCopy, fileName);
|
|
if (!File.Exists(copyFile) || file.Md5() != copyFile.Md5())
|
|
{
|
|
File.Copy(file, copyFile, true);
|
|
}
|
|
}
|
|
var assemblyFiles = Directory.GetFiles(pluginsPathCopy, "*.dll", SearchOption.AllDirectories);
|
|
foreach (var file in assemblyFiles)
|
|
{
|
|
using var fs = new FileStream(file, FileMode.Open);
|
|
var assembly = new AssemblyLoadContext(file, true).LoadFromStream(fs);
|
|
this.assemblyList.Add(file,assembly);
|
|
}
|
|
}
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
Log.Logger.Information("Startup>ConfigureServices");
|
|
services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All));
|
|
services.AddCors(options => options.AddPolicy(_origins,
|
|
builder =>
|
|
{
|
|
builder.SetIsOriginAllowed(o => true)
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader()
|
|
.AllowCredentials();
|
|
}));
|
|
services.AddSingleton(MyActionDescriptorChangeProvider.Instance);
|
|
services.AddSingleton<IActionDescriptorChangeProvider>(MyActionDescriptorChangeProvider.Instance);
|
|
services.AddHttpClient();
|
|
services.AddControllersWithViews()
|
|
.AddNewtonsoftJson(o =>
|
|
{
|
|
o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
|
})
|
|
.ConfigureApplicationPartManager(ConfigureApplicationParts);
|
|
foreach (var assembly in this.assemblyList)
|
|
{
|
|
try
|
|
{
|
|
assembly.Value.GetTypes()
|
|
.Where(o => typeof(IStartup).IsAssignableFrom(o))
|
|
.ToList()
|
|
.ForEach(o => (assembly.Value.CreateInstance(o.FullName) as IStartup).ConfigureServices(services));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex.ToString());
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Configure(IApplicationBuilder app, IHostApplicationLifetime lifetime, ILogger<Startup> logger)
|
|
{
|
|
logger.LogInformation("Startup->Configure");
|
|
|
|
app.UseSerilogRequestLogging();
|
|
|
|
lifetime.ApplicationStarted.Register(() =>
|
|
{
|
|
this._url = app.ServerFeatures.Get<IServerAddressesFeature>().Addresses.FirstOrDefault();
|
|
logger.LogInformation($"ApplicationStarted {this._url}");
|
|
//Program.MainForm.BeginInvoke(new EventHandler(delegate {
|
|
// Program.MainForm.SetUrl(this._url, Env.WebRootPath);
|
|
//}));
|
|
});
|
|
|
|
lifetime.ApplicationStopped.Register(() =>
|
|
{
|
|
logger.LogInformation($"ApplicationStopped {this._url}");
|
|
});
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
app.UseCors(_origins);
|
|
foreach (var assembly in this.assemblyList)
|
|
{
|
|
try
|
|
{
|
|
assembly.Value.GetTypes()
|
|
.Where(o => typeof(IStartup).IsAssignableFrom(o))
|
|
.ToList()
|
|
.ForEach(o => (assembly.Value.CreateInstance(o.FullName) as IStartup).Configure(app));
|
|
assembly.Value.GetTypes()
|
|
.Where(o => o.Name.EndsWith("Middleware"))
|
|
.ToList()
|
|
.ForEach(o => app.UseMiddleware(o));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex.ToString());
|
|
}
|
|
}
|
|
app.UseStaticFiles(new StaticFileOptions
|
|
{
|
|
ServeUnknownFileTypes = true,
|
|
//ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>{
|
|
// { ".dat","video/webm"}
|
|
//})
|
|
});
|
|
app.UseFileServer();
|
|
app.UseRouting();
|
|
app.UseAuthorization();
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllerRoute(
|
|
name: "areas",
|
|
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
|
|
|
|
endpoints.MapControllerRoute(
|
|
name: "default",
|
|
pattern: "{controller=Home}/{action=Index}/{id?}");
|
|
});
|
|
}
|
|
|
|
private void ConfigureApplicationParts(ApplicationPartManager apm)
|
|
{
|
|
Log.Logger.Information("Startup>ConfigureApplicationParts");
|
|
foreach (var assembly in assemblyList)
|
|
{
|
|
try
|
|
{
|
|
if (assembly.Key.EndsWith(".Views.dll"))
|
|
{
|
|
apm.ApplicationParts.Add(new MyCompiledRazorAssemblyPart(assembly.Value));
|
|
}
|
|
else
|
|
{
|
|
apm.ApplicationParts.Add(new MyAssemblyPart(assembly.Value));
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex.ToString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |