Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
241 changes: 188 additions & 53 deletions TestHosts/TestHosts/Program.cs
Original file line number Diff line number Diff line change
@@ -1,60 +1,195 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Shared.EntityFramework;

namespace TestHosts
{
using System.IO;
using Database.PataPawa;

using CoreWCF;
using CoreWCF.Configuration;
using CoreWCF.Description;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.WindowsServices;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Web;
using Shared.EntityFramework;
using Shared.Extensions;
using Shared.General;
using Shared.Middleware;
using System;
using System.IO;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Shared.Logger.TennantContext;
using TestHosts;
using TestHosts.Common;
using TestHosts.Database.PataPawa;
using TestHosts.Database.TestBank;
using TestHosts.SoapServices;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

public class Program
{
public static void Main(string[] args)
{
Program.CreateHostBuilder(args).Build().Run();
try {

WebApplicationBuilder builder = WebApplication.CreateBuilder(new WebApplicationOptions { Args = args, ContentRootPath = AppContext.BaseDirectory });

// ----------------------------------------------------------------------
// Load custom hosting configuration (your existing hosting.json setup)
// ----------------------------------------------------------------------
FileInfo fi = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
builder.Configuration.SetBasePath(fi.Directory!.FullName).AddJsonFile("hosting.json", optional: false, reloadOnChange: true).AddJsonFile("hosting.development.json", optional: true, reloadOnChange: true)
.AddJsonFile("/home/txnproc/config/appsettings.json", true, true)
.AddJsonFile($"/home/txnproc/config/appsettings.{builder.Environment.EnvironmentName}.json", optional: true)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();

ConfigurationReader.Initialise(builder.Configuration);
// ----------------------------------------------------------------------
// Configure Windows Service mode
// ----------------------------------------------------------------------
if (WindowsServiceHelpers.IsWindowsService())
builder.Host.UseWindowsService();

// ----------------------------------------------------------------------
// Configure Kestrel
// ----------------------------------------------------------------------
builder.WebHost.ConfigureKestrel(options => { options.AllowSynchronousIO = true; });

// ----------------------------------------------------------------------
// Configure NLog
// ----------------------------------------------------------------------
string nlogConfigFilename = "nlog.config";
if (builder.Environment.IsDevelopment()) {
String devFile = Path.Combine(builder.Environment.ContentRootPath, "nlog.development.config");
if (File.Exists(devFile))
nlogConfigFilename = "nlog.development.config";
}

public static IHostBuilder CreateHostBuilder(string[] args)
LogManager.Setup().LoadConfigurationFromFile(Path.Combine(builder.Environment.ContentRootPath, nlogConfigFilename));
builder.Logging.ClearProviders();
builder.Host.UseNLog();

// ----------------------------------------------------------------------
// Add application and framework services
// ----------------------------------------------------------------------
builder.Services.AddControllers().AddNewtonsoftJson(options =>
{
FileInfo fi = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);

//At this stage, we only need our hosting file for ip and ports
IConfigurationRoot config = new ConfigurationBuilder().SetBasePath(fi.Directory.FullName)
.AddJsonFile("hosting.json", optional: false)
.AddJsonFile("hosting.development.json", optional: true)
.AddEnvironmentVariables().Build();

IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args);
hostBuilder.UseWindowsService();
hostBuilder.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseConfiguration(config);
webBuilder.UseKestrel();
webBuilder.ConfigureKestrel((context,
options) => {
options.AllowSynchronousIO = true;
});
});
hostBuilder.ConfigureServices(services =>
{
services.AddHostedService<PendingPrePaymentProcessor>(provider =>
{
IDbContextResolver<PataPawaContext> contextResolver = provider.GetRequiredService<IDbContextResolver<PataPawaContext>>();
PendingPrePaymentProcessor worker = new (contextResolver);

return worker;
});
});

return hostBuilder;
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
options.SerializerSettings.TypeNameHandling = TypeNameHandling.None;
options.SerializerSettings.Formatting = Formatting.Indented;
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
builder.Services.AddHealthChecks().AddSqlServer(connectionString: ConfigurationReader.GetConnectionString("HealthCheck"),
healthQuery: "SELECT 1;",
name: "Read Model Server",
failureStatus: HealthStatus.Degraded,
tags: new[] { "db", "sql", "sqlserver" }); ;

Check notice on line 90 in TestHosts/TestHosts/Program.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

TestHosts/TestHosts/Program.cs#L90

Remove this empty statement.
builder.Services.AddServiceModelServices().AddServiceModelMetadata();

builder.Services.AddSingleton(typeof(IDbContextResolver<>), typeof(DbContextResolver<>));
if (builder.Environment.IsEnvironment("IntegrationTest") || builder.Configuration.GetValue<Boolean>("ServiceOptions:UseInMemoryDatabase") == true)
{
builder.Services.AddDbContext<TestBankContext>(builder => builder.UseInMemoryDatabase("TestBankReadModel"));
builder.Services.AddDbContext<PataPawaContext>(builder => builder.UseInMemoryDatabase("PataPawaReadModel"));

}
}
else
{
String testBankConnectionString = ConfigurationReader.GetConnectionString("TestBankReadModel");
builder.Services.AddDbContext<TestBankContext>(builder => builder.UseSqlServer(testBankConnectionString));

String pataPawaConnectionString = ConfigurationReader.GetConnectionString("PataPawaReadModel");
builder.Services.AddDbContext<PataPawaContext>(builder => builder.UseSqlServer(pataPawaConnectionString));
}
builder.Services.AddScoped<TenantContext>(x => new TenantContext());
builder.Services.AddSingleton<PataPawaPostPayService>();
// Add your background hosted service
builder.Services.AddHostedService<PendingPrePaymentProcessor>(provider => {
IDbContextResolver<PataPawaContext> contextResolver = provider.GetRequiredService<IDbContextResolver<PataPawaContext>>();
return new PendingPrePaymentProcessor(contextResolver);
});

// Database initialization will now be handled by a hosted service
builder.Services.AddHostedService<DatabaseInitializerHostedService>();

builder.Services.AddScoped<TenantContext>(x => new TenantContext());
builder.Services.AddSingleton<PataPawaPostPayService>();
builder.Services.AddMvc();

builder.Services.AddServiceModelServices().AddServiceModelMetadata();
builder.Services.AddSingleton<IServiceBehavior, UseRequestHeadersForMetadataAddressBehavior>();
builder.Services.AddSingleton<IServiceBehavior, CorrelationIdBehavior>();


bool logRequests = ConfigurationReader.GetValueOrDefault<Boolean>("MiddlewareLogging", "LogRequests", true);
bool logResponses = ConfigurationReader.GetValueOrDefault<Boolean>("MiddlewareLogging", "LogResponses", true);
LogLevel middlewareLogLevel = ConfigurationReader.GetValueOrDefault<LogLevel>("MiddlewareLogging", "MiddlewareLogLevel", LogLevel.Warning);

RequestResponseMiddlewareLoggingConfig config =
new RequestResponseMiddlewareLoggingConfig(middlewareLogLevel, logRequests, logResponses);

builder.Services.AddSingleton(config);

// ----------------------------------------------------------------------
// Build the app
// ----------------------------------------------------------------------
WebApplication app = builder.Build();

// Create a scoped logger and assign it
using (var scope = app.Services.CreateScope())
{
var loggerFactory = scope.ServiceProvider.GetRequiredService<ILoggerFactory>();
var loggerObject = loggerFactory.CreateLogger("AppStartup");
Shared.Logger.Logger.Initialise(loggerObject);
}

// ----------------------------------------------------------------------
// Middleware pipeline
// ----------------------------------------------------------------------
if (app.Environment.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}

// Custom middleware
app.UseMiddleware<TenantMiddleware>();
app.AddRequestLogging();
app.AddResponseLogging();
app.AddExceptionHandler();

app.UseRouting();

// ----------------------------------------------------------------------
// Endpoints
// ----------------------------------------------------------------------
app.MapControllers();

app.MapHealthChecks("health", new HealthCheckOptions { Predicate = _ => true, ResponseWriter = Shared.HealthChecks.HealthCheckMiddleware.WriteResponse });

app.MapHealthChecks("healthui", new HealthCheckOptions { Predicate = _ => true, ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse });

// ----------------------------------------------------------------------
// CoreWCF setup
// ----------------------------------------------------------------------
app.UseServiceModel(builder => { builder.AddService<PataPawaPostPayService>(options => { options.DebugBehavior.IncludeExceptionDetailInFaults = true; }).AddServiceEndpoint<PataPawaPostPayService, IPataPawaPostPayService>(new BasicHttpBinding(), "/PataPawaPostPayService/basichttp"); });

ServiceMetadataBehavior metadataBehavior = app.Services.GetRequiredService<ServiceMetadataBehavior>();
metadataBehavior.HttpGetEnabled = true;

// ----------------------------------------------------------------------
// Start the application
// ----------------------------------------------------------------------
app.Run();

Shared.Logger.Logger.LogWarning("Application started successfully");
}
catch (Exception ex) {
Shared.Logger.Logger.LogError("Application stopped due to exception", ex);
throw;
}
finally {
LogManager.Shutdown();
}
Loading
Loading