diff --git a/Finch/Configuration/FinchStartupOptions.cs b/Finch/Configuration/FinchStartupOptions.cs index b0966ecf..07068001 100644 --- a/Finch/Configuration/FinchStartupOptions.cs +++ b/Finch/Configuration/FinchStartupOptions.cs @@ -2,17 +2,11 @@ namespace Finch.Configuration; -public class FinchStartupOptions : IFinchStartupOptions +public class FinchStartupOptions(IMvcBuilder mvc) : IFinchStartupOptions { public IList AssemblyDiscoveryRules { get; } = new List(); - public IMvcBuilder Mvc { get; } - - - public FinchStartupOptions(IMvcBuilder mvc) - { - Mvc = mvc; - } + public IMvcBuilder Mvc { get; } = mvc; } public interface IFinchStartupOptions diff --git a/Finch/Context/FinchContextMiddleware.cs b/Finch/Context/FinchContextMiddleware.cs index 8d66dfa0..f324d3b9 100644 --- a/Finch/Context/FinchContextMiddleware.cs +++ b/Finch/Context/FinchContextMiddleware.cs @@ -2,19 +2,12 @@ namespace Finch.Context { - public class FinchContextMiddleware + public class FinchContextMiddleware(RequestDelegate next) { - RequestDelegate _next; - - public FinchContextMiddleware(RequestDelegate next) - { - _next = next; - } - public async Task Invoke(HttpContext httpContext, IFinchContext finchContext) { await finchContext.Resolve(httpContext); - await _next(httpContext); + await next(httpContext); } } } diff --git a/Finch/FinchBuilder.cs b/Finch/FinchBuilder.cs index a5c2df74..64e66bde 100644 --- a/Finch/FinchBuilder.cs +++ b/Finch/FinchBuilder.cs @@ -18,53 +18,52 @@ public class FinchBuilder { public virtual IServiceCollection Services { get; } - public virtual IMvcBuilder Mvc { get; } - internal static FinchModuleCollection Modules { get; } = new(); readonly IConfiguration _configuration; - readonly IFinchStartupOptions _startupOptions; public FinchBuilder(IServiceCollection services, IConfiguration configuration, Action setupAction) { Services = services; - Mvc = services.AddMvc(); _configuration = configuration; - // create startup options - _startupOptions = new FinchStartupOptions(Mvc); - _startupOptions.AssemblyDiscoveryRules.Add(new FinchAssemblyDiscoveryRule()); - setupAction?.Invoke(_startupOptions); + bool isWeb = services.Any(s => s.ServiceType.FullName == "Microsoft.AspNetCore.Hosting.IWebHostEnvironment");; + + if (isWeb) + { + IMvcBuilder mvcBuilder = services.AddMvc(); + // create startup options + IFinchStartupOptions startupOptions = new FinchStartupOptions(mvcBuilder); + startupOptions.AssemblyDiscoveryRules.Add(new FinchAssemblyDiscoveryRule()); + setupAction?.Invoke(startupOptions); + + services.AddControllers(); + services.AddOutputCache(); + mvcBuilder = services.AddRazorPages(); + + mvcBuilder.AddDataAnnotationsLocalization(); + + services.Configure(opts => opts.Cookie.Name = "finch.antiforgery"); + + // adds and discovers additional and built-in assemblies + new AssemblyDiscovery(mvcBuilder).Execute(startupOptions.AssemblyDiscoveryRules); + + Modules.Add(); + } //string appName = configuration.GetValue("Finch:AppName").Or("finch-app"); //services.AddDataProtection();.PersistKeysToRegistry() - services.AddControllers(); - services.AddOutputCache(); - Mvc = services.AddRazorPages(); - - Mvc.AddDataAnnotationsLocalization(); - - services.Configure(opts => opts.Cookie.Name = "finch.antiforgery"); - - - // adds and discovers additional and built-in assemblies - new AssemblyDiscovery(Mvc).Execute(_startupOptions.AssemblyDiscoveryRules); - Modules.Add(); Modules.Add(); - Modules.Add(); Modules.Add(); Modules.Add(); Modules.Add(); Modules.Add(); - //Modules.Add(); Modules.Add(); Modules.Add(); - //Modules.Add(); Modules.Add(); - //Modules.Add(); Modules.Add(); Modules.Add(); Modules.Add();