diff --git a/src/Volo.Abp.ApplicationContracts/Volo/Abp/Application/Services/IApplicationService.cs b/src/Volo.Abp.ApplicationContracts/Volo/Abp/Application/Services/IApplicationService.cs index 49a7fa3898..1658d3b85c 100644 --- a/src/Volo.Abp.ApplicationContracts/Volo/Abp/Application/Services/IApplicationService.cs +++ b/src/Volo.Abp.ApplicationContracts/Volo/Abp/Application/Services/IApplicationService.cs @@ -3,7 +3,7 @@ namespace Volo.Abp.Application.Services { /// - /// This interface must be implemented by all application services to identify them by convention. + /// This interface must be implemented by all application services to register and identify them by convention. /// public interface IApplicationService : ITransientDependency { diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs index 463a424f36..7350ba5295 100644 --- a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs @@ -1,8 +1,9 @@ using Microsoft.AspNetCore.Mvc; +using Volo.DependencyInjection; namespace Volo.Abp.AspNetCore.Mvc { - public abstract class AbpController : Controller + public abstract class AbpController : Controller, ITransientDependency { } diff --git a/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs b/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs index 448570f6cc..5321e4b60c 100644 --- a/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs +++ b/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs @@ -1,4 +1,6 @@ -using Microsoft.Extensions.DependencyInjection; +using JetBrains.Annotations; +using Microsoft.Extensions.DependencyInjection; +using Volo; using Volo.Abp; using Volo.DependencyInjection; @@ -6,8 +8,10 @@ namespace Microsoft.AspNetCore.Builder { public static class AbpApplicationBuilderExtensions { - public static void InitializeApplication(this IApplicationBuilder app) + public static void InitializeApplication([NotNull] this IApplicationBuilder app) { + Check.NotNull(app, nameof(app)); + app.ApplicationServices.GetRequiredService>().Object = app; app.ApplicationServices.GetRequiredService().Initialize(app.ApplicationServices); } diff --git a/src/Volo.DependencyInjection/Microsoft/Extensions/DependencyInjection/ServiceCollectionRegistrationExtensions.cs b/src/Volo.DependencyInjection/Microsoft/Extensions/DependencyInjection/ServiceCollectionRegistrationExtensions.cs index 2a9503823a..5b340f78bb 100644 --- a/src/Volo.DependencyInjection/Microsoft/Extensions/DependencyInjection/ServiceCollectionRegistrationExtensions.cs +++ b/src/Volo.DependencyInjection/Microsoft/Extensions/DependencyInjection/ServiceCollectionRegistrationExtensions.cs @@ -11,33 +11,35 @@ namespace Microsoft.Extensions.DependencyInjection public static class ServiceCollectionRegistrationExtensions { //TODO: Check if assembly/type is added before or add TryAdd versions of them? - //TODO: Return IServiceCollection from all methods + //TODO: When to use Add, when to use TryAdd? We may think to add conventional interfaces to indicate and attributes to override them. - public static void AddAssemblyOf(this IServiceCollection services) + public static IServiceCollection AddAssemblyOf(this IServiceCollection services) { - services.AddAssembly(typeof(T).GetTypeInfo().Assembly); + return services.AddAssembly(typeof(T).GetTypeInfo().Assembly); } - public static void AddAssembly(this IServiceCollection services, Assembly assembly) + public static IServiceCollection AddAssembly(this IServiceCollection services, Assembly assembly) { var types = AssemblyHelper.GetAllTypes(assembly).Where(t => { var typeInfo = t.GetTypeInfo(); - return typeInfo.IsClass && !typeInfo.IsAbstract && !typeInfo.IsGenericType && !typeInfo.IsDefined(typeof(DisableAutoDependencyInjectionRegistrationAttribute)); + return typeInfo.IsClass && !typeInfo.IsAbstract && !typeInfo.IsGenericType && !typeInfo.IsDefined(typeof(SkipAutoRegistrationAttribute)); }); - - services.AddTypes(types.ToArray()); + + return services.AddTypes(types.ToArray()); } - public static void AddTypes(this IServiceCollection services, params Type[] types) + public static IServiceCollection AddTypes(this IServiceCollection services, params Type[] types) { foreach (var type in types) { services.AddType(type); } + + return services; } - public static void AddType(this IServiceCollection services, Type type) + public static IServiceCollection AddType(this IServiceCollection services, Type type) { //TODO: Make this code extensible, so we can add other conventions! @@ -58,9 +60,11 @@ namespace Microsoft.Extensions.DependencyInjection services.AddScoped(serviceType, type); } } + + return services; } - - private static List FindServiceTypes(Type type) + + private static IEnumerable FindServiceTypes(Type type) { var customExposedServices = type.GetTypeInfo().GetCustomAttributes().OfType().SelectMany(p => p.GetExposedServiceTypes()).ToList(); if (customExposedServices.Any()) @@ -68,9 +72,7 @@ namespace Microsoft.Extensions.DependencyInjection return customExposedServices; } - var serviceTypes = new List(); - - serviceTypes.Add(type); + var serviceTypes = new List { type }; foreach (var interfaceType in type.GetTypeInfo().GetInterfaces()) { diff --git a/src/Volo.DependencyInjection/Volo/DependencyInjection/DisableAutoDependencyInjectionRegistrationAttribute.cs b/src/Volo.DependencyInjection/Volo/DependencyInjection/DisableAutoDependencyInjectionRegistrationAttribute.cs deleted file mode 100644 index 6d40fd9a1f..0000000000 --- a/src/Volo.DependencyInjection/Volo/DependencyInjection/DisableAutoDependencyInjectionRegistrationAttribute.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace Volo.DependencyInjection -{ - public class DisableAutoDependencyInjectionRegistrationAttribute : Attribute - { - - } -} \ No newline at end of file diff --git a/src/Volo.DependencyInjection/Volo/DependencyInjection/ExposeServicesAttribute.cs b/src/Volo.DependencyInjection/Volo/DependencyInjection/ExposeServicesAttribute.cs index 276bf25d9f..72c05fdc2a 100644 --- a/src/Volo.DependencyInjection/Volo/DependencyInjection/ExposeServicesAttribute.cs +++ b/src/Volo.DependencyInjection/Volo/DependencyInjection/ExposeServicesAttribute.cs @@ -8,7 +8,7 @@ namespace Volo.DependencyInjection public ExposeServicesAttribute(params Type[] exposedServiceTypes) { - ExposedServiceTypes = exposedServiceTypes; + ExposedServiceTypes = exposedServiceTypes ?? new Type[]; } public Type[] GetExposedServiceTypes() diff --git a/src/Volo.DependencyInjection/Volo/DependencyInjection/SkipAutoRegistrationAttribute.cs b/src/Volo.DependencyInjection/Volo/DependencyInjection/SkipAutoRegistrationAttribute.cs new file mode 100644 index 0000000000..b83feeee44 --- /dev/null +++ b/src/Volo.DependencyInjection/Volo/DependencyInjection/SkipAutoRegistrationAttribute.cs @@ -0,0 +1,9 @@ +using System; + +namespace Volo.DependencyInjection +{ + public class SkipAutoRegistrationAttribute : Attribute + { + + } +} \ No newline at end of file