From aca324d1f124d68b3a16260bb8fc0d8d2fc8992e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 2 May 2017 12:00:27 +0300 Subject: [PATCH] Created basic Autofac integration. --- .../AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs | 4 ++ .../Controllers/HomeController.cs | 5 ++- src/AbpDesk/AbpDesk.Web.Mvc/Startup.cs | 19 +++------ .../MyClassToTestAutofacCustomRegistration.cs | 7 ++++ src/Volo.Abp.Autofac/Volo.Abp.Autofac.csproj | 4 ++ ...icationCreationOptionsAutofacExtensions.cs | 30 ++++++++++++++ .../Abp/AbpAutofacServiceProviderFactory.cs | 39 +++++++++++++++++++ .../ServiceCollectionCommonExtensions.cs | 2 +- ...rviceCollectionObjectAccessorExtensions.cs | 12 ++++++ src/Volo.Abp/Volo/Abp/AbpApplication.cs | 2 +- .../Volo/Abp/AbpApplicationCreationOptions.cs | 9 ++++- 11 files changed, 113 insertions(+), 20 deletions(-) create mode 100644 src/AbpDesk/AbpDesk.Web.Mvc/Temp/MyClassToTestAutofacCustomRegistration.cs create mode 100644 src/Volo.Abp.Autofac/Volo/Abp/AbpApplicationCreationOptionsAutofacExtensions.cs create mode 100644 src/Volo.Abp.Autofac/Volo/Abp/AbpAutofacServiceProviderFactory.cs diff --git a/src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs b/src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs index d2dadb8c46..b0cff4f325 100644 --- a/src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs +++ b/src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs @@ -1,5 +1,7 @@ using AbpDesk.EntityFrameworkCore; using AbpDesk.Web.Mvc.Navigation; +using AbpDesk.Web.Mvc.Temp; +using Autofac; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; @@ -42,6 +44,8 @@ namespace AbpDesk.Web.Mvc services.AddMvc(); services.AddAssemblyOf(); + + services.GetContainerBuilder().RegisterType(); } public override void OnApplicationInitialization(ApplicationInitializationContext context) diff --git a/src/AbpDesk/AbpDesk.Web.Mvc/Controllers/HomeController.cs b/src/AbpDesk/AbpDesk.Web.Mvc/Controllers/HomeController.cs index 1452fd030d..da751428b0 100644 --- a/src/AbpDesk/AbpDesk.Web.Mvc/Controllers/HomeController.cs +++ b/src/AbpDesk/AbpDesk.Web.Mvc/Controllers/HomeController.cs @@ -1,11 +1,12 @@ -using Microsoft.AspNetCore.Mvc; +using AbpDesk.Web.Mvc.Temp; +using Microsoft.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc; namespace AbpDesk.Web.Mvc.Controllers { public class HomeController : AbpController { - public IActionResult Index() + public IActionResult Index(MyClassToTestAutofacCustomRegistration obj) { return View(); } diff --git a/src/AbpDesk/AbpDesk.Web.Mvc/Startup.cs b/src/AbpDesk/AbpDesk.Web.Mvc/Startup.cs index 3635e52c0d..536aa14f96 100644 --- a/src/AbpDesk/AbpDesk.Web.Mvc/Startup.cs +++ b/src/AbpDesk/AbpDesk.Web.Mvc/Startup.cs @@ -1,20 +1,16 @@ -using System; -using System.IO; -using Autofac; -using Autofac.Extensions.DependencyInjection; +using System.IO; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Serilog; +using Volo.Abp; using Volo.Abp.Modularity.PlugIns; namespace AbpDesk.Web.Mvc { public class Startup { - public IContainer ApplicationContainer { get; private set; } - private readonly IHostingEnvironment _env; public Startup(IHostingEnvironment env) @@ -22,10 +18,12 @@ namespace AbpDesk.Web.Mvc _env = env; } - public IServiceProvider ConfigureServices(IServiceCollection services) + public void ConfigureServices(IServiceCollection services) { services.AddApplication(options => { + options.UseAutofac(); + /* @halil: I added Abp.MongoDb package as a dependency to the main application in order to use the blog plugin. * Otherwise, we should add all dependencies (Recursively) into plugin folder * and load in correct order. We should carefully think on that problem in the future. @@ -36,11 +34,6 @@ namespace AbpDesk.Web.Mvc @"../Web_PlugIns/") ); }); - - var builder = new ContainerBuilder(); - builder.Populate(services); - ApplicationContainer = builder.Build(); - return new AutofacServiceProvider(ApplicationContainer); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime) @@ -54,8 +47,6 @@ namespace AbpDesk.Web.Mvc .CreateLogger() ); - appLifetime.ApplicationStopped.Register(() => ApplicationContainer.Dispose()); - app.InitializeApplication(); } } diff --git a/src/AbpDesk/AbpDesk.Web.Mvc/Temp/MyClassToTestAutofacCustomRegistration.cs b/src/AbpDesk/AbpDesk.Web.Mvc/Temp/MyClassToTestAutofacCustomRegistration.cs new file mode 100644 index 0000000000..2f39953a5a --- /dev/null +++ b/src/AbpDesk/AbpDesk.Web.Mvc/Temp/MyClassToTestAutofacCustomRegistration.cs @@ -0,0 +1,7 @@ +namespace AbpDesk.Web.Mvc.Temp +{ + /* Will be removed later */ + public class MyClassToTestAutofacCustomRegistration + { + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Autofac/Volo.Abp.Autofac.csproj b/src/Volo.Abp.Autofac/Volo.Abp.Autofac.csproj index 1463963657..337c7375de 100644 --- a/src/Volo.Abp.Autofac/Volo.Abp.Autofac.csproj +++ b/src/Volo.Abp.Autofac/Volo.Abp.Autofac.csproj @@ -16,4 +16,8 @@ + + + + \ No newline at end of file diff --git a/src/Volo.Abp.Autofac/Volo/Abp/AbpApplicationCreationOptionsAutofacExtensions.cs b/src/Volo.Abp.Autofac/Volo/Abp/AbpApplicationCreationOptionsAutofacExtensions.cs new file mode 100644 index 0000000000..6d66f09ebf --- /dev/null +++ b/src/Volo.Abp.Autofac/Volo/Abp/AbpApplicationCreationOptionsAutofacExtensions.cs @@ -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) new AbpAutofacServiceProviderFactory(builder)); + } + + [NotNull] + public static ContainerBuilder GetContainerBuilder([NotNull] this IServiceCollection services) + { + Check.NotNull(services, nameof(services)); + + var builder = services.GetObjectOrNull(); + if (builder == null) + { + throw new AbpException($"Could not find ContainerBuilder. Be sure that you have called {nameof(UseAutofac)} method before!"); + } + + return builder; + } + } +} diff --git a/src/Volo.Abp.Autofac/Volo/Abp/AbpAutofacServiceProviderFactory.cs b/src/Volo.Abp.Autofac/Volo/Abp/AbpAutofacServiceProviderFactory.cs new file mode 100644 index 0000000000..1e639d0c61 --- /dev/null +++ b/src/Volo.Abp.Autofac/Volo/Abp/AbpAutofacServiceProviderFactory.cs @@ -0,0 +1,39 @@ +using System; +using Autofac; +using Autofac.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; + +namespace Volo.Abp +{ + /// + /// A factory for creating a and an . + /// + public class AbpAutofacServiceProviderFactory : IServiceProviderFactory + { + private readonly ContainerBuilder _builder; + + public AbpAutofacServiceProviderFactory(ContainerBuilder builder) + { + _builder = builder; + } + + /// + /// Creates a container builder from an . + /// + /// The collection of services + /// A container builder that can be used to create an . + 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()); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionCommonExtensions.cs b/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionCommonExtensions.cs index c454964128..7a8f24ec13 100644 --- a/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionCommonExtensions.cs +++ b/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionCommonExtensions.cs @@ -11,7 +11,7 @@ namespace Microsoft.Extensions.DependencyInjection .FirstOrDefault(d => d.ServiceType == typeof(T)) ?.ImplementationInstance; } - + public static T GetSingletonInstance(this IServiceCollection services) { var service = services.GetSingletonInstanceOrNull(); diff --git a/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionObjectAccessorExtensions.cs b/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionObjectAccessorExtensions.cs index 28ad3cb079..882b2ae1be 100644 --- a/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionObjectAccessorExtensions.cs +++ b/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionObjectAccessorExtensions.cs @@ -39,5 +39,17 @@ namespace Microsoft.Extensions.DependencyInjection return accessor; } + + public static T GetObjectOrNull(this IServiceCollection services) + where T : class + { + return services.GetSingletonInstanceOrNull>()?.Value; + } + + public static T GetObject(this IServiceCollection services) + where T : class + { + return services.GetObjectOrNull() ?? throw new Exception($"Could not find an object of {typeof(T).AssemblyQualifiedName} in services. Be sure that you have used AddObjectAccessor before!"); + } } } \ No newline at end of file diff --git a/src/Volo.Abp/Volo/Abp/AbpApplication.cs b/src/Volo.Abp/Volo/Abp/AbpApplication.cs index acd362bd3a..348636b501 100644 --- a/src/Volo.Abp/Volo/Abp/AbpApplication.cs +++ b/src/Volo.Abp/Volo/Abp/AbpApplication.cs @@ -26,7 +26,7 @@ namespace Volo.Abp StartupModuleType = startupModuleType; - var options = new AbpApplicationCreationOptions(); + var options = new AbpApplicationCreationOptions(services); optionsAction?.Invoke(options); services.AddCoreAbpServices(this); diff --git a/src/Volo.Abp/Volo/Abp/AbpApplicationCreationOptions.cs b/src/Volo.Abp/Volo/Abp/AbpApplicationCreationOptions.cs index 869d7eb8a2..296c7a4326 100644 --- a/src/Volo.Abp/Volo/Abp/AbpApplicationCreationOptions.cs +++ b/src/Volo.Abp/Volo/Abp/AbpApplicationCreationOptions.cs @@ -1,4 +1,5 @@ using JetBrains.Annotations; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Modularity.PlugIns; namespace Volo.Abp @@ -6,10 +7,14 @@ namespace Volo.Abp public class AbpApplicationCreationOptions { [NotNull] - public PlugInSourceList PlugInSources { get; private set; } + public IServiceCollection Services { get; } - public AbpApplicationCreationOptions() + [NotNull] + public PlugInSourceList PlugInSources { get; } + + public AbpApplicationCreationOptions([NotNull] IServiceCollection services) { + Services = Check.NotNull(services, nameof(services)); PlugInSources = new PlugInSourceList(); } }