2024-11-13 00:30:24 +08:00

93 lines
4.4 KiB
C#

//using JiShe.CollectBus.Common.Extensions.DependencyInjections;
//using JiShe.CollectBus.Protocol.Contracts.Attributes;
//using System.Reflection;
//using TouchSocket.Core;
//using System;
//using Serilog;
//// ReSharper disable once CheckNamespace
//namespace Microsoft.Extensions.DependencyInjection
//{
// public static class DependencyInjectionExtensions
// {
// public static void PluginServiceRegister(this IServiceCollection services, string pluginPath = "")
// {
// if (pluginPath.IsNullOrWhiteSpace())
// {
// pluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
// }
// var assemblies = GetAssembliesFromFolder(pluginPath);
// foreach (var assembly in assemblies)
// {
// var allTypes = assembly.GetTypes();
// foreach (var type in allTypes)
// {
// if (typeof(ISingletonDependency).IsAssignableFrom(type) && type is { IsClass: true, IsAbstract: false })
// {
// var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ISingletonDependency"));
// foreach (var interfaceType in interfaceTypes)
// {
// var attr = type.GetCustomAttribute<ProtocolNameAttribute>();
// if (attr == null) continue;
// Log.Logger.Information($"正在加载插件{attr.Name}...");
// var serviceDescriptor = new ServiceDescriptor(interfaceType, attr.Name, type, ServiceLifetime.Singleton);
// services.Add(serviceDescriptor);
// }
// }
// if (typeof(ITransientDependency).IsAssignableFrom(type) && type is { IsClass: true, IsAbstract: false })
// {
// var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ITransientDependency"));
// foreach (var interfaceType in interfaceTypes)
// {
// var attr = type.GetCustomAttribute<ProtocolNameAttribute>();
// if (attr == null) continue;
// Log.Logger.Information($"正在加载插件{attr.Name}...");
// var serviceDescriptor = new ServiceDescriptor(interfaceType, attr.Name, type, ServiceLifetime.Transient);
// services.Add(serviceDescriptor);
// }
// }
// if (typeof(IScopedDependency).IsAssignableFrom(type) && type is { IsClass: true, IsAbstract: false })
// {
// var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("IScopedDependency"));
// foreach (var interfaceType in interfaceTypes)
// {
// var attr = type.GetCustomAttribute<ProtocolNameAttribute>();
// if (attr == null) continue;
// Log.Logger.Information($"正在加载插件{attr.Name}...");
// var serviceDescriptor = new ServiceDescriptor(interfaceType, attr.Name, type, ServiceLifetime.Scoped);
// services.Add(serviceDescriptor);
// }
// }
// }
// }
// }
// private static IEnumerable<Assembly> GetAssembliesFromFolder(string folderPath)
// {
// var directory = new DirectoryInfo(folderPath);
// if (!directory.Exists) return [];
// var files = directory.GetFiles("*.dll");
// var assemblies = new List<Assembly>();
// foreach (var file in files)
// {
// try
// {
// var assembly = Assembly.LoadFrom(file.FullName);
// assemblies.Add(assembly);
// }
// catch (Exception ex)
// {
// Console.WriteLine($"Error loading assembly from {file.FullName}: {ex.Message}");
// }
// }
// return assemblies;
// }
// }
//}