/// <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. This
        /// method passes custom parameters to the constructor of <typeparamref name="THandler"/>.
        /// </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>
        /// <param name="parameters">The custom parameters to pass into the handler.</param>
        public static ExceptionMappingBuilder AddParameterizedScheme <TOptions, TConventionService, THandler>(
            this ExceptionMappingBuilder builder,
            string name,
            Action <TOptions> configure,
            params object[] parameters)
            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));
            }

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

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

            return(builder);
        }
 /// <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 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);
        }
 /// <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. This
 /// method passes custom parameters to the constructor of <typeparamref name="THandler"/>.
 /// </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="parameters">The custom parameters to pass into the handler.</param>
 public static ExceptionMappingBuilder AddParameterizedScheme <TOptions, TConventionService, THandler>(
     this ExceptionMappingBuilder builder,
     string name,
     params object[] parameters)
     where TOptions : ExceptionMappingSchemeOptions, new()
     where TConventionService : class
     where THandler : ExceptionHandler <TOptions, TConventionService>
 => builder.AddParameterizedScheme <TOptions, TConventionService, THandler>(name, _ => { }, parameters);
Exemple #5
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);
        }
        /// <summary>
        /// Updates lifetime of <see cref="IExceptionMapper"/> service.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="lifetime">The new lifetime of <see cref="IExceptionMapper"/> service.</param>
        public static ExceptionMappingBuilder SetMapperLifetime(
            this ExceptionMappingBuilder builder,
            ServiceLifetime lifetime)
        {
            var serviceDescriptor = builder.Services.FirstOrDefault(
                sd => sd.ServiceType == typeof(IExceptionMapper));

            if (serviceDescriptor == null)
            {
                throw new InvalidOperationException("No registered 'IExceptionMapper' found.");
            }

            if (serviceDescriptor.Lifetime == lifetime ||
                serviceDescriptor.ImplementationInstance != null)
            {
                return(builder);
            }

            ServiceDescriptor?newServiceDescriptor = null;

            if (serviceDescriptor.ImplementationType != null)
            {
                newServiceDescriptor = new ServiceDescriptor(typeof(IExceptionMapper),
                                                             serviceDescriptor.ImplementationType, lifetime);
            }
            else if (serviceDescriptor.ImplementationFactory != null)
            {
                newServiceDescriptor = new ServiceDescriptor(typeof(IExceptionMapper),
                                                             serviceDescriptor.ImplementationFactory, lifetime);
            }

            if (newServiceDescriptor != null)
            {
                builder.Services.Replace(newServiceDescriptor);
            }

            return(builder);
        }
Exemple #7
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="configure">The action to configure the endpoint exception mapping scheme.</param>
 public static ExceptionMappingBuilder AddEndpointResponse(
     this ExceptionMappingBuilder builder,
     Action <EndpointExceptionHandlerOptions> configure)
 => builder.AddEndpointResponse(EndpointExceptionMappingDefaults.EndpointScheme, configure);
Exemple #8
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>
 public static ExceptionMappingBuilder AddEndpointResponse(
     this ExceptionMappingBuilder builder,
     string name)
 => builder.AddEndpointResponse(name, _ => { });
Exemple #9
0
 /// <summary>
 /// Adds an exception mapping scheme which maps exception to HTTP response by convention.
 /// </summary>
 /// <param name="builder">The exception mapping builder.</param>
 public static ExceptionMappingBuilder AddEndpointResponse(this ExceptionMappingBuilder builder)
 => builder.AddEndpointResponse(EndpointExceptionMappingDefaults.EndpointScheme, _ => { });