Skip to content

Commit

Permalink
Fix bug on DLL resolve for plugins (#380)
Browse files Browse the repository at this point in the history
* Fix bug on DLL resolve for plugins

* Increase waiting time
  • Loading branch information
antoineatstariongroup authored Jan 24, 2025
1 parent 8cb6f14 commit 7725cc3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CodeQuality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:
run: dotnet-coverage collect --output CoverageResults/integration.test.report.coverage.xml --output-format cobertura --session-id integrationtestsession "dotnet run --project CometServer/CometServer.csproj -c Debug" &

- name: Wait for API to start
run: sleep 45 # Adjust as necessary to ensure the API is up
run: sleep 60 # Adjust as necessary to ensure the API is up

- name: Checkout Integration Test Suite
uses: actions/checkout@v4
Expand Down
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 7725cc3

Please sign in to comment.