public async Task Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken) where T : class { IPipe <ClientContext> clientPipe = Pipe.New <ClientContext>(p => { p.UseExecuteAsync(async clientContext => { var context = new HttpSendContextImpl <T>(message, cancellationToken); try { await pipe.Send(context).ConfigureAwait(false); using (var request = new HttpRequestMessage(_sendSettings.Method, context.DestinationAddress)) using (var payload = new ByteArrayContent(context.Body)) { request.Headers.UserAgent.Add(new ProductInfoHeaderValue("MassTransit", HostMetadataCache.Host.MassTransitVersion)); if (context.ResponseAddress != null) { request.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))) { request.Headers.Add(header.Key, header.Value.ToString()); } if (context.MessageId.HasValue) { request.Headers.Add(Clients.HttpHeaders.MessageId, context.MessageId.Value.ToString()); } if (context.CorrelationId.HasValue) { request.Headers.Add(Clients.HttpHeaders.CorrelationId, context.CorrelationId.Value.ToString()); } if (context.InitiatorId.HasValue) { request.Headers.Add(Clients.HttpHeaders.InitiatorId, context.InitiatorId.Value.ToString()); } if (context.ConversationId.HasValue) { request.Headers.Add(Clients.HttpHeaders.ConversationId, context.ConversationId.Value.ToString()); } if (context.RequestId.HasValue) { request.Headers.Add(Clients.HttpHeaders.RequestId, context.RequestId.Value.ToString()); } //TODO: TTL? request.Content = payload; await _observers.PreSend(context).ConfigureAwait(false); using (var response = await clientContext.SendAsync(request, cancellationToken).ConfigureAwait(false)) { response.EnsureSuccessStatusCode(); var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); if (responseStream.Length > 0) { var receiveContext = new HttpClientReceiveContext(response, responseStream, false, _topology); 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 _clientContextSupervisor.Send(clientPipe, cancellationToken).ConfigureAwait(false); }
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); }