Ejemplo n.º 1
0
 /// <summary>
 /// Gives an opportunity to IBatchContainer to set any data in the RequestContext before this IBatchContainer is sent to consumers.
 /// It can be the data that was set at the time event was generated and enqueued into the persistent provider or any other data.
 /// </summary>
 /// <returns>True if the RequestContext was indeed modified, false otherwise.</returns>
 public bool ImportRequestContext()
 {
     if (GetPayload().RequestContext != null)
     {
         RequestContextExtensions.Import(GetPayload().RequestContext);
         return(true);
     }
     return(false);
 }
Ejemplo n.º 2
0
 public bool ImportRequestContext()
 {
     if (_requestContext != null)
     {
         RequestContextExtensions.Import(_requestContext);
         return(true);
     }
     return(false);
 }
Ejemplo n.º 3
0
        private async Task LocalObjectMessagePumpAsync(LocalObjectData objectData)
        {
            while (true)
            {
                try
                {
                    Message message;
                    lock (objectData.Messages)
                    {
                        if (objectData.Messages.Count == 0)
                        {
                            objectData.Running = false;
                            break;
                        }
                        message = objectData.Messages.Dequeue();
                    }

                    if (ExpireMessageIfExpired(message, MessagingStatisticsGroup.Phase.Invoke))
                    {
                        continue;
                    }

                    RequestContextExtensions.Import(message.RequestContextData);
                    var       request      = (InvokeMethodRequest)message.GetDeserializedBody(this.SerializationManager);
                    var       targetOb     = (IAddressable)objectData.LocalObject.Target;
                    object    resultObject = null;
                    Exception caught       = null;
                    try
                    {
                        // exceptions thrown within this scope are not considered to be thrown from user code
                        // and not from runtime code.
                        var resultPromise = objectData.Invoker.Invoke(targetOb, request);
                        if (resultPromise != null) // it will be null for one way messages
                        {
                            resultObject = await resultPromise;
                        }
                    }
                    catch (Exception exc)
                    {
                        // the exception needs to be reported in the log or propagated back to the caller.
                        caught = exc;
                    }
                    if (caught != null)
                    {
                        this.ReportException(message, caught);
                    }
                    else if (message.Direction != Message.Directions.OneWay)
                    {
                        await this.SendResponseAsync(message, resultObject);
                    }
                }
                catch (Exception)
                {
                    // ignore, keep looping.
                }
            }
        }
Ejemplo n.º 4
0
        public bool ImportRequestContext()
        {
            var context = Payload().RequestContext;

            if (context != null)
            {
                RequestContextExtensions.Import(context);
                return(true);
            }
            return(false);
        }
        public void RequestContext_ActivityId_ExportImport()
        {
            Guid activityId     = Guid.NewGuid();
            Guid activityId2    = Guid.NewGuid();
            Guid nullActivityId = Guid.Empty;

            Message msg = new Message();

            msg.RequestContextData = RequestContextExtensions.Export(this.fixture.SerializationManager);
            RequestContext.Clear();
            RequestContextExtensions.Import(msg.RequestContextData);
            var actId = RequestContext.Get(RequestContext.E2_E_TRACING_ACTIVITY_ID_HEADER);

            Assert.Null(actId);
            TestCleanup();

            RequestContextTestUtils.SetActivityId(activityId);
            msg = new Message();
            msg.RequestContextData = RequestContextExtensions.Export(this.fixture.SerializationManager);
            RequestContext.Clear();
            RequestContextExtensions.Import(msg.RequestContextData);
            actId = RequestContext.Get(RequestContext.E2_E_TRACING_ACTIVITY_ID_HEADER);
            if (msg.RequestContextData != null)
            {
                foreach (var kvp in msg.RequestContextData)
                {
                    headers.Add(kvp.Key, kvp.Value);
                }
            }
            ;
            Assert.NotNull(actId); // "ActivityId #1 should be present " + headers.ToStrings(separator: ",")
            object result = headers[RequestContext.E2_E_TRACING_ACTIVITY_ID_HEADER];

            Assert.NotNull(result);                                            // "ActivityId #1 should not be null"
            Assert.Equal(activityId, result);                                  // "E2E ActivityId #1 not propagated correctly"
            Assert.Equal(activityId, RequestContextTestUtils.GetActivityId()); // "Original E2E ActivityId #1 should not have changed"
            TestCleanup();

            RequestContextTestUtils.SetActivityId(nullActivityId);
            msg = new Message();
            msg.RequestContextData = RequestContextExtensions.Export(this.fixture.SerializationManager);
            RequestContext.Clear();
            RequestContextExtensions.Import(msg.RequestContextData);
            actId = RequestContext.Get(RequestContext.E2_E_TRACING_ACTIVITY_ID_HEADER);
            Assert.Null(actId);
            TestCleanup();

            RequestContextTestUtils.SetActivityId(activityId2);
            msg = new Message();
            msg.RequestContextData = RequestContextExtensions.Export(this.fixture.SerializationManager);
            RequestContext.Clear();
            RequestContextExtensions.Import(msg.RequestContextData);
            actId = RequestContext.Get(RequestContext.E2_E_TRACING_ACTIVITY_ID_HEADER);
            if (msg.RequestContextData != null)
            {
                foreach (var kvp in msg.RequestContextData)
                {
                    headers.Add(kvp.Key, kvp.Value);
                }
            }
            ;
            Assert.NotNull(actId);                                              // "ActivityId #2 should be present " + headers.ToStrings(separator: ",")
            result = headers[RequestContext.E2_E_TRACING_ACTIVITY_ID_HEADER];
            Assert.NotNull(result);                                             // "ActivityId #2 should not be null"
            Assert.Equal(activityId2, result);                                  // "E2E ActivityId #2 not propagated correctly

            Assert.Equal(activityId2, RequestContextTestUtils.GetActivityId()); // "Original E2E ActivityId #2 should not have changed"
            TestCleanup();
        }
Ejemplo n.º 6
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();
                    }
                }
            }
Ejemplo n.º 7
0
        private async Task LocalObjectMessagePumpAsync(LocalObjectData objectData)
        {
            while (true)
            {
                try
                {
                    Message message;
                    lock (objectData.Messages)
                    {
                        if (objectData.Messages.Count == 0)
                        {
                            objectData.Running = false;
                            break;
                        }

                        message = objectData.Messages.Dequeue();
                    }

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

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

                        this.runtimeClient.SendResponse(message, Response.ExceptionResponse(deserializationException));
                        continue;
                    }

                    var       targetOb     = (IAddressable)objectData.LocalObject.Target;
                    object    resultObject = null;
                    Exception caught       = null;
                    try
                    {
                        // exceptions thrown within this scope are not considered to be thrown from user code
                        // and not from runtime code.
                        var resultPromise = objectData.Invoker.Invoke(objectData, request);
                        if (resultPromise != null) // it will be null for one way messages
                        {
                            resultObject = await resultPromise;
                        }
                    }
                    catch (Exception exc)
                    {
                        // the exception needs to be reported in the log or propagated back to the caller.
                        caught = exc;
                    }

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