|
|
|
@ -13,6 +13,7 @@ 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;
|
|
|
|
@ -25,14 +26,39 @@ namespace TestServer
|
|
|
|
|
public IWebHostEnvironment Env { get; }
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
private readonly string _origins = "AllowAllHeaders";
|
|
|
|
|
private List<Type> _myMiddlewares = new List<Type>();
|
|
|
|
|
private List<Type> _startups = new List<Type>();
|
|
|
|
|
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)
|
|
|
|
@ -50,16 +76,25 @@ namespace TestServer
|
|
|
|
|
services.AddSingleton(MyActionDescriptorChangeProvider.Instance);
|
|
|
|
|
services.AddSingleton<IActionDescriptorChangeProvider>(MyActionDescriptorChangeProvider.Instance);
|
|
|
|
|
services.AddHttpClient();
|
|
|
|
|
services.AddSignalR(o => o.EnableDetailedErrors = true).AddJsonProtocol();
|
|
|
|
|
services.AddControllersWithViews()
|
|
|
|
|
.AddNewtonsoftJson(o =>
|
|
|
|
|
{
|
|
|
|
|
o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
|
|
|
|
})
|
|
|
|
|
.ConfigureApplicationPartManager(ConfigureApplicationParts);
|
|
|
|
|
foreach (var item in _startups)
|
|
|
|
|
foreach (var assembly in this.assemblyList)
|
|
|
|
|
{
|
|
|
|
|
(item.Assembly.CreateInstance(item.FullName) as IStartup).ConfigureServices(services);
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -85,13 +120,23 @@ namespace TestServer
|
|
|
|
|
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
app.UseCors(_origins);
|
|
|
|
|
foreach (var item in _startups)
|
|
|
|
|
{
|
|
|
|
|
(item.Assembly.CreateInstance(item.FullName) as IStartup).Configure(app);
|
|
|
|
|
}
|
|
|
|
|
foreach (var item in this._myMiddlewares)
|
|
|
|
|
foreach (var assembly in this.assemblyList)
|
|
|
|
|
{
|
|
|
|
|
app.UseMiddleware(item);
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
@ -105,12 +150,6 @@ namespace TestServer
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
|
{
|
|
|
|
|
//endpoints.MapHub<PageHub>("/hub", o =>
|
|
|
|
|
//{
|
|
|
|
|
// o.ApplicationMaxBufferSize = long.MaxValue;
|
|
|
|
|
// o.TransportMaxBufferSize = long.MaxValue;
|
|
|
|
|
//});
|
|
|
|
|
|
|
|
|
|
endpoints.MapControllerRoute(
|
|
|
|
|
name: "areas",
|
|
|
|
|
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
|
|
|
|
@ -124,27 +163,17 @@ namespace TestServer
|
|
|
|
|
private void ConfigureApplicationParts(ApplicationPartManager apm)
|
|
|
|
|
{
|
|
|
|
|
Log.Logger.Information("Startup>ConfigureApplicationParts");
|
|
|
|
|
var pluginsPath = Path.Combine(this.Env.WebRootPath, "plugin", "copy");
|
|
|
|
|
if (!Directory.Exists(pluginsPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(pluginsPath);
|
|
|
|
|
}
|
|
|
|
|
var assemblyFiles = Directory.GetFiles(pluginsPath, "*.dll", SearchOption.AllDirectories);
|
|
|
|
|
foreach (var assemblyFile in assemblyFiles)
|
|
|
|
|
foreach (var assembly in assemblyList)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var fs = new FileStream(assemblyFile, FileMode.Open);
|
|
|
|
|
var assembly = new AssemblyLoadContext(assemblyFile, true).LoadFromStream(fs);
|
|
|
|
|
if (assemblyFile.EndsWith(".Views.dll"))
|
|
|
|
|
if (assembly.Key.EndsWith(".Views.dll"))
|
|
|
|
|
{
|
|
|
|
|
apm.ApplicationParts.Add(new MyCompiledRazorAssemblyPart(assembly));
|
|
|
|
|
apm.ApplicationParts.Add(new MyCompiledRazorAssemblyPart(assembly.Value));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
apm.ApplicationParts.Add(new MyAssemblyPart(assembly));
|
|
|
|
|
this._myMiddlewares.AddRange(assembly.GetTypes().Where(o => o.Name.EndsWith("Middleware")));
|
|
|
|
|
this._startups.AddRange(assembly.GetTypes().Where(o => typeof(IStartup).IsAssignableFrom(o)));
|
|
|
|
|
apm.ApplicationParts.Add(new MyAssemblyPart(assembly.Value));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|