Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override Exception Filters executed last instead of first #61

Open
mciancia8 opened this issue Sep 2, 2021 · 3 comments
Open

Override Exception Filters executed last instead of first #61

mciancia8 opened this issue Sep 2, 2021 · 3 comments

Comments

@mciancia8
Copy link

mciancia8 commented Sep 2, 2021

Describe the Bug

When using exception filters, the ones registered with override are executed last not first, which is the opposite compared to action filters. The documentation sounds like it should be the same order no matter what type of interface you implement so it would seem like a bug.

Steps to Reproduce

[RoutePrefix("api/healthcheck")]
public class HealthCheckController : ApiController
    {
        [HttpGet]
        [Route]
        public IEnumerable<string> Get()
        {
            throw new InvalidDataException("TEST");
        }
    }

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();

            var builder = new ContainerBuilder();

            builder.RegisterType<HealthCheckController>()
                .InstancePerRequest();

            builder.RegisterType<E1>()
                .AsWebApiExceptionFilterOverrideFor<HealthCheckController>()
                .SingleInstance();

            builder.RegisterType<E2>()
                .AsWebApiExceptionFilterOverrideForAllControllers()
                .SingleInstance();

            builder.RegisterType<E3>()
                .AsWebApiExceptionFilterFor<HealthCheckController>()
                .SingleInstance();

            builder.RegisterType<E4>()
                .AsWebApiExceptionFilterForAllControllers()
                .SingleInstance();

            builder.RegisterWebApiFilterProvider(config);

            builder.RegisterWebApiModelBinderProvider();

            var container = builder.Build();
            config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        }
    }

    public class E1 : IAutofacExceptionFilter
    {
        public Task OnExceptionAsync(
            HttpActionExecutedContext actionExecutedContext,
            CancellationToken cancellationToken)
        {
            Trace.WriteLine("E1-exception");
            return Task.CompletedTask;
        }
    }

    public class E2 : IAutofacExceptionFilter
    {
        public Task OnExceptionAsync(
            HttpActionExecutedContext actionExecutedContext,
            CancellationToken cancellationToken)
        {
            Trace.WriteLine("E2-exception");
            return Task.CompletedTask;
        }
    }

    public class E3 : IAutofacExceptionFilter
    {
        public Task OnExceptionAsync(
            HttpActionExecutedContext actionExecutedContext,
            CancellationToken cancellationToken)
        {
            Trace.WriteLine("E3-exception");
            return Task.CompletedTask;
        }
    }

    public class E4 : IAutofacExceptionFilter
    {
        public Task OnExceptionAsync(
            HttpActionExecutedContext actionExecutedContext,
            CancellationToken cancellationToken)
        {
            Trace.WriteLine("E4-exception");
            return Task.CompletedTask;
        }
    }

Expected Behavior

When I do something very similar above, but with IAutofacContinuationActionFilter instead of IAutofacExceptionFilter (and changing registration to use AsWebApiActionFilter... I get the following printed in order

E1 - continue
E2 - continue
E3 - continue
E4 - continue

But when I run the code above I get this printed in order:

E3-exception
E4-exception
E1-exception
E2-exception

which means the override ones are executed last, not first.

Dependency Versions

Autofac.WebApi2 6.0.1
Autofac 6.2.0

@tillig
Copy link
Member

tillig commented Sep 2, 2021

Interesting. Sounds like something to address if it's inconsistent with the action filter ordering.

I will say it's odd that you're registering overrides before registering the things to override. Registration order has always been important in DI. If you register them in the order of "not overrides" and then "overrides" what happens?

And, just to verify, if you do this same thing with action filters it provides the expected order?

@mciancia8
Copy link
Author

The order does not appear to change what happens for the exception filters or action filters

@tillig
Copy link
Member

tillig commented Sep 2, 2021

Huh. Well, I guess that's something to fix. Might not be too hard if you want to throw a PR at us.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants