-
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.
Merge pull request #89 from canro91/remove-startup-class-vetclinic
Migrate Startup class to Program file - VetClinicPublic
- Loading branch information
Showing
3 changed files
with
94 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,44 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using VetClinicPublic; | ||
|
||
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(); | ||
|
||
var configuration = builder.Configuration; | ||
builder.Services.ConfigureSiteSettings(configuration); | ||
builder.Services.ConfigureEmailSending(configuration); | ||
builder.Services.AddMessaging(configuration); | ||
|
||
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 | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
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<Startup>(); | ||
}); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
VetClinicPublic/src/VetClinicPublic/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,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); | ||
}); | ||
}); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.