using System; using System.IO; using System.Reflection; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; using MassTransit; using LCU.Library.Internal.FileOperationsModels.Models; using LCU.Library.Internal.LcuEvaluation; using LCU.Library.Internal.LcuMassTransit; using LCU.Library.Internal.LcuMassTransitModels; using LCU.Library.Internal.Status; using LCU.Library.Internal.Configuration; using LCU.Library.Internal.FileOperationsModels.Models.Requests; using LCU.Library.Internal.LCUServiceFactory; using LCU.Library.Internal.LCUServiceFactory.Configurations; using LCU.Library.Internal.Logging.Models; using LCU.Library.Internal.Logging; using LCU.Api.Internal.FileOperations.Configurations; using LCU.Api.Internal.FileOperations.Consumers; using LCU.Api.Internal.FileOperations.Services; using LCU.Api.Internal.FileOperations.Services.ExcelServices; using LCU.Api.Internal.FileOperations.Services.FileActionServices; using LCU.Api.Internal.FileOperations.Services.PdfServices; using LCU.Api.Internal.FileOperations.Services.QueueServices; string MyAllowSpecificOrigins = "_myAllowSpecificOrigins"; var builder = BuildBuilder(args); BuildServicesAndConfigurations(builder); BuildAppAndRun(builder); WebApplicationBuilder BuildBuilder(string[] args) { var builder = WebApplication.CreateBuilder(args); builder.Host.UseSerilog(); return builder; } void BuildServicesAndConfigurations(WebApplicationBuilder builder) { var services = builder.Services; var configuration = builder.Configuration; //Configurations var apiConfiguration = configuration.Bind(services, "Primary"); var servicesFactoryConfiguration = configuration.Bind(services); services.AddSingleton(apiConfiguration); var loggerConfiguration = new LoggingConfiguration(apiConfiguration.AppName, apiConfiguration.SeqApiKey, apiConfiguration.SeqUrl); var lcuLogging = new LcuLogging(loggerConfiguration); lcuLogging.UseLcuLogging(); var connectionConfiguration = MassTransitConfigurationSetUp.AddConnectionConfiguration(ref services, configuration); var receiveEndpointConfiguration = MassTransitConfigurationSetUp.GetReceiveEndpointConfiguration(configuration, "FileOperationsReceiveEndpoint"); services.AddSingleton(receiveEndpointConfiguration); AddMassTransitServices(ref services, connectionConfiguration, receiveEndpointConfiguration); //Services services.AddLCUStatusServicesWithApiHealthCheck(configuration); services.AddLcuEvaluationServices(); AddFileOperationServices(services); AddFileActionServices(services); AddServiceFactoryServices(services); AddPdfServices(services); AddExcelServices(services); services.AddLCUServiceFactoryAndValidateServiceRegistration(servicesFactoryConfiguration); services.AddControllers(); services.AddCors(options => { options.AddPolicy(MyAllowSpecificOrigins, builder => { builder.WithOrigins( apiConfiguration.CorsAllowedOrigins ).AllowAnyHeader().AllowAnyMethod(); }); }); // Register the Swagger generator, defining 1 or more Swagger documents services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "File Operations API", Version = "v1" }); // For this to work, I also had to check the generate xml box in build options. var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.XML"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); }); } void BuildAppAndRun(WebApplicationBuilder builder) { var app = builder.Build(); app.UseSwagger(); // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), // specifying the Swagger JSON endpoint. app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "File Operations API"); }); if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseAuthentication(); app.UseCors(MyAllowSpecificOrigins); app.UseHttpsRedirection(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); } ); app.Run(); } void AddFileOperationServices(IServiceCollection services) { services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); } void AddServiceFactoryServices(IServiceCollection services) { services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped>(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); } void AddFileActionServices(IServiceCollection services) { services.AddScoped, MoveFileService>(); services.AddScoped, ModifyFileService>(); services.AddScoped, CopyFileService>(); services.AddScoped, DeleteFileService>(); services.AddScoped, RenameFileService>(); services.AddScoped, ZipDirectoryService>(); services.AddScoped, UnzipFileService>(); services.AddScoped, UnzipPasswordProtectedFileService>(); services.AddScoped, ValidateFileService>(); services.AddScoped, CreateDirectoryService>(); services.AddScoped, RenameDirectoryService>(); services.AddScoped>, BulkOperationService>(); services.AddScoped, EncryptFileDataService>(); services.AddScoped, DecryptFileDataService>(); services.AddScoped, WriteFileService>(); services.AddScoped, DeleteDirectoryService>(); services.AddScoped, AppendFileService>(); services.AddScoped, ExtractIndexFileDataService>(); services.AddScoped, DeleteEmptyDirectoryService>(); services.AddScoped, ValidateFileSizeInRangeService>(); } void AddPdfServices(IServiceCollection services) { services.AddScoped(); services.AddScoped(); services.AddScoped(); } void AddExcelServices(IServiceCollection services) { services.AddScoped(); } void AddMassTransitServices(ref IServiceCollection services, ConnectionConfiguration connectionConfiguration, ReceiveEndpointConfiguration receiveEndpointConfiguration) { services.AddMassTransit(x => { x.AddConsumer(); x.UsingRabbitMq((context, cfg) => { cfg.Host(connectionConfiguration.HostUrl, "/", host => { host.Username(connectionConfiguration.UserName); host.Password(connectionConfiguration.Password); }); cfg.ReceiveEndpoint(receiveEndpointConfiguration.ReceiveEndpoint, e => { e.ConfigureConsumer(context); e.SetQuorumQueue(); e.PrefetchCount = receiveEndpointConfiguration.PrefetchLimit; }); }); }); services.AddMassTransitHostedService(); }