This article is a mirror article of machine translation, please click here to jump to the original article.

View: 20328|Reply: 0

[WebAPI] In MVC5 and webAPI, Autofac dependency injection is used

[Copy link]
Posted on 11/29/2017 2:35:33 PM | | | |
Many books mention concepts such as dependency injection and control inversion, all of which are aimed at achieving loosely coupled layers, components, and class purposes.

It is common to use the Repository class to separate the direct connection between the Controller and the Model. In order to disconnect the close connection between the Repository class and the Controller, it is usually not directly defined and instantiated by the Repository class, but injected into the specified Repository through the Controller's construction method.



Popular IoC containers are: Ninject, Autofac, Unity.

Here is a brief explanation of how to use Autofac injection MVC5 and Webapi2.

1. Use nupkg to reference Autofac, Autofac.Mvc5 and Autofac.Webapi2

PM> install-package autofac -version 3.5.0

PM> install-package autofac.mvc5

PM> install-package autofac.webapi2 (Note: If you are using webapi2 in your project, it must be webapi2 and not webapi here, otherwise "Rewrite member Autofac.Integration.WebApi.AutofacWebApiDependencyResolver.BeginScope()" will appear at runtime) violated the inheritance security rules. The security accessibility of the overridden method must match the secure accessibility of the rewritten method. Wrong. )



2. Register the component.

3. Create a Container for later use.

var container=builder. Build();
4. Create a lifetime scope from the Container.

5. Use this Lifetime Scope to parse instances of components.

config. DependencyResolver = new AutofacWebApiDependencyResolver(container); To register an API container, you need to use the HttpConfiguration object
DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); Register the MVC container
6. In the Register method of the WebApiConfig class, call the above step code and pass in the HttpConfiguration object.



The advantage of autofac is that it can be decoupled at one time without the need for configuration; autofac better implements the concept of "convention is greater than configuration" in MVC.





Previous:asp.net MVC adds a WebAPI controller
Next:How to recharge.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com