private async Task <bool> Handle(HttpContext context)
        {
            if (!string.Equals(context.Request.Method, "POST", StringComparison.InvariantCultureIgnoreCase) ||
                !contentTypes.Contains(context.Request.ContentType))
            {
                return(false);
            }

            string requestPath = context.Request.Path.ToString();

            var controllerKv = controllers.FirstOrDefault(c => requestPath.Contains(c.Key));
            var controller   = controllerKv.Value;

            if (controller == null)
            {
                return(false);
            }

            string methodName = requestPath.Replace($"/{controllerKv.Key}/", "");
            var    method     = controller.GetType().GetMethod(methodName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);

            if (method == null)
            {
                return(false);
            }

            var    requestBodyType = method.GetParameters()[0].ParameterType;
            string content;

            using (var reader = new StreamReader(context.Request.Body))
                content = await reader.ReadToEndAsync();
            IEnvelope requestBody;

            try
            {
                var envelopeT = typeof(Envelope <>).MakeGenericType(requestBodyType);
                requestBody = (IEnvelope)serializer.DeserializeObject(envelopeT, content);
            } catch (Exception ex)
            {
                context.Response.StatusCode = StatusCodes.Status400BadRequest;
                await context.Response.WriteAsync("");

                return(true);
            }

            var resp        = method.Invoke(controller, new[] { requestBody.Body[0] });
            var envelopeRet = typeof(Envelope <>).MakeGenericType(method.ReturnType);
            var ret         = Activator.CreateInstance(envelopeRet, resp);

            context.Response.StatusCode  = StatusCodes.Status200OK;
            context.Response.ContentType = context.Request.ContentType;
            await context.Response.WriteAsync(serializer.SerializeObject(ret));

            return(true);
        }
Exemple #2
0
        public async Task <TRet> SendAsync <TRet, TBody>(string method, Envelope <TBody> message) where TRet : class where TBody : class
        {
            var url    = BaseUrl == null ? new Uri(method) : new Uri(BaseUrl, method);
            var client = httpClientFactory.CreateClient();

            string content;

            try
            {
                content = xmlSerializer.SerializeObject(message);
            } catch (Exception ex)
            {
                throw new InvalidOperationException("Error during serialization", ex);
            }

            using (var request = new HttpRequestMessage(HttpMethod.Post, new Uri(BaseUrl, method))
            {
                Content = new StringContent(content, Encoding.UTF8, SoapContentType.ToEnumString())
            })
            {
                foreach (var header in HttpHeaders)
                {
                    request.Headers.Add(header.Key, header.Value);
                }

                using (var response = await client.SendAsync(request))
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        throw new SoapCallException("Soap call failed", response.StatusCode, await response.Content.ReadAsStringAsync());
                    }

                    try
                    {
                        var resp = xmlSerializer.DeserializeObject <Envelope <TRet> >(await response.Content.ReadAsStringAsync());
                        return(resp.Body[0]);
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidOperationException("Error during derialization", ex);
                    }
                }
            }
        }