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()); }
private void LogEvent(IBusinessEvent packingSlipEvent, MessageType messageType) { _logger.Log(packingSlipEvent.Message, messageType); if (packingSlipEvent.Data is PackingSlip packingSlip && packingSlip.Products.Count > 0) { LogPackingSlipRows(packingSlip); } }
// Not thread safe. public async Task ManageAsync(CancellationToken cancellationToken) { try { while (!cancellationToken.IsCancellationRequested) { ResolutionStreamEvent resolutionStreamEvent; while (!cancellationToken.IsCancellationRequested && _resolutionQueue.TryDequeue(out resolutionStreamEvent)) { var streamEvent = resolutionStreamEvent.StreamEvent; IBusinessEvent businessEvent = null; try { // If unable to resolve event type then subscriber event will still be created but it // will be unresolved. if (_resolver.CanResolve(streamEvent.EventType)) { businessEvent = _resolver.Resolve(streamEvent.EventType, streamEvent.Data); } } catch (Exception ex) { _logger.LogError(ex, "Exception while resolving business event. Subscription event will be created with null business event."); } // Stream events may (and will for subscription streams) have links to original events. // We want the original event as the core event and the subscription event info as secondary. // However, if the event is not a link then we take the event info as both subscription info and original event. var streamId = streamEvent.StreamId; var position = streamEvent.Position; var subscriptionStreamId = streamEvent.StreamId; var subscriptionPosition = streamEvent.Position; if (streamEvent.IsLink) { streamId = streamEvent.Link.StreamId; position = streamEvent.Link.Position; } var subscriberEvent = new SubscriberEvent(resolutionStreamEvent.RegionId, streamId, position, subscriptionStreamId, subscriptionPosition, streamEvent.EventType, businessEvent); // Send to the sorting manager. await _sortingManager.ReceiveSubscriberEventAsync(subscriberEvent, cancellationToken); } await Task.WhenAny(new Task[] { _resolutionQueue.AwaitEnqueueSignalAsync(), cancellationToken.WaitHandle.AsTask() }); } } catch (Exception ex) { _logger.LogError(ex, "Exception while managing resolution."); throw; } }
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 virtual async Task InvokeTypedBusinessEventHandlerAsync(string streamId, long position, IBusinessEvent e, CancellationToken cancellationToken) { // Expects IApplyBusinessEvent<TEvent> for the type of event given. await(Task) this.GetType().InvokeMember("ApplyBusinessEventAsync", BindingFlags.InvokeMethod, null, this, new object[] { streamId, position, e, cancellationToken }); }
public void PublishEvent(IBusinessEvent businessEvent) { _events.Add(businessEvent); _externalServiceEmulator.HandleEvent(businessEvent); }
public Task <Result> TriggerAsync(IBusinessEvent businessEvent) { return(Task.FromResult(Result.Ok())); }
public virtual void AddBusinessEvent(IBusinessEvent businessEvent) { _businessEvents.Add(businessEvent); }
public static Task <ICommandResult> FromEventIAsync(IBusinessEvent e) => Task.FromResult <ICommandResult>(FromEvent(e));
public static CommandResult FromEvent(IBusinessEvent e) => new CommandResult(new List <IBusinessEvent>() { e });
public void AuditFailure(IBusinessEvent businessEvent) { _logger.Error(businessEvent); }
public void AuditSuccess(IBusinessEvent businessEvent) { _logger.Info(businessEvent); }
public Task <Result> TriggerAsync(IBusinessEvent businessEvent, CancellationToken cancellationToken = default) { return(Task.FromResult(Result.Ok())); }
public bool CanUnresolve(IBusinessEvent e) => _strongTypesToTypeNames.ContainsKey(e.GetType());
public UserController(IBusinessEvent userBusinessEvent) { this._userBusinessEvent = userBusinessEvent; this._userBusinessEvent.Subscribe(); }
public SubscriberEvent(string regionId, string streamId, long position, string eventType, IBusinessEvent resolvedEvent) : this(regionId, streamId, position, streamId, position, eventType, resolvedEvent) { }