123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- using Masuit.Tools.AspNetCore.ResumeFileResults.Executor;
- using Masuit.Tools.AspNetCore.ResumeFileResults.ResumeFileResult;
- using Masuit.Tools.Files;
- using Microsoft.AspNetCore.Mvc.Infrastructure;
- using Microsoft.Extensions.DependencyInjection.Extensions;
- using System.Reflection;
- using Masuit.Tools.Reflection;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Hosting;
- namespace Masuit.Tools.Core.AspNetCore;
- /// <summary>
- /// 依赖注入ServiceCollection容器扩展方法
- /// </summary>
- public static class ServiceCollectionExtensions
- {
- /// <summary>
- /// 注入断点续传服务
- /// </summary>
- /// <param name="services"></param>
- /// <returns></returns>
- public static IServiceCollection AddResumeFileResult(this IServiceCollection services)
- {
- services.TryAddSingleton<IActionResultExecutor<ResumePhysicalFileResult>, ResumePhysicalFileResultExecutor>();
- services.TryAddSingleton<IActionResultExecutor<ResumeVirtualFileResult>, ResumeVirtualFileResultExecutor>();
- services.TryAddSingleton<IActionResultExecutor<ResumeFileStreamResult>, ResumeFileStreamResultExecutor>();
- services.TryAddSingleton<IActionResultExecutor<ResumeFileContentResult>, ResumeFileContentResultExecutor>();
- return services;
- }
- /// <summary>
- /// 注入7z压缩
- /// </summary>
- /// <param name="services"></param>
- /// <returns></returns>
- public static IServiceCollection AddSevenZipCompressor(this IServiceCollection services)
- {
- services.AddHttpClient<ISevenZipCompressor, SevenZipCompressor>();
- return services;
- }
- /// <summary>
- /// 自动扫描需要注册的服务,被ServiceInject标记的class可自动注入
- /// </summary>
- /// <param name="services"></param>
- public static void AutoRegisterServices(this IServiceCollection services)
- {
- var assemblies = AppDomain.CurrentDomain.GetAssemblies();
- services.RegisterServiceByAttribute(assemblies);
- services.RegisterBackgroundService(assemblies);
- }
- /// <summary>
- /// 自动扫描需要注册的服务,被ServiceInject标记的class可自动注入
- /// </summary>
- /// <param name="services"></param>
- /// <param name="assemblies"></param>
- public static void AutoRegisterServices(this IServiceCollection services, params Assembly[] assemblies)
- {
- services.RegisterServiceByAttribute(assemblies);
- services.RegisterBackgroundService(assemblies);
- }
- /// <summary>
- /// 通过 ServiceAttribute 批量注册服务
- /// </summary>
- /// <param name="services"></param>
- private static void RegisterServiceByAttribute(this IServiceCollection services, IEnumerable<Assembly> assemblies)
- {
- var types = assemblies.SelectMany(t => t.GetExportedTypes()).Where(t => (t.GetCustomAttributes(typeof(ServiceInjectAttribute), false).Length > 0 || t.GetInterfaces().Intersect([typeof(IScoped), typeof(ITransient), typeof(ISingleton)]).Any()) && t.IsClass && !t.IsAbstract);
- foreach (var type in types)
- {
- var typeInterface = type.GetInterfaces().Except([typeof(IScoped), typeof(ITransient), typeof(ISingleton)]).OrderBy(t => t.Name.HammingDistance(type.Name)).FirstOrDefault();
- if (typeInterface == null)
- {
- //服务非继承自接口的直接注入
- switch (type.GetCustomAttribute<ServiceInjectAttribute>()?.Lifetime)
- {
- case ServiceLifetime.Singleton:
- services.TryAddSingleton(type);
- break;
- case ServiceLifetime.Scoped:
- services.TryAddScoped(type);
- break;
- case ServiceLifetime.Transient:
- services.TryAddTransient(type);
- break;
- default:
- if (type.IsImplementsOf(typeof(IScoped)))
- {
- services.TryAddScoped(type);
- }
- else if (type.IsImplementsOf(typeof(ISingleton)))
- {
- services.TryAddSingleton(type);
- }
- else if (type.IsImplementsOf(typeof(ITransient)))
- {
- services.TryAddTransient(type);
- }
- break;
- }
- }
- else
- {
- //服务继承自接口的和接口一起注入
- switch (type.GetCustomAttribute<ServiceInjectAttribute>()?.Lifetime)
- {
- case ServiceLifetime.Singleton:
- services.TryAddSingleton(type);
- services.TryAddSingleton(typeInterface, type);
- break;
- case ServiceLifetime.Scoped:
- services.TryAddScoped(type);
- services.TryAddScoped(typeInterface, type);
- break;
- case ServiceLifetime.Transient:
- services.TryAddTransient(type);
- services.TryAddTransient(typeInterface, type);
- break;
- default:
- if (type.IsImplementsOf(typeof(IScoped)))
- {
- services.TryAddScoped(type);
- services.TryAddSingleton(typeInterface, type);
- }
- else if (type.IsImplementsOf(typeof(ISingleton)))
- {
- services.TryAddSingleton(type);
- services.TryAddSingleton(typeInterface, type);
- }
- else if (type.IsImplementsOf(typeof(ITransient)))
- {
- services.TryAddTransient(type);
- services.TryAddTransient(typeInterface, type);
- }
- break;
- }
- }
- }
- }
- /// <summary>
- /// 注册后台服务
- /// </summary>
- /// <param name="services"></param>
- private static void RegisterBackgroundService(this IServiceCollection services, IEnumerable<Assembly> assemblies)
- {
- var types = assemblies.SelectMany(t => t.GetTypes()).Where(t => typeof(BackgroundService).IsAssignableFrom(t) && !t.IsAbstract);
- foreach (var type in types)
- {
- services.TryAddSingleton(typeof(IHostedService), type);
- }
- }
- }
- [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
- public class ServiceInjectAttribute : Attribute
- {
- public ServiceInjectAttribute()
- {
- }
- public ServiceInjectAttribute(ServiceLifetime lifetime)
- {
- Lifetime = lifetime;
- }
- public ServiceLifetime Lifetime { get; set; } = ServiceLifetime.Transient;
- }
- public interface IScoped;
- public interface ITransient;
- public interface ISingleton;
|