diff --git a/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs b/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs index fb0daf42e5..11a81aa61b 100644 --- a/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs +++ b/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs @@ -1,39 +1,75 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; using Autofac.Core; using Autofac.Extras.DynamicProxy; -using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Castle.DynamicProxy; using Volo.Abp.DependencyInjection; +using Volo.Abp.Modularity; namespace Autofac.Builder { public static class AbpRegistrationBuilderExtensions { public static IRegistrationBuilder ConfigureAbpConventions( - this IRegistrationBuilder registrationBuilder, - IServiceCollection services) + this IRegistrationBuilder registrationBuilder, + IModuleContainer moduleContainer, + ServiceRegistrationActionList registrationActionList) where TConcreteReflectionActivatorData : ConcreteReflectionActivatorData { - registrationBuilder = registrationBuilder.PropertiesAutowired(); - var serviceType = registrationBuilder.RegistrationData.Services.OfType().FirstOrDefault()?.ServiceType; if (serviceType == null) { return registrationBuilder; } + var implementationType = registrationBuilder.ActivatorData.ImplementationType; + if (implementationType == null) + { + return registrationBuilder; + } + + registrationBuilder = registrationBuilder.EnablePropertyInjection(moduleContainer, implementationType); + registrationBuilder = registrationBuilder.InvokeRegistrationActions(registrationActionList, serviceType); + + return registrationBuilder; + } + + private static IRegistrationBuilder InvokeRegistrationActions( + this IRegistrationBuilder registrationBuilder, + ServiceRegistrationActionList registrationActionList, + Type serviceType) + where TConcreteReflectionActivatorData : ConcreteReflectionActivatorData + { var serviceRegistredArgs = new OnServiceRegistredContext(serviceType); - foreach (var registrationAction in services.GetRegistrationActionList()) + foreach (var registrationAction in registrationActionList) { registrationAction.Invoke(serviceRegistredArgs); } if (serviceRegistredArgs.Interceptors.Any()) { - registrationBuilder = registrationBuilder.AddInterceptors(serviceRegistredArgs.Interceptors.ToArray()); + registrationBuilder = registrationBuilder.AddInterceptors( + serviceType, + serviceRegistredArgs.Interceptors + ); + } + + return registrationBuilder; + } + + private static IRegistrationBuilder EnablePropertyInjection( + this IRegistrationBuilder registrationBuilder, + IModuleContainer moduleContainer, + Type implementationType) + where TConcreteReflectionActivatorData : ConcreteReflectionActivatorData + { + //Enable Property Injection only for types in an assembly containing an AbpModule + if (moduleContainer.Modules.Any(m => m.Assembly == implementationType.Assembly)) + { + registrationBuilder = registrationBuilder.PropertiesAutowired(); } return registrationBuilder; @@ -41,11 +77,10 @@ namespace Autofac.Builder private static IRegistrationBuilder AddInterceptors( this IRegistrationBuilder registrationBuilder, - Type[] interceptors) + Type serviceType, + IEnumerable interceptors) where TConcreteReflectionActivatorData : ConcreteReflectionActivatorData { - var serviceType = registrationBuilder.RegistrationData.Services.OfType().FirstOrDefault()?.ServiceType; - registrationBuilder = serviceType.GetTypeInfo().IsInterface ? registrationBuilder.EnableInterfaceInterceptors() : registrationBuilder.EnableClassInterceptors(); diff --git a/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs b/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs index 04434721ab..f6437cc7c7 100644 --- a/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs +++ b/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs @@ -27,6 +27,7 @@ using System; using System.Reflection; using Autofac.Builder; using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Modularity; namespace Autofac.Extensions.DependencyInjection { @@ -100,6 +101,9 @@ namespace Autofac.Extensions.DependencyInjection ContainerBuilder builder, IServiceCollection services) { + var moduleContainer = services.GetSingletonInstance(); + var registrationActionList = services.GetRegistrationActionList(); + foreach (var service in services) { if (service.ImplementationType != null) @@ -119,7 +123,7 @@ namespace Autofac.Extensions.DependencyInjection .RegisterType(service.ImplementationType) .As(service.ServiceType) .ConfigureLifecycle(service.Lifetime) - .ConfigureAbpConventions(services); + .ConfigureAbpConventions(moduleContainer, registrationActionList); } } else if (service.ImplementationFactory != null) @@ -131,6 +135,7 @@ namespace Autofac.Extensions.DependencyInjection }) .ConfigureLifecycle(service.Lifetime) .CreateRegistration(); + //TODO: ConfigureAbpConventions ? builder.RegisterComponent(registration); } diff --git a/src/Volo.Abp/System/AbpTypeExtensions.cs b/src/Volo.Abp/System/AbpTypeExtensions.cs index 4440bfb9cf..2cbd8dea77 100644 --- a/src/Volo.Abp/System/AbpTypeExtensions.cs +++ b/src/Volo.Abp/System/AbpTypeExtensions.cs @@ -4,6 +4,7 @@ namespace System { public static class AbpTypeExtensions { + //TODO: This method can be removed because not needed anymore! public static Assembly GetAssembly(this Type type) { return type.GetTypeInfo().Assembly; diff --git a/src/Volo.Abp/Volo/Abp/AbpApplicationBase.cs b/src/Volo.Abp/Volo/Abp/AbpApplicationBase.cs index fa0158911b..864a49b6aa 100644 --- a/src/Volo.Abp/Volo/Abp/AbpApplicationBase.cs +++ b/src/Volo.Abp/Volo/Abp/AbpApplicationBase.cs @@ -32,8 +32,8 @@ namespace Volo.Abp var options = new AbpApplicationCreationOptions(services); optionsAction?.Invoke(options); - services.AddSingleton(_ => this); - services.AddSingleton(_ => this); + services.AddSingleton(this); + services.AddSingleton(this); services.AddCoreAbpServices(); Modules = LoadModules(services, options); diff --git a/src/Volo.Abp/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs b/src/Volo.Abp/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs index 9be306c8c5..a18e8a6848 100644 --- a/src/Volo.Abp/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs +++ b/src/Volo.Abp/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs @@ -16,7 +16,7 @@ namespace Volo.Abp services, optionsAction) { - services.AddSingleton(_ => this); + services.AddSingleton(this); } public void Initialize(IServiceProvider serviceProvider) diff --git a/src/Volo.Abp/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs b/src/Volo.Abp/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs index afcf18c419..c46c4a8373 100644 --- a/src/Volo.Abp/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs +++ b/src/Volo.Abp/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs @@ -29,7 +29,7 @@ namespace Volo.Abp services, optionsAction) { - Services.AddSingleton(_ => this); + Services.AddSingleton(this); } public void Initialize() diff --git a/src/Volo.Abp/Volo/Abp/Modularity/AbpModuleDescriptor.cs b/src/Volo.Abp/Volo/Abp/Modularity/AbpModuleDescriptor.cs index a4b679d5ee..69fada953c 100644 --- a/src/Volo.Abp/Volo/Abp/Modularity/AbpModuleDescriptor.cs +++ b/src/Volo.Abp/Volo/Abp/Modularity/AbpModuleDescriptor.cs @@ -10,6 +10,8 @@ namespace Volo.Abp.Modularity { public Type Type { get; } + public Assembly Assembly { get; } + public IAbpModule Instance { get; } public bool IsLoadedAsPlugIn { get; } @@ -31,6 +33,7 @@ namespace Volo.Abp.Modularity } Type = type; + Assembly = type.GetAssembly(); Instance = instance; IsLoadedAsPlugIn = isLoadedAsPlugIn; diff --git a/src/Volo.Abp/Volo/Abp/Modularity/IAbpModuleDescriptor.cs b/src/Volo.Abp/Volo/Abp/Modularity/IAbpModuleDescriptor.cs index 542010ee04..96d4a64ea7 100644 --- a/src/Volo.Abp/Volo/Abp/Modularity/IAbpModuleDescriptor.cs +++ b/src/Volo.Abp/Volo/Abp/Modularity/IAbpModuleDescriptor.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Reflection; namespace Volo.Abp.Modularity { @@ -7,6 +8,8 @@ namespace Volo.Abp.Modularity { Type Type { get; } + Assembly Assembly { get; } + IAbpModule Instance { get; } bool IsLoadedAsPlugIn { get; } diff --git a/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo.Abp.AspNetCore.Mvc.Tests.csproj b/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo.Abp.AspNetCore.Mvc.Tests.csproj index 1be938a154..264a146eb0 100644 --- a/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo.Abp.AspNetCore.Mvc.Tests.csproj +++ b/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo.Abp.AspNetCore.Mvc.Tests.csproj @@ -17,6 +17,7 @@ + diff --git a/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/Startup.cs b/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/Startup.cs index 1af71d75e8..565b801b15 100644 --- a/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/Startup.cs +++ b/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/Startup.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Builder; +using System; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -7,9 +8,16 @@ namespace Volo.Abp.AspNetCore.App { public class Startup { - public void ConfigureServices(IServiceCollection services) + public IServiceProvider ConfigureServices(IServiceCollection services) { - services.AddApplication(); + services.AddApplication(options => + { + options.UseAutofac(); + }); + + //TODO: This is needed because ASP.NET Core does not use IServiceProviderFactory! + return services.BuildServiceProviderFromFactory(); + //return services.BuildServiceProvider(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)