private async Task InvokeConsumerMethodAsync(CapReceivedMessage receivedMessage)
        {
            if (!_selector.TryGetTopicExecutor(receivedMessage.Name, receivedMessage.Group,
                                               out var executor))
            {
                var error = $"Message can not be found subscriber. {receivedMessage} \r\n see: https://github.com/dotnetcore/CAP/issues/63";
                throw new SubscriberNotFoundException(error);
            }

            var startTime   = DateTimeOffset.UtcNow;
            var stopwatch   = Stopwatch.StartNew();
            var operationId = Guid.Empty;

            var consumerContext = new ConsumerContext(executor, receivedMessage.ToMessageContext());

            try
            {
                operationId = s_diagnosticListener.WriteSubscriberInvokeBefore(consumerContext);

                var ret = await Invoker.InvokeAsync(consumerContext);

                s_diagnosticListener.WriteSubscriberInvokeAfter(operationId, consumerContext, startTime, stopwatch.Elapsed);

                if (!string.IsNullOrEmpty(ret.CallbackName))
                {
                    await _callbackMessageSender.SendAsync(ret.MessageId, ret.CallbackName, ret.Result);
                }
            }
            catch (Exception ex)
            {
                s_diagnosticListener.WriteSubscriberInvokeError(operationId, consumerContext, ex, startTime, stopwatch.Elapsed);

                throw new SubscriberExecutionFailedException(ex.Message, ex);
            }
        }
Beispiel #2
0
        private void RegisterMessageProcessor(IConsumerClient client)
        {
            client.OnMessageReceived += async delegate(object sender, TransportMessage transportMessage)
            {
                long?tracingTimestamp = null;
                try
                {
                    tracingTimestamp = TracingBefore(transportMessage, _serverAddress);
                    string name  = transportMessage.GetName();
                    string group = transportMessage.GetGroup();
                    ConsumerExecutorDescriptor executor;
                    bool    canFindSubscriber = _selector.TryGetTopicExecutor(name, group, out executor);
                    Message message;
                    try
                    {
                        if (!canFindSubscriber)
                        {
                            SubscriberNotFoundException ex = new SubscriberNotFoundException("Message can not be found subscriber. Name:" + name + ", Group:" + group + ". " + " see: https://github.com/dotnetcore/CAP/issues/63");
                            TracingError(tracingTimestamp, transportMessage, client.BrokerAddress, ex);
                            throw ex;
                        }
                        Type type = Enumerable.FirstOrDefault <ParameterDescriptor>((IEnumerable <ParameterDescriptor>)executor.Parameters, (Func <ParameterDescriptor, bool>)((ParameterDescriptor x) => !x.IsFromCap))?.ParameterType;
                        message = await _serializer.DeserializeAsync(transportMessage, type);

                        message.RemoveException();
                    }
                    catch (Exception e3)
                    {
                        transportMessage.Headers.Add("cap-exception", "SerializationException-->" + e3.Message);
                        if (transportMessage.Headers.TryGetValue("cap-msg-type", out var val))
                        {
                            string dataUri2 = "data:" + val + ";base64," + Convert.ToBase64String(transportMessage.Body);
                            message = new Message(transportMessage.Headers, dataUri2);
                        }
                        else
                        {
                            string dataUri = "data:UnknownType;base64," + Convert.ToBase64String(transportMessage.Body);
                            message = new Message(transportMessage.Headers, dataUri);
                        }
                    }
                    if (message.HasException())
                    {
                        string content = _serializer.Serialize(message);
                        _storage.StoreReceivedExceptionMessage(name, group, content);
                        client.Commit(sender);
                        try
                        {
                            _options.FailedThresholdCallback?.Invoke(new FailedInfo
                            {
                                ServiceProvider = _serviceProvider,
                                MessageType     = MessageType.Subscribe,
                                Message         = message
                            });
                        }
                        catch (Exception e2)
                        {
                        }
                        TracingAfter(tracingTimestamp, transportMessage, _serverAddress);
                    }
                    else
                    {
                        MediumMessage mediumMessage = _storage.StoreReceivedMessage(name, group, message);
                        mediumMessage.Origin = message;

                        TracingAfter(tracingTimestamp, transportMessage, _serverAddress);
                        _dispatcher.EnqueueToExecute(mediumMessage, executor);
                        client.Commit(sender);
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "An exception occurred when process received message. Message:'{0}'.", transportMessage);
                    client.Reject(sender);
                    TracingError(tracingTimestamp, transportMessage, client.BrokerAddress, e);
                }
            };
            client.OnLog += WriteLog;
        }