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

RegisterWebApiModelBinders has no effect in Web API 2 #8

Closed
tillig opened this issue Sep 4, 2015 · 3 comments
Closed

RegisterWebApiModelBinders has no effect in Web API 2 #8

tillig opened this issue Sep 4, 2015 · 3 comments

Comments

@tillig
Copy link
Member

tillig commented Sep 4, 2015

From @alexmg on January 22, 2014 14:32

From tommyj...@gmail.com on January 20, 2014 00:15:41

Registering Web API 2 model binders using the extension method RegisterWebApiModelBinders has no effect. It's missing the required metadata needed by AutofacWebApiModelBinderProvider to resolve the model binder.

Instead of using RegisterWebApiModelBinders, one can manually register the model binders, using:
builder.RegisterType<RequestContextModelBinder>().AsModelBinderForTypes(typeof(RequestContext));

Not sure if it is possible to make RegisterWebApiModelBinders work, otherwise it should probably be deprecated for Web API 2.

Original issue: http://code.google.com/p/autofac/issues/detail?id=484

Copied from original issue: autofac/Autofac#484

@tillig
Copy link
Member Author

tillig commented Sep 4, 2015

From previous discussions on this issue, I'm gathering that a simple RegisterWebApiModelBinders one-call-does-it-all sort of method isn't really possible in Web API 2. We can augment existing support to allow registration of the types into the container in an easier fashion, but associating binder to type-being-bound is missing and isn't something we can reliably scan for.

I'd like to vote that we:

  • Mark the RegisterWebApiModelBinders method with an [Obsolete] attribute that includes a message explaining the challenges and encourages people to register individual binders for types.
  • Close the issue as 'wontfix' since there's not much we can really do.
  • In some TBD future release, remove the RegisterWebApiModelBinders method.

@tillig
Copy link
Member Author

tillig commented Sep 4, 2015

From @alexandrnikitin on June 16, 2014 18:43

From previous discussions on this issue, I'm gathering that a simple RegisterWebApiModelBinders one-call-does-it-all sort of method isn't really possible in Web API 2. We can augment existing support to allow registration of the types into the container in an easier fashion, but associating binder to type-being-bound is missing and isn't something we can reliably scan for.

Agreed, we should treat ModelBinders and ModelBinderProviders separately. Where RegisterWebApiModelBinders() register binders within container (they are available for MVC, eg ModelBinderAttribute) and AsModelBinderForTypes() registers binders for AutofacModelBinderProvider. AutofacModelBinderProvider isn't the only ModelBinderProvider there, e.g. ModelBinderAttribute also works as a provider.

I would leave RegisterWebApiModelBinders() as is, it does its job - scans assemblies for ModelBinders and registers them, like RegisterControllers() does.

@tillig
Copy link
Member Author

tillig commented Aug 3, 2016

In v4.0 the RegisterWebApiModelBinders method is marked obsolete and folks are encouraged to use the builder.RegisterWebApiModelBinderProvider() in combination with builder.RegisterType<T>().AsModelBinderForTypes(...) extension. Simply registering the model binders into the container isn't valuable because without the Autofac binder provider and the metadata, binders don't actually pass through DI.

@tillig tillig closed this as completed in 23efe3c Aug 3, 2016
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

1 participant