Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Handling F# assemblies correctly by catching reflection exceptions an…
…d falling back to GetTypeInfo
  • Loading branch information
jbogard committed Feb 22, 2026
commit 702cd2d7bede63006fb036aad3a0cecbffb155a0
20 changes: 16 additions & 4 deletions src/MediatR/Registration/ServiceRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static void AddMediatRClasses(IServiceCollection services, MediatRService
var arity = multiOpenInterface.GetGenericArguments().Length;

var concretions = assembliesToScan
.SelectMany(a => a.DefinedTypes)
.SelectMany(a => a.GetLoadableDefinedTypes())
.Where(type => type.FindInterfacesThatClose(multiOpenInterface).Any())
.Where(type => type.IsConcrete() && type.IsOpenGeneric())
.Where(type => type.GetGenericArguments().Length == arity)
Expand All @@ -103,7 +103,7 @@ private static void ConnectImplementationsToTypesClosing(Type openRequestInterfa
var genericInterfaces = new List<Type>();

var types = assembliesToScan
.SelectMany(a => a.DefinedTypes)
.SelectMany(a => a.GetLoadableDefinedTypes())
.Where(t => !t.ContainsGenericParameters || configuration.RegisterGenericHandlers)
.Where(t => t.IsConcrete() && t.FindInterfacesThatClose(openRequestInterface).Any())
.Where(configuration.TypeEvaluator)
Expand Down Expand Up @@ -233,7 +233,7 @@ private static (Type Service, Type Implementation) GetConcreteRegistrationTypes(

var typesThatCanCloseForEachParameter = constraintsForEachParameter
.Select(constraints => assembliesToScan
.SelectMany(assembly => assembly.GetTypes())
.SelectMany(assembly => assembly.GetLoadableDefinedTypes())
.Where(type => type.IsClass && !type.IsAbstract && constraints.All(constraint => constraint.IsAssignableFrom(type))).ToList()
).ToList();

Expand Down Expand Up @@ -385,6 +385,18 @@ private static void Fill<T>(this IList<T> list, T value)
list.Add(value);
}

private static IEnumerable<Type> GetLoadableDefinedTypes(this Assembly assembly)
{
try
{
return assembly.DefinedTypes.Cast<Type>();
Comment thread
jbogard marked this conversation as resolved.
Outdated
}
catch (ReflectionTypeLoadException ex)
{
return ex.Types.Where(t => t != null).Cast<Type>();
Comment thread
jbogard marked this conversation as resolved.
Outdated
}
}

private static bool HasNestedGenericResponseType(Type openBehaviorType)
{
var iface = openBehaviorType.GetInterfaces()
Expand Down Expand Up @@ -425,7 +437,7 @@ private static void RegisterClosedBehaviorsFromAssemblies(
var behaviorParams = openBehaviorType.GetGenericArguments();

var requests = assemblies
.SelectMany(a => a.DefinedTypes)
.SelectMany(a => a.GetLoadableDefinedTypes())
.Where(t => t.IsConcrete() && !t.ContainsGenericParameters)
.SelectMany(t => t.GetInterfaces()
.Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IRequest<>))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace MediatR.Extensions.Microsoft.DependencyInjection.Tests;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Shouldly;
using Xunit;

Expand Down Expand Up @@ -87,4 +89,30 @@ public void ShouldResolveVoidGenericPingRequestHandler()
{
_provider.GetService<IRequestHandler<VoidGenericPing<Pong>>>().ShouldNotBeNull();
}

[Fact]
public void ShouldNotThrowWhenAssemblyThrowsReflectionTypeLoadException()
{
IServiceCollection services = new ServiceCollection();
services.AddFakeLogging();
services.AddSingleton(new Logger());

Action registration = () => services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssemblies(typeof(Ping).Assembly, new BrokenAssembly());
});

registration.ShouldNotThrow();

var provider = services.BuildServiceProvider();
provider.GetService<IRequestHandler<Ping, Pong>>().ShouldNotBeNull();
}

private class BrokenAssembly : Assembly
{
public override IEnumerable<TypeInfo> DefinedTypes =>
throw new ReflectionTypeLoadException(
new Type?[] { typeof(string), null, typeof(int) },
new Exception?[] { null, new Exception("ByRef-like type cannot be loaded"), null });
}
}