diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs b/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs
index e0f7ec50ad..f1a6264683 100644
--- a/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs
+++ b/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs
@@ -1,6 +1,6 @@
// This software is part of the Autofac IoC container
// Copyright © 2015 Autofac Contributors
-// http://autofac.org
+// https://autofac.org
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@@ -24,10 +24,13 @@
// OTHER DEALINGS IN THE SOFTWARE.
using System;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Autofac.Builder;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Autofac;
+using Volo.Abp;
using Volo.Abp.Modularity;
namespace Autofac.Extensions.DependencyInjection
@@ -46,16 +49,62 @@ namespace Autofac.Extensions.DependencyInjection
/// The into which the registrations should be made.
///
///
- /// The set of service descriptors to register in the container.
+ /// A container builder that can be used to create an .
///
public static void Populate(
- this ContainerBuilder builder,
- IServiceCollection services)
+ this ContainerBuilder builder,
+ IServiceCollection services)
{
- builder.RegisterType().As();
- builder.RegisterType().As();
+ Populate(builder, services, null);
+ }
+
+ ///
+ /// Populates the Autofac container builder with the set of registered service descriptors
+ /// and makes and
+ /// available in the container. Using this overload is incompatible with the ASP.NET Core
+ /// support for .
+ ///
+ ///
+ /// The into which the registrations should be made.
+ ///
+ ///
+ /// A container builder that can be used to create an .
+ ///
+ ///
+ /// If provided and not then all registrations with lifetime are registered
+ /// using
+ /// with provided
+ /// instead of using .
+ ///
+ ///
+ ///
+ /// Specifying a addresses a specific case where you have
+ /// an application that uses Autofac but where you need to isolate a set of services in a child scope. For example,
+ /// if you have a large application that self-hosts ASP.NET Core items, you may want to isolate the ASP.NET
+ /// Core registrations in a child lifetime scope so they don't show up for the rest of the application.
+ /// This overload allows that. Note it is the developer's responsibility to execute this and create an
+ /// using the child lifetime scope.
+ ///
+ ///
+ public static void Populate(
+ this ContainerBuilder builder,
+ IServiceCollection services,
+ object lifetimeScopeTagForSingletons)
+ {
+ if (services == null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
- Register(builder, services);
+ builder.RegisterType().As().ExternallyOwned();
+ var autofacServiceScopeFactory = typeof(AutofacServiceProvider).Assembly.GetType("Autofac.Extensions.DependencyInjection.AutofacServiceScopeFactory");
+ if (autofacServiceScopeFactory == null)
+ {
+ throw new AbpException("Unable get type of Autofac.Extensions.DependencyInjection.AutofacServiceScopeFactory!");
+ }
+ builder.RegisterType(autofacServiceScopeFactory).As();
+
+ Register(builder, services, lifetimeScopeTagForSingletons);
}
///
@@ -65,18 +114,33 @@ namespace Autofac.Extensions.DependencyInjection
/// The object registration style.
/// The registration being built.
/// The lifecycle specified on the service registration.
+ ///
+ /// If not then all registrations with lifetime are registered
+ /// using
+ /// with provided
+ /// instead of using .
+ ///
///
/// The , configured with the proper lifetime scope,
/// and available for additional configuration.
///
private static IRegistrationBuilder