Skip to content

Commit

Permalink
Use extension methods to register services
Browse files Browse the repository at this point in the history
  • Loading branch information
canro91 committed Sep 2, 2024
1 parent 92b87fb commit be86a23
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 41 deletions.
45 changes: 4 additions & 41 deletions VetClinicPublic/src/VetClinicPublic/Program.cs
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -16,43 +11,11 @@

builder.Services.AddControllersWithViews();

// configure site settings
var siteSettings = builder.Configuration.GetSection("SiteSettings");
builder.Services.Configure<SiteConfiguration>(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<MailserverConfiguration>(mailserverConfig);
builder.Services.AddSingleton<ISendEmail, SmtpEmailSender>();
builder.Services.AddSingleton<ISendConfirmationEmails, ConfirmationEmailSender>();

// configure MediatR
builder.Services.AddMediatR(config => config.RegisterServicesFromAssembly(typeof(Main).Assembly));

// configure messaging
var messagingConfig = builder.Configuration.GetSection("RabbitMq");
builder.Services.Configure<RabbitMqConfiguration>(messagingConfig);
builder.Services.AddScoped<IMessagePublisher, MassTransitMessagePublisher>();

builder.Services.AddMassTransit(x =>
{
var rabbitMqConfiguration = messagingConfig.Get<RabbitMqConfiguration>();
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())
Expand Down
55 changes: 55 additions & 0 deletions VetClinicPublic/src/VetClinicPublic/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<SiteConfiguration>(siteSettings);
}

public static void ConfigureEmailSending(this IServiceCollection services, ConfigurationManager configuration)
{
var mailserverConfig = configuration.GetSection("Mailserver");
services.Configure<MailserverConfiguration>(mailserverConfig);
services.AddSingleton<ISendEmail, SmtpEmailSender>();
services.AddSingleton<ISendConfirmationEmails, ConfirmationEmailSender>();

services.AddMediatR(config => config.RegisterServicesFromAssembly(typeof(Main).Assembly));
}

public static void AddMessaging(this IServiceCollection services, ConfigurationManager configuration)
{
var messagingConfig = configuration.GetSection("RabbitMq");
services.Configure<RabbitMqConfiguration>(messagingConfig);
services.AddScoped<IMessagePublisher, MassTransitMessagePublisher>();

services.AddMassTransit(x =>
{
var rabbitMqConfiguration = messagingConfig.Get<RabbitMqConfiguration>();
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);
});
});
}
}
}

0 comments on commit be86a23

Please sign in to comment.