From 6efec4c2ea88535f39a65ce202cae0e31738ad03 Mon Sep 17 00:00:00 2001 From: Thomas Fuchs Date: Thu, 21 Sep 2023 19:31:24 +0200 Subject: [PATCH 1/2] Remove Startup.cs --- .../ExamplePolicyProvider.cs | 27 ++++ src/MyApplication.App/Program.cs | 107 ++++++++++++--- .../Properties/launchSettings.json | 2 +- src/MyApplication.App/Startup.cs | 125 ------------------ 4 files changed, 119 insertions(+), 142 deletions(-) create mode 100644 src/MyApplication.App/ExamplePolicyProvider.cs delete mode 100644 src/MyApplication.App/Startup.cs diff --git a/src/MyApplication.App/ExamplePolicyProvider.cs b/src/MyApplication.App/ExamplePolicyProvider.cs new file mode 100644 index 0000000..d2d64bb --- /dev/null +++ b/src/MyApplication.App/ExamplePolicyProvider.cs @@ -0,0 +1,27 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.Extensions.Options; +using System.Threading.Tasks; + +namespace MyApplication.App +{ + public class ExamplePolicyProvider : DefaultAuthorizationPolicyProvider + { + public ExamplePolicyProvider(IOptions options) : base(options) + { + } + + public override async Task GetPolicyAsync(string policyName) + { + var policy = await base.GetPolicyAsync(policyName); + + if (policy == null) + { + policy = new AuthorizationPolicyBuilder() + .RequireClaim("Permission", policyName) + .Build(); + } + return policy; + } + } + +} \ No newline at end of file diff --git a/src/MyApplication.App/Program.cs b/src/MyApplication.App/Program.cs index f81ccf8..ba4e32a 100644 --- a/src/MyApplication.App/Program.cs +++ b/src/MyApplication.App/Program.cs @@ -1,10 +1,16 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Moryx.Asp.Integration; using Moryx.Model; using Moryx.Runtime.Kernel; using Moryx.Tools; +using System.Globalization; using System.IO; +using System.Text.Json.Serialization; namespace MyApplication.App { @@ -14,26 +20,95 @@ public static void Main(string[] args) { AppDomainBuilder.LoadAssemblies(); - var host = Host.CreateDefaultBuilder(args) - .ConfigureServices(serviceCollection => - { - serviceCollection.AddMoryxKernel(); - serviceCollection.AddMoryxModels(); - serviceCollection.AddMoryxModules(); - }) - .ConfigureWebHostDefaults(webBuilder => + var builder = WebApplication.CreateBuilder(); + var services = builder.Services; + + // Setup MORYX + services.AddMoryxKernel(); + services.AddMoryxModels(); + services.AddMoryxModules(); + + #region Startup ConfigureServices + services.AddSingleton(); + + services.AddLocalization(); + services.Configure(options => + { + var supportedCultures = new[] { - webBuilder.UseStartup(); - webBuilder.UseIISIntegration(); - }) - .Build(); + new CultureInfo("de-De"), + new CultureInfo("en-De"), + new CultureInfo("it-De"), + new CultureInfo("zh-Hans"), + }; + + options.SupportedCultures = supportedCultures; + options.SupportedUICultures = supportedCultures; + }); + + + services.AddCors(options => + { + options.AddPolicy("CorsPolicy", builder => builder + .WithOrigins("http://localhost:4200") // Angular app url for testing purposes + .AllowAnyMethod() + .AllowAnyHeader() + .AllowCredentials()); + }); + + services.AddRazorPages(); + + services.AddControllers() + .AddJsonOptions(jo => jo.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter())); + + services.AddSwaggerGen(c => + { + c.CustomOperationIds(api => ((ControllerActionDescriptor)api.ActionDescriptor).MethodInfo.Name); + c.CustomSchemaIds(type => type.ToString()); + }); + #endregion + + var app = builder.Build(); + var env = app.Environment; + + #region Startup Configure App + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + + app.UseSwagger(); + app.UseSwaggerUI(); + } + + app.UseRequestLocalization(); + + app.UseStaticFiles(); + + app.UseHttpsRedirection(); + + app.UseRouting(); + + if (env.IsDevelopment()) + app.UseCors("CorsPolicy"); + + app.UseAuthentication(); + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + var conventionBuilder = endpoints.MapControllers(); + conventionBuilder.WithMetadata(new AllowAnonymousAttribute()); + + endpoints.MapRazorPages(); + }); + #endregion - host.Services.UseMoryxConfigurations("Config"); - host.Services.StartMoryxModules(); + app.Services.UseMoryxConfigurations("Config"); + app.Services.StartMoryxModules(); - host.Run(); + app.Run(); - host.Services.StopMoryxModules(); + app.Services.StopMoryxModules(); } } } diff --git a/src/MyApplication.App/Properties/launchSettings.json b/src/MyApplication.App/Properties/launchSettings.json index 533d5de..465e535 100644 --- a/src/MyApplication.App/Properties/launchSettings.json +++ b/src/MyApplication.App/Properties/launchSettings.json @@ -6,7 +6,7 @@ "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, - "applicationUrl": "https://localhost:60572;http://localhost:60573" + "applicationUrl": "https://localhost:5000" } } } \ No newline at end of file diff --git a/src/MyApplication.App/Startup.cs b/src/MyApplication.App/Startup.cs deleted file mode 100644 index 44f0241..0000000 --- a/src/MyApplication.App/Startup.cs +++ /dev/null @@ -1,125 +0,0 @@ -using System.Text.Json.Serialization; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc.Controllers; -using Microsoft.Extensions.Hosting; -using Microsoft.AspNetCore.Authorization; -using Microsoft.Extensions.Options; -using System.Threading.Tasks; -using System.Globalization; - -namespace MyApplication.App -{ - // ASP.NET Core apps use a Startup class, which is named Startup by convention. The Startup class includes - // a Configure() method to create the app's request processing pipeline. It can also include an optional - // ConfigureServices() method to configure the app's services. A "service" is a reusable component that provides - // app functionality. Services are registered in ConfigureServices and consumed across the app via dependency - // injection (DI) or ApplicationServices. ConfigureServices() and Configure() are called by the ASP.NET Core - // runtime when the application starts. See this link for more information: commment - // https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup?view=aspnetcore-3.1 - public class Startup - { - // ConfigureServices() is called by the host before the Configure() method and will configure the app's - // services. By convention, this where configuration options are set, and where services are added the container. - // This method is optional for the Startup class. - public void ConfigureServices(IServiceCollection services) - { - services.AddSingleton(); - - services.AddLocalization(); - services.Configure(options => - { - var supportedCultures = new[] - { - new CultureInfo("de-De"), - new CultureInfo("en-De"), - new CultureInfo("it-De"), - new CultureInfo("zh-Hans"), - }; - - options.SupportedCultures = supportedCultures; - options.SupportedUICultures = supportedCultures; - }); - - - services.AddCors(options => - { - options.AddPolicy("CorsPolicy", builder => builder - .WithOrigins("http://localhost:4200") // Angular app url for testing purposes - .AllowAnyMethod() - .AllowAnyHeader() - .AllowCredentials()); - }); - - services.AddRazorPages(); - - services.AddControllers() - .AddJsonOptions(jo => jo.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter())); - - services.AddSwaggerGen(c => - { - c.CustomOperationIds(api => ((ControllerActionDescriptor)api.ActionDescriptor).MethodInfo.Name); - c.CustomSchemaIds(type => type.ToString()); - }); - } - - // Configure() is used to specify how the app responds to HTTP requests. The request pipeline is configured - // by adding middleware components to an IApplicationBuilder instance. IApplicationBuilder is available to the - // Configure method(), but it isn't registered in the service container. Hosting creates an IApplicationBuilder - // and passes it directly to Configure(). - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - - app.UseSwagger(); - app.UseSwaggerUI(); - } - - app.UseRequestLocalization(); - - app.UseStaticFiles(); - - app.UseHttpsRedirection(); - - app.UseRouting(); - - if (env.IsDevelopment()) - app.UseCors("CorsPolicy"); - - app.UseAuthentication(); - app.UseAuthorization(); - - app.UseEndpoints(endpoints => - { - var conventionBuilder = endpoints.MapControllers(); - conventionBuilder.WithMetadata(new AllowAnonymousAttribute()); - - endpoints.MapRazorPages(); - }); - } - } - - public class ExamplePolicyProvider : DefaultAuthorizationPolicyProvider - { - public ExamplePolicyProvider(IOptions options) : base(options) - { - } - - public override async Task GetPolicyAsync(string policyName) - { - var policy = await base.GetPolicyAsync(policyName); - - if (policy == null) - { - policy = new AuthorizationPolicyBuilder() - .RequireClaim("Permission", policyName) - .Build(); - } - return policy; - } - } - -} \ No newline at end of file From 55d608a63f137be6938bbe8a08d04e4c2ab4bf88 Mon Sep 17 00:00:00 2001 From: Thomas Fuchs Date: Wed, 6 Dec 2023 16:57:41 +0100 Subject: [PATCH 2/2] Switch to top level class --- src/MyApplication.App/Program.cs | 140 +++++++++++++++---------------- 1 file changed, 66 insertions(+), 74 deletions(-) diff --git a/src/MyApplication.App/Program.cs b/src/MyApplication.App/Program.cs index ba4e32a..068cddd 100644 --- a/src/MyApplication.App/Program.cs +++ b/src/MyApplication.App/Program.cs @@ -8,107 +8,99 @@ using Moryx.Model; using Moryx.Runtime.Kernel; using Moryx.Tools; +using MyApplication.App; using System.Globalization; using System.IO; using System.Text.Json.Serialization; -namespace MyApplication.App +AppDomainBuilder.LoadAssemblies(); + +var builder = WebApplication.CreateBuilder(); +var services = builder.Services; + +// Setup MORYX +services.AddMoryxKernel(); +services.AddMoryxModels(); +services.AddMoryxModules(); + +#region Startup ConfigureServices +services.AddSingleton(); + +services.AddLocalization(); +services.Configure(options => { - public class Program + var supportedCultures = new[] { - public static void Main(string[] args) - { - AppDomainBuilder.LoadAssemblies(); - - var builder = WebApplication.CreateBuilder(); - var services = builder.Services; - - // Setup MORYX - services.AddMoryxKernel(); - services.AddMoryxModels(); - services.AddMoryxModules(); - - #region Startup ConfigureServices - services.AddSingleton(); - - services.AddLocalization(); - services.Configure(options => - { - var supportedCultures = new[] - { new CultureInfo("de-De"), new CultureInfo("en-De"), new CultureInfo("it-De"), new CultureInfo("zh-Hans"), - }; + }; - options.SupportedCultures = supportedCultures; - options.SupportedUICultures = supportedCultures; - }); + options.SupportedCultures = supportedCultures; + options.SupportedUICultures = supportedCultures; +}); - services.AddCors(options => - { - options.AddPolicy("CorsPolicy", builder => builder - .WithOrigins("http://localhost:4200") // Angular app url for testing purposes - .AllowAnyMethod() - .AllowAnyHeader() - .AllowCredentials()); - }); +services.AddCors(options => +{ + options.AddPolicy("CorsPolicy", builder => builder + .WithOrigins("http://localhost:4200") // Angular app url for testing purposes + .AllowAnyMethod() + .AllowAnyHeader() + .AllowCredentials()); +}); - services.AddRazorPages(); +services.AddRazorPages(); - services.AddControllers() - .AddJsonOptions(jo => jo.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter())); +services.AddControllers() + .AddJsonOptions(jo => jo.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter())); - services.AddSwaggerGen(c => - { - c.CustomOperationIds(api => ((ControllerActionDescriptor)api.ActionDescriptor).MethodInfo.Name); - c.CustomSchemaIds(type => type.ToString()); - }); - #endregion +services.AddSwaggerGen(c => +{ + c.CustomOperationIds(api => ((ControllerActionDescriptor)api.ActionDescriptor).MethodInfo.Name); + c.CustomSchemaIds(type => type.ToString()); +}); +#endregion - var app = builder.Build(); - var env = app.Environment; +var app = builder.Build(); +var env = app.Environment; - #region Startup Configure App - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); +#region Startup Configure App +if (env.IsDevelopment()) +{ + app.UseDeveloperExceptionPage(); - app.UseSwagger(); - app.UseSwaggerUI(); - } + app.UseSwagger(); + app.UseSwaggerUI(); +} - app.UseRequestLocalization(); +app.UseRequestLocalization(); - app.UseStaticFiles(); +app.UseStaticFiles(); - app.UseHttpsRedirection(); +app.UseHttpsRedirection(); - app.UseRouting(); +app.UseRouting(); - if (env.IsDevelopment()) - app.UseCors("CorsPolicy"); +if (env.IsDevelopment()) + app.UseCors("CorsPolicy"); - app.UseAuthentication(); - app.UseAuthorization(); +app.UseAuthentication(); +app.UseAuthorization(); - app.UseEndpoints(endpoints => - { - var conventionBuilder = endpoints.MapControllers(); - conventionBuilder.WithMetadata(new AllowAnonymousAttribute()); +app.UseEndpoints(endpoints => +{ + var conventionBuilder = endpoints.MapControllers(); + conventionBuilder.WithMetadata(new AllowAnonymousAttribute()); - endpoints.MapRazorPages(); - }); - #endregion + endpoints.MapRazorPages(); +}); +#endregion - app.Services.UseMoryxConfigurations("Config"); - app.Services.StartMoryxModules(); +app.Services.UseMoryxConfigurations("Config"); +app.Services.StartMoryxModules(); - app.Run(); +app.Run(); - app.Services.StopMoryxModules(); - } - } -} +app.Services.StopMoryxModules();