Example #1
0
        private void ExecutingQuery(HandlerDescriptor handlerDescriptor, Envelope <IQuery> envelope)
        {
            var traceInfo = (TraceInfo)envelope.Items[StandardMetadata.TraceInfo];

            var methodInvocation = new MethodInvocation(handlerDescriptor.Target, handlerDescriptor.Method, envelope.Body);

            methodInvocation.InvocationContext[StandardMetadata.TraceInfo] = traceInfo;
            methodInvocation.InvocationContext[StandardMetadata.MessageId] = envelope.MessageId;

            try {
                var methodReturn = _pipelineManager.GetPipeline(handlerDescriptor.Method).Invoke(methodInvocation,
                                                                                                 delegate(IMethodInvocation input, GetNextHandlerDelegate getNext) {
                    var result = handlerDescriptor.Invode(input.Arguments.Cast <object>().ToArray());
                    return(input.CreateMethodReturn(result));
                });
                NotifyResult(traceInfo, methodReturn.Exception ?? methodReturn.ReturnValue);
            }
            catch (Exception ex) {
                NotifyResult(traceInfo, ex);
            }
        }
Example #2
0
        private bool ProcessEvent(HandlerDescriptor handlerDescriptor, IVersionedEvent @event, string eventId,
                                  IDictionary <string, object> items)
        {
            var sourceInfo = (SourceInfo)items[StandardMetadata.SourceInfo];
            var traceInfo  = (TraceInfo)items[StandardMetadata.TraceInfo];

            if (@event.Version > 1)
            {
                var lastPublishedVersion = _publishedVersionStore.GetPublishedVersion(sourceInfo) + 1;
                if (lastPublishedVersion < @event.Version)
                {
                    var envelope = new Envelope <IEvent>(@event, eventId)
                    {
                        Items = items
                    };
                    _eventBus.Send(envelope);

                    if (LogManager.Default.IsDebugEnabled)
                    {
                        LogManager.Default.DebugFormat(
                            "The event cannot be process now as the version is not the next version, it will be handle later. aggregateRootType={0},aggregateRootId={1},lastPublishedVersion={2},eventVersion={3},eventType={4}.",
                            sourceInfo.Type.FullName,
                            sourceInfo.Id,
                            lastPublishedVersion,
                            @event.Version,
                            @event.GetType().FullName);
                    }

                    return(false);
                }

                if (lastPublishedVersion > @event.Version)
                {
                    if (LogManager.Default.IsDebugEnabled)
                    {
                        LogManager.Default.DebugFormat(
                            "The event is ignored because it is obsoleted. aggregateRootType={0},aggregateRootId={1},lastPublishedVersion={2},eventVersion={3},eventType={4}.",
                            sourceInfo.Type.FullName,
                            sourceInfo.Id,
                            lastPublishedVersion,
                            @event.Version,
                            @event.GetType().FullName);
                    }

                    return(false);
                }
            }

            var context = new EventContext(_commandBus, _resultBus);

            context.SourceInfo = sourceInfo;
            context.TraceInfo  = traceInfo;
            if (items.ContainsKey(StandardMetadata.CommandInfo))
            {
                context.CommandInfo = (SourceInfo)items[StandardMetadata.CommandInfo];
            }

            try {
                handlerDescriptor.Invode(context, @event);
                context.Commit();
            }
            catch (Exception ex)
            {
                var commandResult = new CommandResult(HandleStatus.SyncFailed, ex.Message)
                {
                    Result    = _serializer.Serialize(ex.Data),
                    ReplyType = CommandReturnMode.EventHandled
                };
                _resultBus.Send(commandResult, traceInfo);
                return(false);
            }

            _publishedVersionStore.AddOrUpdatePublishedVersion(sourceInfo, @event.Version);

            return(true);
        }