Ejemplo n.º 1
0
        public async Task Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancelSend) where T : class
        {
            var context = new HttpSendContextImpl <T>(message, cancelSend);
            await pipe.Send(context).ConfigureAwait(false);

            _owinContext.Response.Headers["Content-Type"] = context.ContentType.MediaType;
            foreach (var header in context.Headers.GetAll().Where(h => h.Value != null && (h.Value is string || h.Value.GetType().IsValueType)))
            {
                _owinContext.Response.Headers[header.Key] = header.Value.ToString();
            }

            if (context.MessageId.HasValue)
            {
                _owinContext.Response.Headers[HttpHeaders.MessageId] = context.MessageId.Value.ToString();
            }

            if (context.CorrelationId.HasValue)
            {
                _owinContext.Response.Headers[HttpHeaders.CorrelationId] = context.CorrelationId.Value.ToString();
            }

            if (context.InitiatorId.HasValue)
            {
                _owinContext.Response.Headers[HttpHeaders.InitiatorId] = context.InitiatorId.Value.ToString();
            }

            if (context.ConversationId.HasValue)
            {
                _owinContext.Response.Headers[HttpHeaders.ConversationId] = context.ConversationId.Value.ToString();
            }

            if (context.RequestId.HasValue)
            {
                _owinContext.Response.Headers[HttpHeaders.RequestId] = context.RequestId.Value.ToString();
            }

            _owinContext.Response.StatusCode = (int)HttpStatusCode.OK;
            _owinContext.Response.Write(context.Body);

            // ??
        }
Ejemplo n.º 2
0
        public async Task Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancelSend) where T : class
        {
            IPipe <ClientContext> clientPipe = Pipe.New <ClientContext>(p =>
            {
                //TODO: p.UseFilter(_filter);

                p.UseExecuteAsync(async clientContext =>
                {
                    //sendSettings
                    var method  = HttpMethod.Post;
                    var timeOut = TimeSpan.FromSeconds(5);

                    var context = new HttpSendContextImpl <T>(message, cancelSend);

                    try
                    {
                        await pipe.Send(context).ConfigureAwait(false);

                        using (var msg = new HttpRequestMessage(_sendSettings.Method, context.DestinationAddress))
                            using (var payload = new ByteArrayContent(context.Body))
                            {
                                //TODO: Get access to a HostInfo instance
                                msg.Headers.UserAgent.Add(new ProductInfoHeaderValue("MassTransit", Version));

                                if (context.ResponseAddress != null)
                                {
                                    msg.Headers.Referrer = context.ResponseAddress;
                                }

                                payload.Headers.ContentType = new MediaTypeHeaderValue(context.ContentType.MediaType);

                                foreach (
                                    KeyValuePair <string, object> header in
                                    context.Headers.GetAll().Where(h => h.Value != null && (h.Value is string || h.Value.GetType().IsValueType)))
                                {
                                    msg.Headers.Add(header.Key, header.Value.ToString());
                                }

                                if (context.MessageId.HasValue)
                                {
                                    msg.Headers.Add(HttpHeaders.MessageId, context.MessageId.Value.ToString());
                                }

                                if (context.CorrelationId.HasValue)
                                {
                                    msg.Headers.Add(HttpHeaders.CorrelationId, context.CorrelationId.Value.ToString());
                                }

                                if (context.InitiatorId.HasValue)
                                {
                                    msg.Headers.Add(HttpHeaders.InitiatorId, context.InitiatorId.Value.ToString());
                                }

                                if (context.ConversationId.HasValue)
                                {
                                    msg.Headers.Add(HttpHeaders.ConversationId, context.ConversationId.Value.ToString());
                                }

                                if (context.RequestId.HasValue)
                                {
                                    msg.Headers.Add(HttpHeaders.RequestId, context.RequestId.Value.ToString());
                                }

                                //TODO: TTL?

                                msg.Content = payload;

                                await _observers.PreSend(context).ConfigureAwait(false);

                                using (var response = await clientContext.SendAsync(msg, cancelSend).ConfigureAwait(false))
                                {
                                    var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                                    if (responseStream.Length > 0)
                                    {
                                        ISendEndpointProvider sendEndpointProvider       = new HttpClientSendEndpointProvider();
                                        IPublishEndpointProvider publishEndpointProvider = new HttpClientPublishEndpointProvider();

                                        var receiveContext = new HttpClientReceiveContext(response, responseStream, false, _receiveObserver, sendEndpointProvider,
                                                                                          publishEndpointProvider);

                                        await clientContext.ReceiveResponse(receiveContext).ConfigureAwait(false);
                                    }
                                }

                                await _observers.PostSend(context).ConfigureAwait(false);
                            }
                    }
                    catch (Exception ex)
                    {
                        await _observers.SendFault(context, ex).ConfigureAwait(false);

                        if (_log.IsErrorEnabled)
                        {
                            _log.Error("Send Fault: " + context.DestinationAddress, ex);
                        }

                        throw;
                    }
                });
            });

            await _clientCache.DoWith(clientPipe, cancelSend).ConfigureAwait(false);
        }