128 lines
5.1 KiB
C#
Raw Normal View History

using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder;
2017-04-28 15:04:38 +02:00
using Microsoft.AspNetCore.Hosting;
using Microsoft.Azure.ServiceBus;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
2017-04-28 15:04:38 +02:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Payment.API.IntegrationEvents.EventHandling;
using Payment.API.IntegrationEvents.Events;
using RabbitMQ.Client;
using System;
2017-04-28 15:04:38 +02:00
namespace Payment.API
{
public class Startup
{
public Startup(IConfiguration configuration)
2017-04-28 15:04:38 +02:00
{
Configuration = configuration;
2017-04-28 15:04:38 +02:00
}
public IConfiguration Configuration { get; }
2017-04-28 15:04:38 +02:00
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
2017-04-28 15:04:38 +02:00
{
// Add framework services.
services.AddMvc();
services.Configure<PaymentSettings>(Configuration);
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
{
services.AddSingleton<IServiceBusPersisterConnection>(sp =>
{
var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>();
var serviceBusConnectionString = Configuration["EventBusConnection"];
var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString);
return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger);
});
}
else
{
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
{
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
var factory = new ConnectionFactory()
{
HostName = Configuration["EventBusConnection"]
};
return new DefaultRabbitMQPersistentConnection(factory, logger);
});
}
2017-05-17 11:57:02 +02:00
RegisterEventBus(services);
2017-06-12 13:52:23 +02:00
services.AddSwaggerGen(options =>
2017-04-28 15:04:38 +02:00
{
options.DescribeAllEnumsAsStrings();
2017-06-12 13:52:23 +02:00
options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
2017-04-28 15:04:38 +02:00
{
Title = "eShopOnContainers - Payment HTTP API",
Version = "v1",
Description = "The Payment Microservice HTTP API. This is a Data-Driven/CRUD microservice sample",
TermsOfService = "Terms Of Service"
});
});
var container = new ContainerBuilder();
container.Populate(services);
return new AutofacServiceProvider(container.Build());
2017-04-28 15:04:38 +02:00
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
2017-04-28 15:04:38 +02:00
app.UseMvcWithDefaultRoute();
app.UseSwagger()
2017-06-12 13:52:23 +02:00
.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
ConfigureEventBus(app);
}
private void RegisterEventBus(IServiceCollection services)
2017-05-17 11:57:02 +02:00
{
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
{
services.AddSingleton<IEventBus, EventBusServiceBus>(sp =>
{
var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>();
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
var subscriptionClientName = Configuration["SubscriptionClientName"];
return new EventBusServiceBus(serviceBusPersisterConnection, logger,
eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope);
});
}
else
{
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
}
2017-05-17 11:57:02 +02:00
services.AddTransient<OrderStatusChangedToStockConfirmedIntegrationEventHandler>();
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
2017-05-17 11:57:02 +02:00
}
private void ConfigureEventBus(IApplicationBuilder app)
{
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
eventBus.Subscribe<OrderStatusChangedToStockConfirmedIntegrationEvent, OrderStatusChangedToStockConfirmedIntegrationEventHandler>();
2017-04-28 15:04:38 +02:00
}
}
}