Skip to content

Commit

Permalink
Fix bug on DLL resolve for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineatstariongroup committed Jan 23, 2025
1 parent 8cb6f14 commit 549ec6f
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions CometServer/Authentication/AuthenticationPluginInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ namespace CometServer.Authentication
/// <summary>
/// The injector loads up authenticator plugins.
/// </summary>
public class AuthenticationPluginInjector : IAuthenticationPluginInjector
public class AuthenticationPluginInjector : IAuthenticationPluginInjector, IDisposable
{
/// <summary>
/// A collection of <see cref="Assembly" /> that has been discovered
/// </summary>
private List<Assembly> discoveredAssemblies = new List<Assembly>();

/// <summary>
/// The name of the folder where all authentication modules reside.
/// </summary>
Expand All @@ -59,6 +64,7 @@ public class AuthenticationPluginInjector : IAuthenticationPluginInjector
public AuthenticationPluginInjector(ILogger<AuthenticationPluginInjector> logger)
{
this.logger = logger;
AppDomain.CurrentDomain.AssemblyResolve += this.OnAssemblyResolve;

this.Plugins = this.LoadPlugins();

Expand Down Expand Up @@ -103,6 +109,7 @@ private static string[] GetFolders()
/// <returns>The <see cref="List{T}"/> of <see cref="IAuthenticatorPlugin"/> modules</returns>
private ReadOnlyCollection<IAuthenticatorPlugin> LoadPlugins()
{
this.discoveredAssemblies.Clear();
var sw = Stopwatch.StartNew();

var result = new List<IAuthenticatorPlugin>();
Expand All @@ -116,15 +123,20 @@ private ReadOnlyCollection<IAuthenticatorPlugin> LoadPlugins()
// load all assemblies types encountered in the plugin folders that implement the IAuthenticatorPlugin interface
foreach (var pluginFolder in pluginFolders)
{
foreach (var assembly in new DirectoryInfo(pluginFolder).GetFiles().Where(file => file.Extension == ".dll")
.Select(file => Assembly.LoadFile(file.FullName)))
var assemblies = new DirectoryInfo(pluginFolder).GetFiles().Where(file => file.Extension == ".dll")
.Select(file => Assembly.LoadFile(file.FullName))
.ToList();

foreach (var assembly in assemblies)
{
builder.RegisterAssemblyTypes(assembly)
.Where(x => typeof(IAuthenticatorPlugin).IsAssignableFrom(x))
.AsImplementedInterfaces()
.PropertiesAutowired()
.SingleInstance();
}

this.discoveredAssemblies.AddRange(assemblies);
}

var container = builder.Build();
Expand All @@ -138,5 +150,25 @@ private ReadOnlyCollection<IAuthenticatorPlugin> LoadPlugins()

return result.AsReadOnly();
}

/// <summary>
/// Helps resolving assemblies that may be required for loaded plugins
/// </summary>
/// <param name="sender">The sender object</param>
/// <param name="args">The <see cref="ResolveEventArgs"/></param>
/// <returns>The resolved assemblies, if found</returns>
private Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
{
return this.discoveredAssemblies.FirstOrDefault(x => x.FullName == args.Name);
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
AppDomain.CurrentDomain.AssemblyResolve -= this.OnAssemblyResolve;
}
}
}

0 comments on commit 549ec6f

Please sign in to comment.