示例#1
0
        public async Task Process(ESBContext context,
                                  Inbound.ResponseHandle responseHandle)
        {
            InvokeInterceptor(context);

            await responseHandle.Write(context.ResponseBody,
                                       context.StatusCode, context.ResponseHeaders);
        }
        public async Task Dispatch(ESBContext context,
                                   Inbound.ResponseHandle responseHandle)
        {
            try
            {
                //using (HttpClientHandler handler = new HttpClientHandler())
                //{
                //handler.ServerCertificateCustomValidationCallback = delegate {

                //	return true;
                //};

                //using (HttpClient client = new HttpClient(handler))
                using (HttpClient client = new HttpClient())
                {
                    HttpContent content = null;
                    if (context.Body != null && context.Body.Length > 0)
                    {
                        content =
                            new ByteArrayContent(context.Body);

                        content.Headers.ContentType =
                            new MediaTypeHeaderValue(GetConteType(context));
                    }

                    HttpRequestMessage msg = new HttpRequestMessage()
                    {
                        Content = content
                    };

                    msg.Headers.Add("User-Agent", ESBContants.HEADER_USER_AGENTE);
                    msg.Headers.Add("X-Forwarded-For", context.IP);
                    msg.Headers.Add("Accept", GetAccept(context));
                    msg.Method     = new HttpMethod(context.Method);
                    msg.RequestUri = CreateUri(context);
                    var resp = await client.SendAsync(msg);

                    Dictionary <string, string> headersMap = new Dictionary <string, string>();

                    var headers = resp.Headers.GetEnumerator();
                    while (headers.MoveNext())
                    {
                        headersMap.Add(headers.Current.Key,
                                       string.Join(",", headers.Current.Value));
                    }

                    string contentType = resp.Content != null &&
                                         resp.Content.Headers != null
                                             ? resp.Content.Headers.ContentType.ToString() : null;

                    byte[] bytes;

                    using (var strem = await resp.Content.ReadAsStreamAsync())
                    {
                        bytes = new byte[strem.Length];

                        await strem.ReadAsync(bytes, 0, bytes.Length);
                    }

                    if (!string.IsNullOrEmpty(contentType))
                    {
                        headersMap.Add("Content-Type", contentType);
                    }

                    context.ResponseBody    = bytes;
                    context.StatusCode      = (int)resp.StatusCode;
                    context.ResponseHeaders = headersMap;

                    await ESBFactory
                    .Instance
                    .OutboundProcess
                    .Process(context, responseHandle);
                }
                //}
            }
            catch (Exception e)
            {
                var json = ESBUtil.ParseJson(new
                {
                    Message = "Não foi possível enviar a solicitação para a origem",
                    Url     = context.Url,
                    Detail  = e.Message,
                    Trace   = e.StackTrace
                });

                await responseHandle.Write(ESBUtil.CreateBytes(json), 501);
            }
        }