IServerStartup implementation

This commit is contained in:
Pavel Vesely 2020-05-11 11:05:17 +02:00
parent 552df0c9fc
commit 7c814a67b3
7 changed files with 91 additions and 9 deletions

View File

@ -9,7 +9,7 @@ using Oqtane.UI;
namespace Oqtane.Modules namespace Oqtane.Modules
{ {
public class ModuleBase : ComponentBase, IModuleControl public abstract class ModuleBase : ComponentBase, IModuleControl
{ {
private Logger _logger; private Logger _logger;

View File

@ -0,0 +1,26 @@
using System;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Oqtane.Infrastructure;
namespace Oqtane.Extensions
{
public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder ConfigureOqtaneAssemblies(this IApplicationBuilder app, IWebHostEnvironment env)
{
var startUps = AppDomain.CurrentDomain
.GetOqtaneAssemblies()
.SelectMany(x => x.GetInstances<IServerStartup>());
foreach (var startup in startUps)
{
startup.Configure(app, env);
}
return app;
}
}
}

View File

@ -31,7 +31,24 @@ namespace System.Reflection
} }
return assembly.GetTypes() return assembly.GetTypes()
.Where(t => t.GetInterfaces().Contains(interfaceType)); //.Where(t => t.GetInterfaces().Contains(interfaceType));
.Where(x => interfaceType.IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract);
}
public static IEnumerable<T> GetInstances<T>(this Assembly assembly) where T : class
{
if (assembly is null)
{
throw new ArgumentNullException(nameof(assembly));
}
var type = typeof(T);
var list = assembly.GetTypes()
.Where(x => type.IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract && !x.IsGenericType);
foreach (var type1 in list)
{
if (Activator.CreateInstance(type1) is T instance) yield return instance;
}
} }
public static bool IsOqtaneAssembly(this Assembly assembly) public static bool IsOqtaneAssembly(this Assembly assembly)

View File

@ -3,6 +3,7 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Oqtane.Infrastructure;
// ReSharper disable once CheckNamespace // ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection namespace Microsoft.Extensions.DependencyInjection
@ -30,6 +31,22 @@ namespace Microsoft.Extensions.DependencyInjection
} }
} }
} }
return mvcBuilder;
}
public static IMvcBuilder ConfigureOqtaneMvc(this IMvcBuilder mvcBuilder)
{
var startUps = AppDomain.CurrentDomain
.GetOqtaneAssemblies()
.SelectMany(x => x.GetInstances<IServerStartup>());
foreach (var startup in startUps)
{
startup.ConfigureMvc(mvcBuilder);
}
return mvcBuilder; return mvcBuilder;
} }
} }

View File

@ -53,11 +53,17 @@ namespace Microsoft.Extensions.DependencyInjection
services.AddSingleton(hostedServiceType, serviceType); services.AddSingleton(hostedServiceType, serviceType);
} }
} }
}
var startUps = assembly.GetInstances<IServerStartup>();
foreach (var startup in startUps)
{
startup.ConfigureServices(services);
}
}
return services; return services;
} }
private static void LoadAssemblies() private static void LoadAssemblies()
{ {
var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location); var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);

View File

@ -0,0 +1,17 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace Oqtane.Infrastructure
{
public interface IServerStartup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
void ConfigureServices(IServiceCollection services);
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
void Configure(IApplicationBuilder app, IWebHostEnvironment env);
void ConfigureMvc(IMvcBuilder mvcBuilder);
}
}

View File

@ -14,6 +14,7 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using Oqtane.Extensions;
using Oqtane.Infrastructure; using Oqtane.Infrastructure;
using Oqtane.Repository; using Oqtane.Repository;
using Oqtane.Security; using Oqtane.Security;
@ -187,14 +188,13 @@ namespace Oqtane
services.AddTransient<ISqlRepository, SqlRepository>(); services.AddTransient<ISqlRepository, SqlRepository>();
services.AddTransient<IUpgradeManager, UpgradeManager>(); services.AddTransient<IUpgradeManager, UpgradeManager>();
// load the external assemblies into the app domain // load the external assemblies into the app domain, install services
services.AddOqtaneParts(); services.AddOqtaneParts();
services.AddMvc() services.AddMvc()
.AddNewtonsoftJson()
.AddOqtaneApplicationParts() // register any Controllers from custom modules .AddOqtaneApplicationParts() // register any Controllers from custom modules
.AddNewtonsoftJson(); .ConfigureOqtaneMvc(); // any additional configuration from IStart classes.
services.AddSwaggerGen(c => services.AddSwaggerGen(c =>
{ {
@ -217,14 +217,12 @@ namespace Oqtane
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. // 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.UseHsts();
} }
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseStaticFiles(); app.UseStaticFiles();
app.UseBlazorFrameworkFiles(); app.UseBlazorFrameworkFiles();
app.UseRouting(); app.UseRouting();
app.UseAuthentication(); app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(c => app.UseSwaggerUI(c =>
{ {
@ -237,6 +235,7 @@ namespace Oqtane
endpoints.MapControllers(); endpoints.MapControllers();
endpoints.MapFallbackToPage("/_Host"); endpoints.MapFallbackToPage("/_Host");
}); });
app.ConfigureOqtaneAssemblies(env);
} }
} }
} }