Exemple #1
0
        public async Task Consume(ConsumeContext <DispatchMessage> context)
        {
            if (_log.IsDebugEnabled)
            {
                _log.DebugFormat("DispatchMessage: {0} at {1}", context.Message.DispatchId, context.Message.CreateTime);
            }

            string body = Encoding.UTF8.GetString(context.ReceiveContext.GetBody());

            if (string.Compare(context.ReceiveContext.ContentType.MediaType, JsonMessageSerializer.JsonContentType.MediaType,
                               StringComparison.OrdinalIgnoreCase)
                == 0)
            {
                body = TranslateJsonBody(body, context.Message.Destination.ToString());
            }
            else if (string.Compare(context.ReceiveContext.ContentType.MediaType, XmlMessageSerializer.XmlContentType.MediaType,
                                    StringComparison.OrdinalIgnoreCase) == 0)
            {
                body = TranslateXmlBody(body, context.Message.Destination.ToString());
            }
            else
            {
                throw new InvalidOperationException("Only JSON and XML messages can be scheduled");
            }

            var dispatchContext = new ConsumerDispatchContext(context, body);

            await _agent.Execute(dispatchContext);
        }
        public void Consume(IConsumeContext <DispatchMessage> context)
        {
            if (_log.IsDebugEnabled)
            {
                _log.DebugFormat("DispatchMessage: {0} at {1}", context.Message.DispatchId,
                                 context.Message.CreateTime);
            }

            string body;

            using (var ms = new MemoryStream())
            {
                context.BaseContext.CopyBodyTo(ms);

                body = Encoding.UTF8.GetString(ms.ToArray());
            }

            if (string.Compare(context.ContentType, "application/vnd.masstransit+json",
                               StringComparison.OrdinalIgnoreCase)
                == 0)
            {
                body = TranslateJsonBody(body, context.Message.Destination.ToString());
            }
            else if (string.Compare(context.ContentType, "application/vnd.masstransit+xml",
                                    StringComparison.OrdinalIgnoreCase) == 0)
            {
                body = TranslateXmlBody(body, context.Message.Destination.ToString());
            }
            else
            {
                throw new InvalidOperationException("Only JSON and XML messages can be executed");
            }

            var executionContext = new ConsumerDispatchContext(context, body);

            _agent.Execute(executionContext);
        }
        public async Task Consume(ConsumeContext <DispatchMessage> context)
        {
            string body = Encoding.UTF8.GetString(context.ReceiveContext.GetBody());

            var mediaType = context.ReceiveContext.ContentType?.MediaType;

            if (JsonMessageSerializer.JsonContentType.MediaType.Equals(mediaType, StringComparison.OrdinalIgnoreCase))
            {
                body = TranslateJsonBody(body, context.Message.Destination.ToString());
            }

            else if (XmlMessageSerializer.XmlContentType.MediaType.Equals(mediaType, StringComparison.OrdinalIgnoreCase))
            {
                body = TranslateXmlBody(body, context.Message.Destination.ToString());
            }
            else
            {
                throw new InvalidOperationException("Only JSON and XML messages can be scheduled");
            }

            var dispatchContext = new ConsumerDispatchContext(context, body);

            await _agent.Execute(dispatchContext);
        }