mirror of https://github.com/abpframework/abp.git
11 changed files with 113 additions and 20 deletions
@ -0,0 +1,7 @@ |
|||||
|
namespace AbpDesk.Web.Mvc.Temp |
||||
|
{ |
||||
|
/* Will be removed later */ |
||||
|
public class MyClassToTestAutofacCustomRegistration |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
using Autofac; |
||||
|
using JetBrains.Annotations; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp |
||||
|
{ |
||||
|
public static class AbpApplicationCreationOptionsAutofacExtensions |
||||
|
{ |
||||
|
public static void UseAutofac(this AbpApplicationCreationOptions options) |
||||
|
{ |
||||
|
var builder = new ContainerBuilder(); |
||||
|
options.Services.AddObjectAccessor(builder); |
||||
|
options.Services.AddSingleton((IServiceProviderFactory<ContainerBuilder>) new AbpAutofacServiceProviderFactory(builder)); |
||||
|
} |
||||
|
|
||||
|
[NotNull] |
||||
|
public static ContainerBuilder GetContainerBuilder([NotNull] this IServiceCollection services) |
||||
|
{ |
||||
|
Check.NotNull(services, nameof(services)); |
||||
|
|
||||
|
var builder = services.GetObjectOrNull<ContainerBuilder>(); |
||||
|
if (builder == null) |
||||
|
{ |
||||
|
throw new AbpException($"Could not find ContainerBuilder. Be sure that you have called {nameof(UseAutofac)} method before!"); |
||||
|
} |
||||
|
|
||||
|
return builder; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,39 @@ |
|||||
|
using System; |
||||
|
using Autofac; |
||||
|
using Autofac.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// A factory for creating a <see cref="T:Autofac.ContainerBuilder" /> and an <see cref="T:System.IServiceProvider" />.
|
||||
|
/// </summary>
|
||||
|
public class AbpAutofacServiceProviderFactory : IServiceProviderFactory<ContainerBuilder> |
||||
|
{ |
||||
|
private readonly ContainerBuilder _builder; |
||||
|
|
||||
|
public AbpAutofacServiceProviderFactory(ContainerBuilder builder) |
||||
|
{ |
||||
|
_builder = builder; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Creates a container builder from an <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" />.
|
||||
|
/// </summary>
|
||||
|
/// <param name="services">The collection of services</param>
|
||||
|
/// <returns>A container builder that can be used to create an <see cref="T:System.IServiceProvider" />.</returns>
|
||||
|
public ContainerBuilder CreateBuilder(IServiceCollection services) |
||||
|
{ |
||||
|
AutofacRegistration.Populate(_builder, services); |
||||
|
|
||||
|
return _builder; |
||||
|
} |
||||
|
|
||||
|
public IServiceProvider CreateServiceProvider(ContainerBuilder containerBuilder) |
||||
|
{ |
||||
|
Check.NotNull(containerBuilder, nameof(containerBuilder)); |
||||
|
|
||||
|
return new AutofacServiceProvider(containerBuilder.Build()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue