/// <summary>
 /// Adds an exception mapping scheme which uses strongly-typed settings of <typeparamref name="TOptions"/>
 /// and a set of mapping conventions of <typeparamref name="TConventionService"/> to handle exception.
 /// </summary>
 /// <typeparam name="TOptions">The type of the settings consumed by the scheme.</typeparam>
 /// <typeparam name="TConventionService">The mapping convention service type.</typeparam>
 /// <typeparam name="THandler">The type of the scheme implementer.</typeparam>
 /// <param name="builder">The builder.</param>
 /// <param name="name">The name of the scheme to be added.</param>
 public static ExceptionMappingBuilder AddScheme <TOptions, TConventionService, THandler>(
     this ExceptionMappingBuilder builder,
     string name)
     where TOptions : ExceptionMappingSchemeOptions, new()
     where TConventionService : class
     where THandler : ExceptionHandler <TOptions, TConventionService>
 => builder.AddScheme <TOptions, TConventionService, THandler>(name, _ => { });
        /// <summary>
        /// Adds an exception mapping scheme which uses strongly-typed settings of <typeparamref name="TOptions"/>
        /// and a set of mapping conventions of <typeparamref name="TConventionService"/> to handle exception.
        /// </summary>
        /// <typeparam name="TOptions">The type of the settings consumed by the scheme.</typeparam>
        /// <typeparam name="TConventionService">The mapping convention service type.</typeparam>
        /// <typeparam name="THandler">The type of the scheme implementer.</typeparam>
        /// <param name="builder">The builder.</param>
        /// <param name="name">The name of the scheme to be added.</param>
        /// <param name="configure">The action to configure scheme options.</param>
        public static ExceptionMappingBuilder AddScheme <TOptions, TConventionService, THandler>(
            this ExceptionMappingBuilder builder,
            string name,
            Action <TOptions> configure)
            where TOptions : ExceptionMappingSchemeOptions, new()
            where TConventionService : class
            where THandler : ExceptionHandler <TOptions, TConventionService>
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            builder.Services.Configure <TOptions>(name, opt => opt.SchemeName = name);
            builder.AddScheme <THandler>(name);
            builder.Services.Configure(name, configure);

            return(builder);
        }
        /// <summary>
        /// Adds an exception mapping scheme of <typeparamref name="THandler"/> and passes custom parameters to the
        /// constructor of <typeparamref name="THandler"/>.
        /// </summary>
        /// <typeparam name="THandler">The type of the scheme implementer.</typeparam>
        /// <param name="builder">The builder.</param>
        /// <param name="name">The name of the scheme to be added.</param>
        /// <param name="parameters">The custom parameters to pass into the handler.</param>
        public static ExceptionMappingBuilder AddParameterizedScheme <THandler>(
            this ExceptionMappingBuilder builder,
            string name,
            params object[] parameters)
            where THandler : IExceptionHandler
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            builder.AddScheme(new ExceptionMappingSchemeRegistration(
                                  name, typeof(THandler),
                                  serviceProvider => ActivatorUtilities.CreateInstance <THandler>(serviceProvider, parameters)));

            return(builder);
        }
示例#4
0
        /// <summary>
        /// Adds an exception mapping scheme which maps exception to HTTP response by convention.
        /// </summary>
        /// <param name="builder">The exception mapping builder.</param>
        /// <param name="name">The name of the scheme.</param>
        /// <param name="configure">The action to configure the endpoint exception mapping scheme.</param>
        public static ExceptionMappingBuilder AddEndpointResponse(
            this ExceptionMappingBuilder builder,
            string name,
            Action <EndpointExceptionHandlerOptions> configure)
        {
            builder.Services.TryAddSingleton <IHttpContextFinder, HttpContextFinder>();
            builder.Services.TryAddSingleton <IRoutePatternFormatter, RoutePatternFormatter>();
            builder.Services.TryAddSingleton <IProblemResponseWriterFactory, ProblemResponseWriterFactory>();

            builder.AddScheme <EndpointExceptionHandlerOptions, IEndpointExceptionMappingConvention,
                               EndpointExceptionHandler>(name, configure);

            return(builder);
        }