Esempio n. 1
0
            private async Task LocalObjectMessagePumpAsync()
            {
                while (true)
                {
                    try
                    {
                        Message message;
                        lock (this.Messages)
                        {
                            if (this.Messages.Count == 0)
                            {
                                this.Running = false;
                                break;
                            }

                            message = this.Messages.Dequeue();
                        }

                        if (message.IsExpired)
                        {
                            _manager.messagingTrace.OnDropExpiredMessage(message, MessagingStatisticsGroup.Phase.Invoke);
                            continue;
                        }

                        RequestContextExtensions.Import(message.RequestContextData);
                        IInvokable request = null;
                        try
                        {
                            request = (IInvokable)message.BodyObject;
                        }
                        catch (Exception deserializationException)
                        {
                            if (_manager.logger.IsEnabled(LogLevel.Warning))
                            {
                                _manager.logger.LogWarning(
                                    deserializationException,
                                    "Exception during message body deserialization in " + nameof(LocalObjectMessagePumpAsync) + " for message: {Message}",
                                    message);
                            }

                            _manager.runtimeClient.SendResponse(message, Response.FromException(deserializationException));
                            continue;
                        }

                        try
                        {
                            request.SetTarget(this);
                            var response = await request.Invoke();

                            if (message.Direction != Message.Directions.OneWay)
                            {
                                this.SendResponseAsync(message, response);
                            }
                        }
                        catch (Exception exc)
                        {
                            this.ReportException(message, exc);
                        }
                    }
                    catch (Exception outerException)
                    {
                        // ignore, keep looping.
                        _manager.logger.LogWarning(
                            outerException,
                            "Exception in " + nameof(LocalObjectMessagePumpAsync));
                    }
                    finally
                    {
                        RequestContext.Clear();
                    }
                }
            }