-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify API Program.cs with extension methods
- Loading branch information
Showing
3 changed files
with
77 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
FrontDesk/src/FrontDesk.Api/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Reflection; | ||
using FrontDesk.Core.Interfaces; | ||
using FrontDesk.Infrastructure.Messaging; | ||
using MassTransit; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace FrontDesk.Api | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
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); | ||
}); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using FrontDesk.Infrastructure.Data; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace FrontDesk.Api | ||
{ | ||
public static class WebApplicationExtensions | ||
{ | ||
public static async Task SeedDatabaseAsync(this WebApplication app) | ||
{ | ||
using (var scope = app.Services.CreateScope()) | ||
{ | ||
var services = scope.ServiceProvider; | ||
var hostEnvironment = services.GetService<IWebHostEnvironment>(); | ||
var loggerFactory = services.GetRequiredService<ILoggerFactory>(); | ||
var logger = loggerFactory.CreateLogger<Program>(); | ||
logger.LogInformation($"Starting in environment {hostEnvironment.EnvironmentName}"); | ||
try | ||
{ | ||
var seedService = services.GetRequiredService<AppDbContextSeed>(); | ||
//var catalogContext = services.GetRequiredService<AppDbContext>(); | ||
await seedService.SeedAsync(new OfficeSettings().TestDate); | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(ex, "An error occurred seeding the DB."); | ||
} | ||
Check notice Code scanning / CodeQL Generic catch clause Note
Generic catch clause.
|
||
} | ||
} | ||
} | ||
} |