public RequestHandlerModule(IMediator mediator, RequestTypeProvider requestTypeProvider)
            : base("/RequestHandler/request")
        {
            Post["/{requestType}", true] = async (parameters, ct) =>
            {
                Console.WriteLine("Call to AsyncRequestHandlerModule");
                var requestTypeString = (string)parameters.requestType;

                var requestType = requestTypeProvider.FindByFullName(requestTypeString);

                if (requestType == null)
                {
                    return (int)HttpStatusCode.NotAcceptable;
                }

                dynamic deserializedRequest = this.Bind(requestType);

                var isAsyncRequest = requestType.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IAsyncRequest<>));

                if (requestType.BaseType != typeof(object) && requestType.BaseType == typeof(FileRequest))
                {
                    var response = new Response { StatusCode = HttpStatusCode.OK, ContentType = "application/octet-stream", Contents = async s => {
                        ((FileRequest)deserializedRequest).SetStream(s);
                        if (isAsyncRequest)
                        {
                            await mediator.SendAsync(deserializedRequest);
                        }
                        else
                        {
                            mediator.Send(deserializedRequest);
                        }
                    } };

                    return response;
                }

                try
                {
                    if (isAsyncRequest)
                    {
                        return await mediator.SendAsync(deserializedRequest);
                    }

                    return mediator.Send(deserializedRequest);
                }
                catch (Exception exc)
                {
                    //todo:log, etc.
                    var errorResponse = Response.AsText(exc.Message);
                    errorResponse.StatusCode = HttpStatusCode.InternalServerError;

                    return errorResponse;
                }
            };
        }
 public RequestDelegatingHandler(IMediator mediator, RequestTypeProvider requestTypeProvider)
 {
     _mediator = mediator;
     _requestTypeProvider = requestTypeProvider;
 }
Example #3
0
 public Startup(IMediator mediator, RequestTypeProvider requestTypeProvider)
 {
     _mediator = mediator;
     _requestTypeProvider = requestTypeProvider;
 }