50 lines
1.6 KiB
C#
Raw Normal View History

2024-11-13 00:30:24 +08:00
using System.Reflection;
using Volo.Abp.Modularity;
2024-09-30 17:10:43 +08:00
2024-10-11 11:27:57 +08:00
// ReSharper disable once CheckNamespace
2024-09-30 17:10:43 +08:00
namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{
2024-11-13 00:30:24 +08:00
public static void AddPluginApplications(this IServiceCollection services, string pluginPath = "")
2024-09-30 17:10:43 +08:00
{
2024-11-13 00:30:24 +08:00
if (string.IsNullOrWhiteSpace(pluginPath))
2024-09-30 17:10:43 +08:00
{
2024-11-13 00:30:24 +08:00
pluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
}
var assemblies = GetAssembliesFromFolder(pluginPath);
2024-09-30 17:10:43 +08:00
2024-11-13 00:30:24 +08:00
foreach (var assembly in assemblies)
{
var applicationServiceType = assembly.GetTypes()
.FirstOrDefault(a => a.IsClass && !a.IsAbstract && typeof(AbpModule).IsAssignableFrom(a));
services.AddApplication(applicationServiceType);
}
}
2024-09-30 17:10:43 +08:00
2024-11-13 00:30:24 +08:00
private static IEnumerable<Assembly> GetAssembliesFromFolder(string folderPath)
2024-09-30 17:10:43 +08:00
{
2024-11-13 00:30:24 +08:00
var directory = new DirectoryInfo(folderPath);
if (!directory.Exists) return [];
var files = directory.GetFiles("*.dll");
var assemblies = new List<Assembly>();
foreach (var file in files)
2024-09-30 17:10:43 +08:00
{
2024-11-13 00:30:24 +08:00
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;
2024-09-30 17:10:43 +08:00
}
}
}