public async Task <Result> TriggerAsync(IBusinessEvent businessEvent, CancellationToken cancellationToken = default) { var eventType = businessEvent.GetType(); var handlerType = typeof(IBusinessEventHandler <>).MakeGenericType(eventType); var method = handlerType.GetMethod(MethodName, new[] { eventType, typeof(CancellationToken) }); if (method == null) { throw new InvalidOperationException(); } var handlers = _provider.GetServices(handlerType); foreach (var handler in handlers) { var result = await(Task <Result>) method.Invoke(handler, new object[] { businessEvent, cancellationToken }); if (result.Failed) { return(result); } } return(Result.Ok()); }
public SubscriberEvent(string regionId, string streamId, long position, string subscriptionStreamId, long subscriptionPosition, string eventType, IBusinessEvent resolvedEvent) { RegionId = regionId; StreamId = streamId; Position = position; SubscriptionStreamId = subscriptionStreamId; SubscriptionPosition = subscriptionPosition; EventType = eventType; IsResolved = resolvedEvent != null; ResolvedEventType = resolvedEvent?.GetType(); ResolvedEvent = resolvedEvent; }
public UnresolvedBusinessEvent Unresolve(IBusinessEvent e) { var typeName = _strongTypesToTypeNames[e.GetType()]; // Allow to throw unhandled exception. try { var json = JsonConvert.SerializeObject(e); var data = Encoding.Unicode.GetBytes(json); return(new UnresolvedBusinessEvent(typeName, data)); } catch (Exception ex) { _logger.LogError(ex, "Exception while unresolving business event."); return(null); // Intentionally resolve to null when serialization or encoding exception. } }
/// <remarks> /// This emulates an external service subscriber reacting to events published on the service bus. /// </remarks> public void HandleEvent(IBusinessEvent businessEvent) { var eventType = businessEvent.GetType().FullName; switch (eventType) { case "BusinessRulesEngine.Domain.Models.Events.PackingSlipCreated": case "BusinessRulesEngine.Domain.Models.Events.PackingSlipDuplicated": case "BusinessRulesEngine.Domain.Models.Events.MembershipActivated": case "BusinessRulesEngine.Domain.Models.Events.MembershipUpgraded": case "BusinessRulesEngine.Domain.Models.Events.CommissionPaymentGenerated": case "BusinessRulesEngine.Domain.Models.Events.EmailMembershipOwner": LogEvent(businessEvent, MessageType.Success); break; case "BusinessRulesEngine.Domain.Models.Events.PackingSlipUpdated": LogEvent(businessEvent, MessageType.Warning); break; default: throw new Exception($"Unknown event type [{eventType}]"); } }
public async Task <Result> TriggerAsync(IBusinessEvent businessEvent) { var eventType = businessEvent.GetType(); var handlerType = typeof(IBusinessEventHandler <>).MakeGenericType(eventType); foreach (var handler in _provider.GetServices(handlerType)) { var method = handlerType.GetMethod(MethodName, new[] { eventType }); if (method == null) { continue; } var result = await(Task <Result>) method.Invoke(handler, new object[] { businessEvent }); if (result.Failed) { return(result); } } return(Result.Ok()); }
public bool CanUnresolve(IBusinessEvent e) => _strongTypesToTypeNames.ContainsKey(e.GetType());