154 lines
5.7 KiB
C#
154 lines
5.7 KiB
C#
using Autofac;
|
|
using Autofac.Extensions.DependencyInjection;
|
|
using Microsoft.ApplicationInsights.Extensibility;
|
|
using Microsoft.ApplicationInsights.ServiceFabric;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Payment.API.IntegrationEvents.EventHandling;
|
|
using System;
|
|
using HealthChecks.UI.Client;
|
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
|
|
namespace Payment.API
|
|
{
|
|
public class Startup
|
|
{
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
public IServiceProvider ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddCustomHealthCheck(Configuration);
|
|
services.Configure<PaymentSettings>(Configuration);
|
|
|
|
RegisterAppInsights(services);
|
|
|
|
services.AddIntegrationEventHandler();
|
|
services.AddCap(options =>
|
|
{
|
|
options.UseInMemoryStorage();
|
|
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
|
{
|
|
options.UseAzureServiceBus(Configuration["EventBusConnection"]);
|
|
}
|
|
else
|
|
{
|
|
options.UseRabbitMQ(conf =>
|
|
{
|
|
conf.HostName = Configuration["EventBusConnection"];
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusUserName"]))
|
|
{
|
|
conf.UserName = Configuration["EventBusUserName"];
|
|
}
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"]))
|
|
{
|
|
conf.Password = Configuration["EventBusPassword"];
|
|
}
|
|
});
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
|
|
{
|
|
options.FailedRetryCount = int.Parse(Configuration["EventBusRetryCount"]);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(Configuration["SubscriptionClientName"]))
|
|
{
|
|
options.DefaultGroup = Configuration["SubscriptionClientName"];
|
|
}
|
|
});
|
|
|
|
var container = new ContainerBuilder();
|
|
container.Populate(services);
|
|
return new AutofacServiceProvider(container.Build());
|
|
}
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
|
{
|
|
//loggerFactory.AddAzureWebAppDiagnostics();
|
|
//loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace);
|
|
|
|
var pathBase = Configuration["PATH_BASE"];
|
|
if (!string.IsNullOrEmpty(pathBase))
|
|
{
|
|
app.UsePathBase(pathBase);
|
|
}
|
|
|
|
|
|
app.UseHealthChecks("/hc", new HealthCheckOptions()
|
|
{
|
|
Predicate = _ => true,
|
|
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
|
|
});
|
|
|
|
app.UseHealthChecks("/liveness", new HealthCheckOptions
|
|
{
|
|
Predicate = r => r.Name.Contains("self")
|
|
});
|
|
}
|
|
|
|
private void RegisterAppInsights(IServiceCollection services)
|
|
{
|
|
services.AddApplicationInsightsTelemetry(Configuration);
|
|
var orchestratorType = Configuration.GetValue<string>("OrchestratorType");
|
|
|
|
if (orchestratorType?.ToUpper() == "K8S")
|
|
{
|
|
// Enable K8s telemetry initializer
|
|
services.AddApplicationInsightsKubernetesEnricher();
|
|
}
|
|
if (orchestratorType?.ToUpper() == "SF")
|
|
{
|
|
// Enable SF telemetry initializer
|
|
services.AddSingleton<ITelemetryInitializer>((serviceProvider) =>
|
|
new FabricTelemetryInitializer());
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class CustomExtensionMethods
|
|
{
|
|
public static IServiceCollection AddIntegrationEventHandler(this IServiceCollection services)
|
|
{
|
|
services.AddTransient<OrderStatusChangedToStockConfirmedIntegrationEventHandler>(); //Subscribe for OrderStatusChangedToStockConfirmedIntegrationEvent
|
|
return services;
|
|
}
|
|
|
|
public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
var hcBuilder = services.AddHealthChecks();
|
|
|
|
hcBuilder.AddCheck("self", () => HealthCheckResult.Healthy());
|
|
|
|
if (configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
|
{
|
|
hcBuilder
|
|
.AddAzureServiceBusTopic(
|
|
configuration["EventBusConnection"],
|
|
topicName: "eshop_event_bus",
|
|
name: "payment-servicebus-check",
|
|
tags: new string[] { "servicebus" });
|
|
}
|
|
else
|
|
{
|
|
hcBuilder
|
|
.AddRabbitMQ(
|
|
$"amqp://{configuration["EventBusConnection"]}",
|
|
name: "payment-rabbitmqbus-check",
|
|
tags: new string[] { "rabbitmqbus" });
|
|
}
|
|
|
|
return services;
|
|
}
|
|
}
|
|
} |