Messagebus dispatch messages in its own scope
This commit is contained in:
parent
9a6ad2cc85
commit
7fc63fe196
@ -18,7 +18,7 @@ namespace EventBus.Tests
|
|||||||
public void After_One_Event_Subscription_Should_Contain_The_Event()
|
public void After_One_Event_Subscription_Should_Contain_The_Event()
|
||||||
{
|
{
|
||||||
var manager = new InMemoryEventBusSubscriptionsManager();
|
var manager = new InMemoryEventBusSubscriptionsManager();
|
||||||
manager.AddSubscription<TestIntegrationEvent,TestIntegrationEventHandler>(() => new TestIntegrationEventHandler());
|
manager.AddSubscription<TestIntegrationEvent,TestIntegrationEventHandler>();
|
||||||
Assert.True(manager.HasSubscriptionsForEvent<TestIntegrationEvent>());
|
Assert.True(manager.HasSubscriptionsForEvent<TestIntegrationEvent>());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ namespace EventBus.Tests
|
|||||||
public void After_All_Subscriptions_Are_Deleted_Event_Should_No_Longer_Exists()
|
public void After_All_Subscriptions_Are_Deleted_Event_Should_No_Longer_Exists()
|
||||||
{
|
{
|
||||||
var manager = new InMemoryEventBusSubscriptionsManager();
|
var manager = new InMemoryEventBusSubscriptionsManager();
|
||||||
manager.AddSubscription<TestIntegrationEvent, TestIntegrationEventHandler>(() => new TestIntegrationEventHandler());
|
manager.AddSubscription<TestIntegrationEvent, TestIntegrationEventHandler>();
|
||||||
manager.RemoveSubscription<TestIntegrationEvent, TestIntegrationEventHandler>();
|
manager.RemoveSubscription<TestIntegrationEvent, TestIntegrationEventHandler>();
|
||||||
Assert.False(manager.HasSubscriptionsForEvent<TestIntegrationEvent>());
|
Assert.False(manager.HasSubscriptionsForEvent<TestIntegrationEvent>());
|
||||||
}
|
}
|
||||||
@ -37,7 +37,7 @@ namespace EventBus.Tests
|
|||||||
bool raised = false;
|
bool raised = false;
|
||||||
var manager = new InMemoryEventBusSubscriptionsManager();
|
var manager = new InMemoryEventBusSubscriptionsManager();
|
||||||
manager.OnEventRemoved += (o, e) => raised = true;
|
manager.OnEventRemoved += (o, e) => raised = true;
|
||||||
manager.AddSubscription<TestIntegrationEvent, TestIntegrationEventHandler>(() => new TestIntegrationEventHandler());
|
manager.AddSubscription<TestIntegrationEvent, TestIntegrationEventHandler>();
|
||||||
manager.RemoveSubscription<TestIntegrationEvent, TestIntegrationEventHandler>();
|
manager.RemoveSubscription<TestIntegrationEvent, TestIntegrationEventHandler>();
|
||||||
Assert.True(raised);
|
Assert.True(raised);
|
||||||
}
|
}
|
||||||
@ -46,8 +46,8 @@ namespace EventBus.Tests
|
|||||||
public void Get_Handlers_For_Event_Should_Return_All_Handlers()
|
public void Get_Handlers_For_Event_Should_Return_All_Handlers()
|
||||||
{
|
{
|
||||||
var manager = new InMemoryEventBusSubscriptionsManager();
|
var manager = new InMemoryEventBusSubscriptionsManager();
|
||||||
manager.AddSubscription<TestIntegrationEvent, TestIntegrationEventHandler>(() => new TestIntegrationEventHandler());
|
manager.AddSubscription<TestIntegrationEvent, TestIntegrationEventHandler>();
|
||||||
manager.AddSubscription<TestIntegrationEvent, TestIntegrationOtherEventHandler>(() => new TestIntegrationOtherEventHandler());
|
manager.AddSubscription<TestIntegrationEvent, TestIntegrationOtherEventHandler>();
|
||||||
var handlers = manager.GetHandlersForEvent<TestIntegrationEvent>();
|
var handlers = manager.GetHandlersForEvent<TestIntegrationEvent>();
|
||||||
Assert.Equal(2, handlers.Count());
|
Assert.Equal(2, handlers.Count());
|
||||||
}
|
}
|
||||||
|
@ -5,10 +5,10 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions
|
|||||||
{
|
{
|
||||||
public interface IEventBus
|
public interface IEventBus
|
||||||
{
|
{
|
||||||
void Subscribe<T, TH>(Func<TH> handler)
|
void Subscribe<T, TH>()
|
||||||
where T : IntegrationEvent
|
where T : IntegrationEvent
|
||||||
where TH : IIntegrationEventHandler<T>;
|
where TH : IIntegrationEventHandler<T>;
|
||||||
void SubscribeDynamic<TH>(string eventName, Func<TH> handler)
|
void SubscribeDynamic<TH>(string eventName)
|
||||||
where TH : IDynamicIntegrationEventHandler;
|
where TH : IDynamicIntegrationEventHandler;
|
||||||
|
|
||||||
void UnsubscribeDynamic<TH>(string eventName)
|
void UnsubscribeDynamic<TH>(string eventName)
|
||||||
|
@ -10,10 +10,10 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
|
|||||||
{
|
{
|
||||||
bool IsEmpty { get; }
|
bool IsEmpty { get; }
|
||||||
event EventHandler<string> OnEventRemoved;
|
event EventHandler<string> OnEventRemoved;
|
||||||
void AddDynamicSubscription<TH>(string eventName, Func<TH> handler)
|
void AddDynamicSubscription<TH>(string eventName)
|
||||||
where TH : IDynamicIntegrationEventHandler;
|
where TH : IDynamicIntegrationEventHandler;
|
||||||
|
|
||||||
void AddSubscription<T, TH>(Func<TH> handler)
|
void AddSubscription<T, TH>()
|
||||||
where T : IntegrationEvent
|
where T : IntegrationEvent
|
||||||
where TH : IIntegrationEventHandler<T>;
|
where TH : IIntegrationEventHandler<T>;
|
||||||
|
|
||||||
|
@ -26,34 +26,41 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
|
|||||||
public bool IsEmpty => !_handlers.Keys.Any();
|
public bool IsEmpty => !_handlers.Keys.Any();
|
||||||
public void Clear() => _handlers.Clear();
|
public void Clear() => _handlers.Clear();
|
||||||
|
|
||||||
public void AddDynamicSubscription<TH>(string eventName, Func<TH> handler)
|
public void AddDynamicSubscription<TH>(string eventName)
|
||||||
where TH : IDynamicIntegrationEventHandler
|
where TH : IDynamicIntegrationEventHandler
|
||||||
{
|
{
|
||||||
DoAddSubscription(handler, eventName, isDynamic: true);
|
DoAddSubscription(typeof(TH), eventName, isDynamic: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddSubscription<T, TH>(Func<TH> handler)
|
public void AddSubscription<T, TH>()
|
||||||
where T : IntegrationEvent
|
where T : IntegrationEvent
|
||||||
where TH : IIntegrationEventHandler<T>
|
where TH : IIntegrationEventHandler<T>
|
||||||
{
|
{
|
||||||
var eventName = GetEventKey<T>();
|
var eventName = GetEventKey<T>();
|
||||||
DoAddSubscription(handler, eventName, isDynamic: false);
|
DoAddSubscription(typeof(TH), eventName, isDynamic: false);
|
||||||
_eventTypes.Add(typeof(T));
|
_eventTypes.Add(typeof(T));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DoAddSubscription(Delegate handler, string eventName, bool isDynamic)
|
private void DoAddSubscription(Type handlerType, string eventName, bool isDynamic)
|
||||||
{
|
{
|
||||||
if (!HasSubscriptionsForEvent(eventName))
|
if (!HasSubscriptionsForEvent(eventName))
|
||||||
{
|
{
|
||||||
_handlers.Add(eventName, new List<SubscriptionInfo>());
|
_handlers.Add(eventName, new List<SubscriptionInfo>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_handlers[eventName].Any(s => s.HandlerType == handlerType))
|
||||||
|
{
|
||||||
|
throw new ArgumentException(
|
||||||
|
$"Handler Type {handlerType.Name} already registered for '{eventName}'", nameof(handlerType));
|
||||||
|
}
|
||||||
|
|
||||||
if (isDynamic)
|
if (isDynamic)
|
||||||
{
|
{
|
||||||
_handlers[eventName].Add(SubscriptionInfo.Dynamic(handler));
|
_handlers[eventName].Add(SubscriptionInfo.Dynamic(handlerType));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_handlers[eventName].Add(SubscriptionInfo.Typed(handler));
|
_handlers[eventName].Add(SubscriptionInfo.Typed(handlerType));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,7 +122,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
|
|||||||
private SubscriptionInfo FindDynamicSubscriptionToRemove<TH>(string eventName)
|
private SubscriptionInfo FindDynamicSubscriptionToRemove<TH>(string eventName)
|
||||||
where TH : IDynamicIntegrationEventHandler
|
where TH : IDynamicIntegrationEventHandler
|
||||||
{
|
{
|
||||||
return DoFindHandlerToRemove(eventName, typeof(TH));
|
return DoFindSubscriptionToRemove(eventName, typeof(TH));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -124,25 +131,18 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
|
|||||||
where TH : IIntegrationEventHandler<T>
|
where TH : IIntegrationEventHandler<T>
|
||||||
{
|
{
|
||||||
var eventName = GetEventKey<T>();
|
var eventName = GetEventKey<T>();
|
||||||
return DoFindHandlerToRemove(eventName, typeof(TH));
|
return DoFindSubscriptionToRemove(eventName, typeof(TH));
|
||||||
}
|
}
|
||||||
|
|
||||||
private SubscriptionInfo DoFindHandlerToRemove(string eventName, Type handlerType)
|
private SubscriptionInfo DoFindSubscriptionToRemove(string eventName, Type handlerType)
|
||||||
{
|
{
|
||||||
if (!HasSubscriptionsForEvent(eventName))
|
if (!HasSubscriptionsForEvent(eventName))
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
foreach (var subscription in _handlers[eventName])
|
|
||||||
{
|
|
||||||
var genericArgs = subscription.Factory.GetType().GetGenericArguments();
|
|
||||||
if (genericArgs.SingleOrDefault() == handlerType)
|
|
||||||
{
|
|
||||||
return subscription;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
return _handlers[eventName].SingleOrDefault(s => s.HandlerType == handlerType);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool HasSubscriptionsForEvent<T>() where T : IntegrationEvent
|
public bool HasSubscriptionsForEvent<T>() where T : IntegrationEvent
|
||||||
|
@ -7,21 +7,21 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
|
|||||||
public class SubscriptionInfo
|
public class SubscriptionInfo
|
||||||
{
|
{
|
||||||
public bool IsDynamic { get; }
|
public bool IsDynamic { get; }
|
||||||
public Delegate Factory { get; }
|
public Type HandlerType{ get; }
|
||||||
|
|
||||||
private SubscriptionInfo(bool isDynamic, Delegate factory)
|
private SubscriptionInfo(bool isDynamic, Type handlerType)
|
||||||
{
|
{
|
||||||
IsDynamic = isDynamic;
|
IsDynamic = isDynamic;
|
||||||
Factory = factory;
|
HandlerType = handlerType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SubscriptionInfo Dynamic(Delegate factory)
|
public static SubscriptionInfo Dynamic(Type handlerType)
|
||||||
{
|
{
|
||||||
return new SubscriptionInfo(true, factory);
|
return new SubscriptionInfo(true, handlerType);
|
||||||
}
|
}
|
||||||
public static SubscriptionInfo Typed(Delegate factory)
|
public static SubscriptionInfo Typed(Type handlerType)
|
||||||
{
|
{
|
||||||
return new SubscriptionInfo(false, factory);
|
return new SubscriptionInfo(false, handlerType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
using Autofac;
|
||||||
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@ -26,17 +27,20 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
|
|||||||
private readonly IRabbitMQPersistentConnection _persistentConnection;
|
private readonly IRabbitMQPersistentConnection _persistentConnection;
|
||||||
private readonly ILogger<EventBusRabbitMQ> _logger;
|
private readonly ILogger<EventBusRabbitMQ> _logger;
|
||||||
private readonly IEventBusSubscriptionsManager _subsManager;
|
private readonly IEventBusSubscriptionsManager _subsManager;
|
||||||
|
private readonly ILifetimeScope _autofac;
|
||||||
|
private readonly string AUTOFAC_SCOPE_NAME = "eshop_event_bus";
|
||||||
|
|
||||||
private IModel _consumerChannel;
|
private IModel _consumerChannel;
|
||||||
private string _queueName;
|
private string _queueName;
|
||||||
|
|
||||||
public EventBusRabbitMQ(IRabbitMQPersistentConnection persistentConnection, ILogger<EventBusRabbitMQ> logger, IEventBusSubscriptionsManager subsManager)
|
public EventBusRabbitMQ(IRabbitMQPersistentConnection persistentConnection, ILogger<EventBusRabbitMQ> logger,
|
||||||
|
ILifetimeScope autofac, IEventBusSubscriptionsManager subsManager)
|
||||||
{
|
{
|
||||||
_persistentConnection = persistentConnection ?? throw new ArgumentNullException(nameof(persistentConnection));
|
_persistentConnection = persistentConnection ?? throw new ArgumentNullException(nameof(persistentConnection));
|
||||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
_subsManager = subsManager ?? new InMemoryEventBusSubscriptionsManager();
|
_subsManager = subsManager ?? new InMemoryEventBusSubscriptionsManager();
|
||||||
_consumerChannel = CreateConsumerChannel();
|
_consumerChannel = CreateConsumerChannel();
|
||||||
|
_autofac = autofac;
|
||||||
|
|
||||||
_subsManager.OnEventRemoved += SubsManager_OnEventRemoved;
|
_subsManager.OnEventRemoved += SubsManager_OnEventRemoved;
|
||||||
}
|
}
|
||||||
@ -97,20 +101,20 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SubscribeDynamic<TH>(string eventName, Func<TH> handler)
|
public void SubscribeDynamic<TH>(string eventName)
|
||||||
where TH: IDynamicIntegrationEventHandler
|
where TH : IDynamicIntegrationEventHandler
|
||||||
{
|
{
|
||||||
DoInternalSubscription(eventName);
|
DoInternalSubscription(eventName);
|
||||||
_subsManager.AddDynamicSubscription<TH>(eventName,handler);
|
_subsManager.AddDynamicSubscription<TH>(eventName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Subscribe<T, TH>(Func<TH> handler)
|
public void Subscribe<T, TH>()
|
||||||
where T : IntegrationEvent
|
where T : IntegrationEvent
|
||||||
where TH : IIntegrationEventHandler<T>
|
where TH : IIntegrationEventHandler<T>
|
||||||
{
|
{
|
||||||
var eventName = _subsManager.GetEventKey<T>();
|
var eventName = _subsManager.GetEventKey<T>();
|
||||||
DoInternalSubscription(eventName);
|
DoInternalSubscription(eventName);
|
||||||
_subsManager.AddSubscription<T, TH>(handler);
|
_subsManager.AddSubscription<T, TH>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DoInternalSubscription(string eventName)
|
private void DoInternalSubscription(string eventName)
|
||||||
@ -140,7 +144,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void UnsubscribeDynamic<TH>(string eventName)
|
public void UnsubscribeDynamic<TH>(string eventName)
|
||||||
where TH: IDynamicIntegrationEventHandler
|
where TH : IDynamicIntegrationEventHandler
|
||||||
{
|
{
|
||||||
_subsManager.RemoveDynamicSubscription<TH>(eventName);
|
_subsManager.RemoveDynamicSubscription<TH>(eventName);
|
||||||
}
|
}
|
||||||
@ -195,15 +199,17 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
|
|||||||
private async Task ProcessEvent(string eventName, string message)
|
private async Task ProcessEvent(string eventName, string message)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
if (_subsManager.HasSubscriptionsForEvent(eventName))
|
if (_subsManager.HasSubscriptionsForEvent(eventName))
|
||||||
|
{
|
||||||
|
using (var scope = _autofac.BeginLifetimeScope(AUTOFAC_SCOPE_NAME))
|
||||||
{
|
{
|
||||||
var subscriptions = _subsManager.GetHandlersForEvent(eventName);
|
var subscriptions = _subsManager.GetHandlersForEvent(eventName);
|
||||||
|
|
||||||
foreach (var subscription in subscriptions)
|
foreach (var subscription in subscriptions)
|
||||||
{
|
{
|
||||||
if (subscription.IsDynamic)
|
if (subscription.IsDynamic)
|
||||||
{
|
{
|
||||||
var handler = subscription.Factory.DynamicInvoke() as IDynamicIntegrationEventHandler;
|
var handler = scope.ResolveOptional(subscription.HandlerType) as IDynamicIntegrationEventHandler;
|
||||||
dynamic eventData = JObject.Parse(message);
|
dynamic eventData = JObject.Parse(message);
|
||||||
await handler.Handle(eventData);
|
await handler.Handle(eventData);
|
||||||
}
|
}
|
||||||
@ -211,7 +217,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
|
|||||||
{
|
{
|
||||||
var eventType = _subsManager.GetEventTypeByName(eventName);
|
var eventType = _subsManager.GetEventTypeByName(eventName);
|
||||||
var integrationEvent = JsonConvert.DeserializeObject(message, eventType);
|
var integrationEvent = JsonConvert.DeserializeObject(message, eventType);
|
||||||
var handler = subscription.Factory.DynamicInvoke();
|
var handler = scope.ResolveOptional(subscription.HandlerType);
|
||||||
var concreteType = typeof(IIntegrationEventHandler<>).MakeGenericType(eventType);
|
var concreteType = typeof(IIntegrationEventHandler<>).MakeGenericType(eventType);
|
||||||
await (Task)concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent });
|
await (Task)concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent });
|
||||||
}
|
}
|
||||||
@ -219,4 +225,5 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Autofac" Version="4.5.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
|
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
|
||||||
<PackageReference Include="Polly" Version="5.0.6" />
|
<PackageReference Include="Polly" Version="5.0.6" />
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.0.0" />
|
||||||
<PackageReference Include="System.Threading" Version="4.3.0" />
|
<PackageReference Include="System.Threading" Version="4.3.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />
|
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />
|
||||||
|
@ -23,6 +23,8 @@ using System.Threading.Tasks;
|
|||||||
using System;
|
using System;
|
||||||
using Microsoft.eShopOnContainers.Services.Basket.API.Services;
|
using Microsoft.eShopOnContainers.Services.Basket.API.Services;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Autofac;
|
||||||
|
using Autofac.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.Services.Basket.API
|
namespace Microsoft.eShopOnContainers.Services.Basket.API
|
||||||
{
|
{
|
||||||
@ -41,7 +43,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
|
|||||||
public IConfigurationRoot Configuration { get; }
|
public IConfigurationRoot Configuration { get; }
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to add services to the container.
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public IServiceProvider ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
|
||||||
services.AddHealthChecks(checks =>
|
services.AddHealthChecks(checks =>
|
||||||
@ -112,6 +114,10 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
|
|||||||
services.AddTransient<IBasketRepository, RedisBasketRepository>();
|
services.AddTransient<IBasketRepository, RedisBasketRepository>();
|
||||||
services.AddTransient<IIdentityService, IdentityService>();
|
services.AddTransient<IIdentityService, IdentityService>();
|
||||||
RegisterServiceBus(services);
|
RegisterServiceBus(services);
|
||||||
|
|
||||||
|
var container = new ContainerBuilder();
|
||||||
|
container.Populate(services);
|
||||||
|
return new AutofacServiceProvider(container.Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RegisterServiceBus(IServiceCollection services)
|
private void RegisterServiceBus(IServiceCollection services)
|
||||||
@ -167,11 +173,8 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
|
|||||||
|
|
||||||
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
||||||
|
|
||||||
eventBus.Subscribe<ProductPriceChangedIntegrationEvent, ProductPriceChangedIntegrationEventHandler>
|
eventBus.Subscribe<ProductPriceChangedIntegrationEvent, ProductPriceChangedIntegrationEventHandler>();
|
||||||
(() => app.ApplicationServices.GetRequiredService<ProductPriceChangedIntegrationEventHandler>());
|
eventBus.Subscribe<OrderStartedIntegrationEvent, OrderStartedIntegrationEventHandler>();
|
||||||
|
|
||||||
eventBus.Subscribe<OrderStartedIntegrationEvent, OrderStartedIntegrationEventHandler>
|
|
||||||
(() => app.ApplicationServices.GetRequiredService<OrderStartedIntegrationEventHandler>());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.0.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.1" />
|
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.1" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.Abstractions" Version="1.1.1" />
|
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.Abstractions" Version="1.1.1" />
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API
|
namespace Microsoft.eShopOnContainers.Services.Catalog.API
|
||||||
{
|
{
|
||||||
|
using Autofac;
|
||||||
|
using Autofac.Extensions.DependencyInjection;
|
||||||
using global::Catalog.API.Infrastructure.Filters;
|
using global::Catalog.API.Infrastructure.Filters;
|
||||||
using global::Catalog.API.IntegrationEvents;
|
using global::Catalog.API.IntegrationEvents;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
@ -44,7 +46,7 @@
|
|||||||
Configuration = builder.Build();
|
Configuration = builder.Build();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public IServiceProvider ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
// Add framework services.
|
// Add framework services.
|
||||||
|
|
||||||
@ -118,6 +120,10 @@
|
|||||||
|
|
||||||
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
||||||
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
||||||
|
|
||||||
|
var container = new ContainerBuilder();
|
||||||
|
container.Populate(services);
|
||||||
|
return new AutofacServiceProvider(container.Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.0.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.1.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.1.0" />
|
||||||
|
@ -19,6 +19,8 @@ using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
|
|||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.Extensions.HealthChecks;
|
using Microsoft.Extensions.HealthChecks;
|
||||||
using Identity.API.Certificate;
|
using Identity.API.Certificate;
|
||||||
|
using Autofac.Extensions.DependencyInjection;
|
||||||
|
using Autofac;
|
||||||
|
|
||||||
namespace eShopOnContainers.Identity
|
namespace eShopOnContainers.Identity
|
||||||
{
|
{
|
||||||
@ -44,7 +46,7 @@ namespace eShopOnContainers.Identity
|
|||||||
public IConfigurationRoot Configuration { get; }
|
public IConfigurationRoot Configuration { get; }
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to add services to the container.
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public IServiceProvider ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
|
||||||
// Add framework services.
|
// Add framework services.
|
||||||
@ -88,6 +90,10 @@ namespace eShopOnContainers.Identity
|
|||||||
.AddInMemoryClients(Config.GetClients(clientUrls))
|
.AddInMemoryClients(Config.GetClients(clientUrls))
|
||||||
.AddAspNetIdentity<ApplicationUser>()
|
.AddAspNetIdentity<ApplicationUser>()
|
||||||
.Services.AddTransient<IProfileService, ProfileService>();
|
.Services.AddTransient<IProfileService, ProfileService>();
|
||||||
|
|
||||||
|
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.
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
|
@ -7,6 +7,7 @@ using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Utilities
|
|||||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
|
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
|
||||||
using System;
|
using System;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Ordering.API.Application.IntegrationEvents
|
namespace Ordering.API.Application.IntegrationEvents
|
||||||
|
@ -129,7 +129,7 @@
|
|||||||
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
||||||
|
|
||||||
services.AddTransient<UserCheckoutAcceptedIntegrationEventHandler>();
|
services.AddTransient<UserCheckoutAcceptedIntegrationEventHandler>();
|
||||||
services.AddTransient<IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>, OrderProcessSaga>();
|
services.AddTransient<OrderProcessSaga>();
|
||||||
services.AddTransient<OrderStockConfirmedIntegrationEventHandler>();
|
services.AddTransient<OrderStockConfirmedIntegrationEventHandler>();
|
||||||
services.AddTransient<OrderStockNotConfirmedIntegrationEventHandler>();
|
services.AddTransient<OrderStockNotConfirmedIntegrationEventHandler>();
|
||||||
services.AddOptions();
|
services.AddOptions();
|
||||||
@ -156,14 +156,13 @@
|
|||||||
app.UseFailingMiddleware();
|
app.UseFailingMiddleware();
|
||||||
|
|
||||||
ConfigureAuth(app);
|
ConfigureAuth(app);
|
||||||
ConfigureEventBus(app);
|
|
||||||
|
|
||||||
app.UseMvcWithDefaultRoute();
|
app.UseMvcWithDefaultRoute();
|
||||||
|
|
||||||
app.UseSwagger()
|
app.UseSwagger()
|
||||||
.UseSwaggerUi();
|
.UseSwaggerUi();
|
||||||
|
|
||||||
OrderingContextSeed.SeedAsync(app).Wait();
|
OrderingContextSeed.SeedAsync(app).Wait();
|
||||||
|
ConfigureEventBus(app);
|
||||||
|
|
||||||
var integrationEventLogContext = new IntegrationEventLogContext(
|
var integrationEventLogContext = new IntegrationEventLogContext(
|
||||||
new DbContextOptionsBuilder<IntegrationEventLogContext>()
|
new DbContextOptionsBuilder<IntegrationEventLogContext>()
|
||||||
@ -176,18 +175,11 @@
|
|||||||
private void ConfigureEventBus(IApplicationBuilder app)
|
private void ConfigureEventBus(IApplicationBuilder app)
|
||||||
{
|
{
|
||||||
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
||||||
eventBus.SubscribeDynamic(
|
eventBus.SubscribeDynamic<UserCheckoutAcceptedIntegrationEventHandler>("UserCheckoutAccepted");
|
||||||
"UserCheckoutAccepted",
|
|
||||||
() => app.ApplicationServices.GetRequiredService<UserCheckoutAcceptedIntegrationEventHandler>());
|
|
||||||
|
|
||||||
eventBus.Subscribe<ConfirmGracePeriodCommandMsg, IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>>
|
eventBus.Subscribe<ConfirmGracePeriodCommandMsg, OrderProcessSaga>();
|
||||||
(() => app.ApplicationServices.GetRequiredService<IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>>());
|
eventBus.Subscribe<OrderStockConfirmedIntegrationEvent, OrderStockConfirmedIntegrationEventHandler>();
|
||||||
|
eventBus.Subscribe<OrderStockNotConfirmedIntegrationEvent, OrderStockNotConfirmedIntegrationEventHandler>();
|
||||||
eventBus.Subscribe<OrderStockConfirmedIntegrationEvent, OrderStockConfirmedIntegrationEventHandler>
|
|
||||||
(() => app.ApplicationServices.GetRequiredService<OrderStockConfirmedIntegrationEventHandler>());
|
|
||||||
|
|
||||||
eventBus.Subscribe<OrderStockNotConfirmedIntegrationEvent, OrderStockNotConfirmedIntegrationEventHandler>
|
|
||||||
(() => app.ApplicationServices.GetRequiredService<OrderStockNotConfirmedIntegrationEventHandler>());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void ConfigureAuth(IApplicationBuilder app)
|
protected virtual void ConfigureAuth(IApplicationBuilder app)
|
||||||
|
@ -38,7 +38,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
|||||||
|
|
||||||
private int? _paymentMethodId;
|
private int? _paymentMethodId;
|
||||||
|
|
||||||
protected Order() { }
|
protected Order() { _orderItems = new List<OrderItem>(); }
|
||||||
|
|
||||||
public Order(Address address, int cardTypeId, string cardNumber, string cardSecurityNumber,
|
public Order(Address address, int cardTypeId, string cardNumber, string cardSecurityNumber,
|
||||||
string cardHolderName, DateTime cardExpiration, int? buyerId = null, int? paymentMethodId = null)
|
string cardHolderName, DateTime cardExpiration, int? buyerId = null, int? paymentMethodId = null)
|
||||||
|
@ -36,6 +36,9 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
|
|||||||
public OrderingContext(DbContextOptions options, IMediator mediator) : base(options)
|
public OrderingContext(DbContextOptions options, IMediator mediator) : base(options)
|
||||||
{
|
{
|
||||||
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
||||||
|
|
||||||
|
|
||||||
|
System.Diagnostics.Debug.WriteLine("OrderingContext::ctor ->" + this.GetHashCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
<Folder Include="wwwroot\" />
|
<Folder Include="wwwroot\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.0.0" />
|
||||||
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
|
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
|
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
using Microsoft.AspNetCore.Builder;
|
using Autofac;
|
||||||
|
using Autofac.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace Payment.API
|
namespace Payment.API
|
||||||
{
|
{
|
||||||
@ -21,7 +24,7 @@ namespace Payment.API
|
|||||||
public IConfigurationRoot Configuration { get; }
|
public IConfigurationRoot Configuration { get; }
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to add services to the container.
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public IServiceProvider ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
// Add framework services.
|
// Add framework services.
|
||||||
services.AddMvc();
|
services.AddMvc();
|
||||||
@ -38,6 +41,10 @@ namespace Payment.API
|
|||||||
TermsOfService = "Terms Of Service"
|
TermsOfService = "Terms Of Service"
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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.
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
|
@ -16,7 +16,8 @@ namespace SagaManager
|
|||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using RabbitMQ.Client;
|
using RabbitMQ.Client;
|
||||||
using Services;
|
using Services;
|
||||||
|
using Autofac.Extensions.DependencyInjection;
|
||||||
|
using Autofac;
|
||||||
|
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
@ -76,6 +77,10 @@ namespace SagaManager
|
|||||||
|
|
||||||
RegisterServiceBus(services);
|
RegisterServiceBus(services);
|
||||||
|
|
||||||
|
var container = new ContainerBuilder();
|
||||||
|
container.Populate(services);
|
||||||
|
return new AutofacServiceProvider(container.Build());
|
||||||
|
|
||||||
return services.BuildServiceProvider();
|
return services.BuildServiceProvider();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.0.0" />
|
||||||
<PackageReference Include="Dapper" Version="1.50.2" />
|
<PackageReference Include="Dapper" Version="1.50.2" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="1.1.1" />
|
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="1.1.1" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.1" />
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.1" />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user