Esempio n. 1
0
 /// <summary>
 /// Set <see cref="IActionResult"/> to response and pass control to next handler.
 /// </summary>
 /// <typeparam name="TException">
 /// The exception type
 /// </typeparam>
 /// <typeparam name="TResult">
 /// The action result type. Should implement <see cref="IActionResult"/>.
 /// </typeparam>
 /// <param name="builder">
 /// The policy builder
 /// </param>
 /// <param name="result">
 /// The <see cref="IActionResult"/> action result.
 /// </param>
 /// <param name="index" optional="true">
 /// Handler index in the chain. Optional. By default handler added to the end of chain.
 /// </param>
 /// <returns>
 /// Policy builder
 /// </returns>
 public static IResponseHandlers <TException> WithActionResult <TException, TResult>(
     this IResponseHandlers <TException> builder, TResult result, int index = -1)
     where TException : Exception
     where TResult : IActionResult
 {
     return(WithActionResult(builder, (request, exception) => result));
 }
Esempio n. 2
0
        /// <summary>
        /// Set response body, close response builder and pass control to next handler.
        /// </summary>
        /// <typeparam name="TException">
        /// The exception type
        /// </typeparam>
        /// <param name="builder">
        /// The policy builder
        /// </param>
        /// <param name="settings">
        /// Delegate to write to response stream.
        /// </param>
        /// <param name="index" optional="true">
        /// Handler index in the chain. Optional. By default handler added to the end of chain.
        /// </param>
        /// <returns>
        /// Policy builder
        /// </returns>
        public static IResponseHandlers <TException> WithBody <TException>(
            this IResponseHandlers <TException> builder, Func <HttpRequest, Stream, TException, Task> settings, int index = -1)
            where TException : Exception
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            builder.Services.Configure <RawResponseHandlerOptions <TException> >(responceOptions =>
            {
                responceOptions.SetResponse.Add((context, exception) =>
                                                RawResponseExceptionHandler <TException> .SetBody(context, exception, settings));
            });

            return(builder);
        }
        public static IResponseHandlers <TException> WithBodyJson <TException, TObject>(
            this IResponseHandlers <TException> builder, Func <HttpRequest, TException, TObject> valueFactory, JsonSerializerSettings settings = null, int index = -1)
            where TException : Exception
        {
            return(builder.WithBody((request, stream, exception) =>
            {
                if (settings == null)
                {
                    settings = request.HttpContext.RequestServices.GetService <JsonSerializerSettings>();
                }

                if (settings == null)
                {
                    settings = new JsonSerializerSettings();
                }

                JsonSerializer jsonSerializer = JsonSerializer.Create(settings);

                var headers = request.HttpContext.Response.Headers;
                if (!headers.ContainsKey(HeaderNames.ContentType))
                {
                    headers[HeaderNames.ContentType] = "application/json";
                }

                TObject val = valueFactory(request, exception);

                //return Task.CompletedTask;
                using (MemoryStream ms = new MemoryStream())
                {
                    using (StreamWriter sw = new StreamWriter(ms, Encoding.UTF8, 1024, true))
                    {
                        jsonSerializer.Serialize(sw, val);
                    }

                    ms.Seek(0, SeekOrigin.Begin);
                    byte[] array = ms.ToArray();
                    stream.WriteAsync(array, 0, array.Length);

                    return Task.CompletedTask;
                }
            }));
        }
Esempio n. 4
0
        /// <summary>
        /// Modify response headers
        /// </summary>
        /// <typeparam name="TException">
        /// The exception type
        /// </typeparam>
        /// <param name="builder">
        /// The policy builder
        /// </param>
        /// <param name="settings">
        /// Action for response headers modification
        /// </param>
        /// <param name="index" optional="true">
        /// Handler index in the chain. Optional. By default handler added to the end of chain.
        /// </param>
        /// <returns>
        /// Response builder
        /// </returns>
        public static IResponseHandlers <TException> Headers <TException>(
            this IResponseHandlers <TException> builder, Action <IHeaderDictionary, TException> settings, int index = -1)
            where TException : Exception
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            builder.Services.Configure <RawResponseHandlerOptions <TException> >(responceOptions =>
            {
                responceOptions.SetResponse.Add((context, exception) =>
                {
                    settings.Invoke(context.Response.Headers, exception);
                    return(Task.CompletedTask);
                });
            });

            return(builder);
        }
