From 92b87fb754b98af15238169525808aa4968b790a Mon Sep 17 00:00:00 2001 From: Cesar Aguirre Date: Mon, 2 Sep 2024 12:54:39 -0500 Subject: [PATCH 1/2] Use simplified Program.cs - VetClinicPublic --- .../src/VetClinicPublic/Program.cs | 91 ++++++++++++++---- .../src/VetClinicPublic/Startup.cs | 92 ------------------- 2 files changed, 74 insertions(+), 109 deletions(-) delete mode 100644 VetClinicPublic/src/VetClinicPublic/Startup.cs diff --git a/VetClinicPublic/src/VetClinicPublic/Program.cs b/VetClinicPublic/src/VetClinicPublic/Program.cs index bca377d3..b0982d68 100644 --- a/VetClinicPublic/src/VetClinicPublic/Program.cs +++ b/VetClinicPublic/src/VetClinicPublic/Program.cs @@ -1,24 +1,81 @@ using System; -using Microsoft.AspNetCore.Hosting; +using System.Reflection; +using MassTransit; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using VetClinicPublic; +using VetClinicPublic.Web.Interfaces; +using VetClinicPublic.Web.Services; -namespace VetClinicPublic +var builder = WebApplication.CreateBuilder(args); + +Console.WriteLine("Run RabbitMQ in Docker if it's not already running."); +Console.WriteLine(">> docker run -p 5672:5672 -p 15672:15672 rabbitmq:management"); + +builder.Services.AddControllersWithViews(); + +// configure site settings +var siteSettings = builder.Configuration.GetSection("SiteSettings"); +builder.Services.Configure(siteSettings); + +// configure email sending +var mailserverConfig = builder.Configuration.GetSection("Mailserver"); +builder.Services.Configure(mailserverConfig); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); + +// configure MediatR +builder.Services.AddMediatR(config => config.RegisterServicesFromAssembly(typeof(Main).Assembly)); + +// configure messaging +var messagingConfig = builder.Configuration.GetSection("RabbitMq"); +builder.Services.Configure(messagingConfig); +builder.Services.AddScoped(); + +builder.Services.AddMassTransit(x => { - public class Program + var rabbitMqConfiguration = messagingConfig.Get(); + x.SetKebabCaseEndpointNameFormatter(); + + x.AddConsumers(Assembly.GetExecutingAssembly()); + + x.UsingRabbitMq((context, cfg) => { - public static void Main(string[] args) + var port = (ushort)rabbitMqConfiguration.Port; + cfg.Host(rabbitMqConfiguration.Hostname, port, rabbitMqConfiguration.VirtualHost, h => { - CreateHostBuilder(args).Build().Run(); - - Console.WriteLine("Run RabbitMQ in Docker if it's not already running."); - Console.WriteLine(">> docker run -p 5672:5672 -p 15672:15672 rabbitmq:management"); - } - - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); - } + h.Username(rabbitMqConfiguration.UserName); + h.Password(rabbitMqConfiguration.Password); + }); + + cfg.ConfigureEndpoints(context); + }); +}); +var app = builder.Build(); + +if (builder.Environment.IsDevelopment()) +{ + app.UseDeveloperExceptionPage(); +} +else +{ + app.UseExceptionHandler("/Home/Error"); + app.UseHsts(); +} +// disabled because of docker setup required +//app.UseHttpsRedirection(); +app.UseStaticFiles(); + +app.UseRouting(); + +app.MapControllerRoute( + name: "default", + pattern: "{controller=Home}/{action=Index}/{id?}"); + +app.Run(); + +public partial class Main +{ } diff --git a/VetClinicPublic/src/VetClinicPublic/Startup.cs b/VetClinicPublic/src/VetClinicPublic/Startup.cs deleted file mode 100644 index 2a46659e..00000000 --- a/VetClinicPublic/src/VetClinicPublic/Startup.cs +++ /dev/null @@ -1,92 +0,0 @@ -using System.Reflection; -using MassTransit; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using VetClinicPublic.Web.Interfaces; -using VetClinicPublic.Web.Services; - -namespace VetClinicPublic -{ - public class Startup - { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - public void ConfigureServices(IServiceCollection services) - { - services.AddControllersWithViews(); - - // configure site settings - var siteSettings = Configuration.GetSection("SiteSettings"); - services.Configure(siteSettings); - - // configure email sending - var mailserverConfig = Configuration.GetSection("Mailserver"); - services.Configure(mailserverConfig); - services.AddSingleton(); - services.AddSingleton(); - - - // configure MediatR - services.AddMediatR(config => config.RegisterServicesFromAssembly(typeof(Startup).Assembly)); - - // configure messaging - var messagingConfig = Configuration.GetSection("RabbitMq"); - services.Configure(messagingConfig); - services.AddScoped(); - - services.AddMassTransit(x => - { - var rabbitMqConfiguration = messagingConfig.Get(); - x.SetKebabCaseEndpointNameFormatter(); - - x.AddConsumers(Assembly.GetExecutingAssembly()); - - x.UsingRabbitMq((context, cfg) => - { - var port = (ushort)rabbitMqConfiguration.Port; - cfg.Host(rabbitMqConfiguration.Hostname, port, rabbitMqConfiguration.VirtualHost, h => - { - h.Username(rabbitMqConfiguration.UserName); - h.Password(rabbitMqConfiguration.Password); - }); - - cfg.ConfigureEndpoints(context); - }); - }); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - else - { - app.UseExceptionHandler("/Home/Error"); - app.UseHsts(); - } - // disabled because of docker setup required - //app.UseHttpsRedirection(); - app.UseStaticFiles(); - - app.UseRouting(); - - app.UseEndpoints(endpoints => - { - endpoints.MapControllerRoute( - name: "default", - pattern: "{controller=Home}/{action=Index}/{id?}"); - }); - } - } -} From be86a23fbefd02b2453471f11c2a83a2f8d6b20c Mon Sep 17 00:00:00 2001 From: Cesar Aguirre Date: Mon, 2 Sep 2024 13:15:26 -0500 Subject: [PATCH 2/2] Use extension methods to register services --- .../src/VetClinicPublic/Program.cs | 45 ++------------- .../ServiceCollectionExtensions.cs | 55 +++++++++++++++++++ 2 files changed, 59 insertions(+), 41 deletions(-) create mode 100644 VetClinicPublic/src/VetClinicPublic/ServiceCollectionExtensions.cs diff --git a/VetClinicPublic/src/VetClinicPublic/Program.cs b/VetClinicPublic/src/VetClinicPublic/Program.cs index b0982d68..cae83c58 100644 --- a/VetClinicPublic/src/VetClinicPublic/Program.cs +++ b/VetClinicPublic/src/VetClinicPublic/Program.cs @@ -1,13 +1,8 @@ using System; -using System.Reflection; -using MassTransit; using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using VetClinicPublic; -using VetClinicPublic.Web.Interfaces; -using VetClinicPublic.Web.Services; var builder = WebApplication.CreateBuilder(args); @@ -16,43 +11,11 @@ builder.Services.AddControllersWithViews(); -// configure site settings -var siteSettings = builder.Configuration.GetSection("SiteSettings"); -builder.Services.Configure(siteSettings); +var configuration = builder.Configuration; +builder.Services.ConfigureSiteSettings(configuration); +builder.Services.ConfigureEmailSending(configuration); +builder.Services.AddMessaging(configuration); -// configure email sending -var mailserverConfig = builder.Configuration.GetSection("Mailserver"); -builder.Services.Configure(mailserverConfig); -builder.Services.AddSingleton(); -builder.Services.AddSingleton(); - -// configure MediatR -builder.Services.AddMediatR(config => config.RegisterServicesFromAssembly(typeof(Main).Assembly)); - -// configure messaging -var messagingConfig = builder.Configuration.GetSection("RabbitMq"); -builder.Services.Configure(messagingConfig); -builder.Services.AddScoped(); - -builder.Services.AddMassTransit(x => -{ - var rabbitMqConfiguration = messagingConfig.Get(); - x.SetKebabCaseEndpointNameFormatter(); - - x.AddConsumers(Assembly.GetExecutingAssembly()); - - x.UsingRabbitMq((context, cfg) => - { - var port = (ushort)rabbitMqConfiguration.Port; - cfg.Host(rabbitMqConfiguration.Hostname, port, rabbitMqConfiguration.VirtualHost, h => - { - h.Username(rabbitMqConfiguration.UserName); - h.Password(rabbitMqConfiguration.Password); - }); - - cfg.ConfigureEndpoints(context); - }); -}); var app = builder.Build(); if (builder.Environment.IsDevelopment()) diff --git a/VetClinicPublic/src/VetClinicPublic/ServiceCollectionExtensions.cs b/VetClinicPublic/src/VetClinicPublic/ServiceCollectionExtensions.cs new file mode 100644 index 00000000..f0274feb --- /dev/null +++ b/VetClinicPublic/src/VetClinicPublic/ServiceCollectionExtensions.cs @@ -0,0 +1,55 @@ +using System.Reflection; +using MassTransit; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using VetClinicPublic.Web.Interfaces; +using VetClinicPublic.Web.Services; + +namespace VetClinicPublic +{ + public static class ServiceCollectionExtensions + { + public static void ConfigureSiteSettings(this IServiceCollection services, ConfigurationManager configuration) + { + var siteSettings = configuration.GetSection("SiteSettings"); + services.Configure(siteSettings); + } + + public static void ConfigureEmailSending(this IServiceCollection services, ConfigurationManager configuration) + { + var mailserverConfig = configuration.GetSection("Mailserver"); + services.Configure(mailserverConfig); + services.AddSingleton(); + services.AddSingleton(); + + services.AddMediatR(config => config.RegisterServicesFromAssembly(typeof(Main).Assembly)); + } + + public static void AddMessaging(this IServiceCollection services, ConfigurationManager configuration) + { + var messagingConfig = configuration.GetSection("RabbitMq"); + services.Configure(messagingConfig); + services.AddScoped(); + + services.AddMassTransit(x => + { + var rabbitMqConfiguration = messagingConfig.Get(); + x.SetKebabCaseEndpointNameFormatter(); + + x.AddConsumers(Assembly.GetExecutingAssembly()); + + x.UsingRabbitMq((context, cfg) => + { + var port = (ushort)rabbitMqConfiguration.Port; + cfg.Host(rabbitMqConfiguration.Hostname, port, rabbitMqConfiguration.VirtualHost, h => + { + h.Username(rabbitMqConfiguration.UserName); + h.Password(rabbitMqConfiguration.Password); + }); + + cfg.ConfigureEndpoints(context); + }); + }); + } + } +}