IServerStartup implementation
This commit is contained in:
26
Oqtane.Server/Extensions/ApplicationBuilderExtensions.cs
Normal file
26
Oqtane.Server/Extensions/ApplicationBuilderExtensions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -31,7 +31,24 @@ namespace System.Reflection
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -3,6 +3,7 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
||||
using Oqtane.Infrastructure;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -53,11 +53,17 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||
services.AddSingleton(hostedServiceType, serviceType);
|
||||
}
|
||||
}
|
||||
|
||||
var startUps = assembly.GetInstances<IServerStartup>();
|
||||
foreach (var startup in startUps)
|
||||
{
|
||||
startup.ConfigureServices(services);
|
||||
}
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
private static void LoadAssemblies()
|
||||
{
|
||||
var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
|
||||
|
Reference in New Issue
Block a user