2024-10-08 14:41:41 +08:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts.Attributes;
|
2024-09-30 17:53:14 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts.DependencyInjection;
|
|
|
|
|
|
using TouchSocket.Core;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.Extensions.DependencyInjection
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class DependencyInjectionExtensions
|
|
|
|
|
|
{
|
2024-09-30 23:30:03 +08:00
|
|
|
|
public static void ServiceRegister(this IServiceCollection services, params Assembly[] assemblies)
|
2024-09-30 17:53:14 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (assemblies.Length <= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
assemblies = [Assembly.GetExecutingAssembly()];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var assembly in assemblies)
|
|
|
|
|
|
{
|
|
|
|
|
|
var allTypes = assembly.GetTypes();
|
|
|
|
|
|
foreach (var type in allTypes)
|
|
|
|
|
|
{
|
2024-10-08 14:41:41 +08:00
|
|
|
|
if (typeof(ITransientDependency).IsAssignableFrom(type) && type is { IsClass: true, IsAbstract: false })
|
2024-09-30 17:53:14 +08:00
|
|
|
|
{
|
|
|
|
|
|
var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ITransientDependency"));
|
|
|
|
|
|
foreach (var interfaceType in interfaceTypes)
|
|
|
|
|
|
{
|
2024-10-08 14:41:41 +08:00
|
|
|
|
var attr = type.GetCustomAttribute<ProtocolNameAttribute>();
|
|
|
|
|
|
if (attr == null) continue;
|
|
|
|
|
|
var serviceDescriptor = new ServiceDescriptor(interfaceType, attr.Name, type, ServiceLifetime.Transient);
|
|
|
|
|
|
services.Add(serviceDescriptor);
|
2024-09-30 17:53:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var type in allTypes)
|
|
|
|
|
|
{
|
2024-10-08 14:41:41 +08:00
|
|
|
|
if (typeof(ISingletonDependency).IsAssignableFrom(type) && type is { IsClass: true, IsAbstract: false })
|
2024-09-30 17:53:14 +08:00
|
|
|
|
{
|
|
|
|
|
|
var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ISingletonDependency"));
|
|
|
|
|
|
foreach (var interfaceType in interfaceTypes)
|
|
|
|
|
|
{
|
2024-10-08 14:41:41 +08:00
|
|
|
|
var attr = type.GetCustomAttribute<ProtocolNameAttribute>();
|
|
|
|
|
|
if (attr == null) continue;
|
|
|
|
|
|
var serviceDescriptor = new ServiceDescriptor(interfaceType, attr.Name, type, ServiceLifetime.Singleton);
|
|
|
|
|
|
services.Add(serviceDescriptor);
|
2024-09-30 17:53:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var type in allTypes)
|
|
|
|
|
|
{
|
2024-10-08 14:41:41 +08:00
|
|
|
|
if (typeof(IScopedDependency).IsAssignableFrom(type) && type is { IsClass: true, IsAbstract: false })
|
2024-09-30 17:53:14 +08:00
|
|
|
|
{
|
|
|
|
|
|
var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("IScopedDependency"));
|
|
|
|
|
|
foreach (var interfaceType in interfaceTypes)
|
|
|
|
|
|
{
|
2024-10-08 14:41:41 +08:00
|
|
|
|
var attr = type.GetCustomAttribute<ProtocolNameAttribute>();
|
|
|
|
|
|
if (attr == null) continue;
|
|
|
|
|
|
var serviceDescriptor = new ServiceDescriptor(interfaceType, attr.Name, type, ServiceLifetime.Scoped);
|
|
|
|
|
|
services.Add(serviceDescriptor);
|
2024-09-30 17:53:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-30 23:30:03 +08:00
|
|
|
|
public static void PluginServiceRegister(this IServiceCollection services, string pluginPath = "")
|
2024-09-30 17:53:14 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (pluginPath.IsNullOrWhiteSpace())
|
|
|
|
|
|
{
|
|
|
|
|
|
pluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
|
|
|
|
|
|
}
|
|
|
|
|
|
var assemblies = GetAssembliesFromFolder(pluginPath);
|
|
|
|
|
|
ServiceRegister(services, assemblies.ToArray());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-08 14:41:41 +08:00
|
|
|
|
private static IEnumerable<Assembly> GetAssembliesFromFolder(string folderPath)
|
2024-09-30 17:53:14 +08:00
|
|
|
|
{
|
|
|
|
|
|
var directory = new DirectoryInfo(folderPath);
|
2024-09-30 23:30:03 +08:00
|
|
|
|
if (!directory.Exists) return Enumerable.Empty<Assembly>();
|
|
|
|
|
|
|
2024-09-30 17:53:14 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|