From 626ae01a475d3b77b989e9db8fd2e3f047db8da9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 26 Apr 2022 16:40:21 +0300 Subject: [PATCH] Documented: "Using the Autofac Registration API" --- docs/en/Autofac-Integration.md | 37 +++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/docs/en/Autofac-Integration.md b/docs/en/Autofac-Integration.md index 1313d2c482..fcd6389681 100644 --- a/docs/en/Autofac-Integration.md +++ b/docs/en/Autofac-Integration.md @@ -1,12 +1,12 @@ # 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. +[Autofac](https://autofac.org/) is one of the most used dependency injection frameworks for .NET. It provides advanced features compared to .Net Core's 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. +> All the [startup templates](Startup-Templates/Index.md) 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 [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 @@ -52,6 +52,23 @@ public class Program } ```` +If you are using the static `WebApplication` class, you can call the `UseAutofac()` extension method as shown below: + +````csharp +public class Program +{ + public async static Task Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + builder.Host.UseAutofac(); // Integrate Autofac! + await builder.AddApplicationAsync(); + var app = builder.Build(); + await app.InitializeApplicationAsync(); + await app.RunAsync(); + } +} +```` + ### Console Application Call `UseAutofac()` method in the `AbpApplicationFactory.Create` options as shown below: @@ -79,3 +96,17 @@ namespace AbpConsoleDemo } ```` +## Using the Autofac Registration API + +If you want to use Autofac's advanced [registration API](https://autofac.readthedocs.io/en/latest/register/registration.html), you need to access the `ContainerBuilder` object. [Volo.Abp.Autofac](https://www.nuget.org/packages/Volo.Abp.Autofac) nuget package defines the `IServiceCollection.GetContainerBuilder()` extension method to obtain the `ContainerBuilder` object. + +**Example: Get the `ContainerBuilder` object in the `ConfigureServices` method of your [module class](Module-Development-Basics.md)** + +````csharp +public override void ConfigureServices(ServiceConfigurationContext context) +{ + var containerBuilder = context.Services.GetContainerBuilder(); + containerBuilder.RegisterType(); // Using Autofac's registration API +} +```` +