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

Registering one filter type for multiple controllers doesn't work in one statement #17

Closed
petermorlion opened this issue Aug 30, 2016 · 2 comments · Fixed by #43
Closed

Comments

@petermorlion
Copy link

When registering filters, you can't register one type for multiple controllers like this:

builder.RegisterType<CustomActionFilter>()
            .AsWebApiActionFilterFor<ProductsController>()
            .AsWebApiActionFilterFor<OrdersController>()
            .InstancePerRequest();

Rather, you'll have to do it per controller separately:

builder.RegisterType<CustomActionFilter>()
            .AsWebApiActionFilterFor<ProductsController>()
            .InstancePerRequest();

builder.RegisterType<CustomActionFilter>()
            .AsWebApiActionFilterFor<OrdersController>()
            .InstancePerRequest();

This can become cumbersome, because often, you'd want to use certain filters for lots, if not all, controllers.

You can't use the first option, because you get an exception stating that an item with the same key has already been added to the dictionary. The key is AutofacWebApiFilterProvider.ActionFilterMetadataKey and has a value of "AutofacWebApiActionFilter".

The problem is that in the AsFilterFor method, metadata is constructed that will be unique in the registration (because of a different controller type), but won't be added with a unique metadatakey (always "AutofacWebApiActionFilter").

I'd be happy to fix this and submit a PR, if you're willing to guide me where and how (and if) you'd like it fixed. Can I safely change the metadatakey to be something unique?

@tillig
Copy link
Member

tillig commented Oct 25, 2016

Sorry for the delay in getting back to you on this. It's a good find and we'd love to have a PR for it with associated unit tests.

As you've noticed, the way we figure out the way a service is a filter is by looking at the specific metadata key. If you're able to fix things up so all the filters (action filters, authentication filters, etc.) all work correctly then we'd love a PR for it. I anticipate changing the metadata key may be painful, but I haven't dived in too deep on this.

Anyway, yeah, if you can figure it out, we'd love to see a fix. Thanks!

@petermorlion
Copy link
Author

I'll have a look into this, but give me some time, because I currently worked around it with a method like this:

private void RegisterControllerWithFilters<TController>(ContainerBuilder builder) where TController : IHttpController
{
    builder.RegisterType<TController>().InstancePerRequest();

    builder.RegisterType<MyExceptionFilter>()
        .AsWebApiExceptionFilterFor<TController>()
        .InstancePerRequest();

    builder.RegisterType<MyAuthorizationFilter>()
        .AsWebApiAuthorizationFilterFor<TController>()
        .InstancePerRequest();
}

And then calling it like this:

RegisterControllerWithFilters<MyController>(builder);

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

Successfully merging a pull request may close this issue.

2 participants