-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolved #486: AutoFac integration document.
- Loading branch information
Showing
7 changed files
with
193 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,84 @@ | ||
## Autofac Integration | ||
# Autofac Integration | ||
|
||
Autofac is one of the most used dependency injection frameworks for .Net. It provides some advanced features compared to .Net Core standard DI library, like dynamic proxying and property injection. | ||
|
||
## Install Autofac Integration | ||
|
||
> All startup templates and samples are Autofac integrated. So, most of the time you don't need to manually install this package. | ||
Install [Volo.Abp.Autofac](https://www.nuget.org/packages/Volo.Abp.Autofac) nuget package to your project (for a multi-projects application, it's suggested to add to the executable/web project.) | ||
|
||
```` | ||
Install-Package Volo.Abp.Autofac | ||
```` | ||
|
||
Then add `AbpAutofacModule` dependency to your module: | ||
|
||
```csharp | ||
using Volo.Abp.Modularity; | ||
using Volo.Abp.Autofac; | ||
|
||
namespace MyCompany.MyProject | ||
{ | ||
[DependsOn(typeof(AbpAutofacModule))] | ||
public class MyModule : AbpModule | ||
{ | ||
//... | ||
} | ||
} | ||
``` | ||
|
||
Finally, configure `AbpApplicationCreationOptions` to replace default dependency injection services by Autofac. It depends on the application type. | ||
|
||
### ASP.NET Core Application | ||
|
||
Call `UseAutofac()` in the **Startup.cs** file as shown below: | ||
|
||
````csharp | ||
public class Startup | ||
{ | ||
public IServiceProvider ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddApplication<MyWebModule>(options => | ||
{ | ||
//Integrate Autofac! | ||
options.UseAutofac(); | ||
}); | ||
|
||
return services.BuildServiceProviderFromFactory(); | ||
} | ||
|
||
public void Configure(IApplicationBuilder app) | ||
{ | ||
app.InitializeApplication(); | ||
} | ||
} | ||
```` | ||
|
||
### Console Application | ||
|
||
Call `UseAutofac()` method in the `AbpApplicationFactory.Create` options as shown below: | ||
|
||
````csharp | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Volo.Abp; | ||
|
||
namespace AbpConsoleDemo | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
using (var application = AbpApplicationFactory.Create<AppModule>(options => | ||
{ | ||
options.UseAutofac(); //Autofac integration | ||
})) | ||
{ | ||
//... | ||
} | ||
} | ||
} | ||
} | ||
```` | ||
|
||
TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.