Esempio n. 5
0
        /// <summary>
        /// Set response headers which revents response from being cached.
        /// </summary>
        /// <typeparam name="TException">
        /// The exception type
        /// </typeparam>
        /// <param name="builder">
        /// The policy builder
        /// </param>
        /// <param name="index" optional="true">
        /// Handler index in the chain. Optional. By default handler added to the end of chain.
        /// </param>
        /// <returns>
        /// Policy builder
        /// </returns>
        public static IResponseHandlers <TException> ClearCacheHeaders <TException>(
            this IResponseHandlers <TException> builder, int index = -1)
            where TException : Exception
        {
            builder.Services.Configure <RawResponseHandlerOptions <TException> >(responceOptions =>
            {
                responceOptions.SetResponse.Add((context, exception) =>
                {
                    HttpResponse response = context.Response;

                    response.Headers[HeaderNames.CacheControl] = "no-cache";
                    response.Headers[HeaderNames.Pragma]       = "no-cache";
                    response.Headers[HeaderNames.Expires]      = "-1";
                    response.Headers.Remove(HeaderNames.ETag);

                    return(Task.CompletedTask);
                });
            });

            return(builder);
        }
Esempio n. 6
0
        /// <summary>
        /// Set <see cref="IActionResult"/> to response and pass control to next handler.
        /// </summary>
        /// <typeparam name="TException">
        /// The exception type
        /// </typeparam>
        /// <typeparam name="TResult">
        /// The action result type. Should implement <see cref="IActionResult"/>.
        /// </typeparam>
        /// <param name="builder">
        /// The policy builder
        /// </param>
        /// <param name="resultFactory">
        /// The <see cref="IActionResult"/> factory.
        /// </param>
        /// <param name="index" optional="true">
        /// Handler index in the chain. Optional. By default handler added to the end of chain.
        /// </param>
        /// <returns>
        /// Policy builder
        /// </returns>
        public static IResponseHandlers <TException> WithActionResult <TException, TResult>(
            this IResponseHandlers <TException> builder, Func <HttpRequest, TException, TResult> resultFactory, int index = -1)
            where TException : Exception
            where TResult : IActionResult
        {
            return(builder.WithBody((request, streamWriter, exception) =>
            {
                var context = request.HttpContext;
                var executor = context.RequestServices.GetService <IActionResultExecutor <TResult> >();

                if (executor == null)
                {
                    throw new InvalidOperationException($"No result executor for '{typeof(TResult).FullName}' has been registered.");
                }

                var routeData = context.GetRouteData() ?? EmptyRouteData;

                var actionContext = new ActionContext(context, routeData, EmptyActionDescriptor);

                return executor.ExecuteAsync(actionContext, resultFactory(request, exception));
            }));
        }
Esempio n. 7
0
 /// <summary>
 /// Set <see cref="ObjectResult"/> to response and pass control to next handler.
 /// </summary>
 /// <typeparam name="TException">
 /// The exception type
 /// </typeparam>
 /// <typeparam name="TObject">
 /// The result object type.
 /// </typeparam>
 /// <param name="builder">
 /// The policy builder
 /// </param>
 /// <param name="valueFactory">
 /// The result object factory.
 /// </param>
 /// <param name="index" optional="true">
 /// Handler index in the chain. Optional. By default handler added to the end of chain.
 /// </param>
 /// <returns>
 /// Policy builder
 /// </returns>
 public static IResponseHandlers <TException> WithObjectResult <TException, TObject>(
     this IResponseHandlers <TException> builder, Func <HttpRequest, TException, TObject> valueFactory, int index = -1)
     where TException : Exception
 {
     return(WithActionResult(builder, (request, exception) => new ObjectResult(valueFactory(request, exception)), index));
 }
Esempio n. 8
0
 /// <summary>
 /// Set <see cref="ObjectResult"/> to response and pass control to next handler.
 /// </summary>
 /// <typeparam name="TException">
 /// The exception type
 /// </typeparam>
 /// <typeparam name="TObject">
 /// The result object type.
 /// </typeparam>
 /// <param name="builder">
 /// The policy builder
 /// </param>
 /// <param name="value">
 /// The result object.
 /// </param>
 /// <param name="index" optional="true">
 /// Handler index in the chain. Optional. By default handler added to the end of chain.
 /// </param>
 /// <returns>
 /// Policy builder
 /// </returns>
 public static IResponseHandlers <TException> WithObjectResult <TException, TObject>(
     this IResponseHandlers <TException> builder, TObject value, int index = -1)
     where TException : Exception
 {
     return(WithActionResult(builder, new ObjectResult(value), index));
 }