We are using MediatR for our application, and running on Hololens 2 with the IL2CPP backend with Unity 2020.1.11. We configured the linker not to strip the assemblies we need.
<linker>
<!-- other preserved assemblies -->
<assembly fullname="MediatR" preserve="all" />
<assembly fullname="OurAssembly" preseve="all"/>
<!-- other preserved assemblies -->
</linker>
Requests that have a specified return value are working fine:
public sealed class LoadScan3DFile : IRequest<PointSet> // PointSet is a class
{
public LoadScan3DFile(Guid measurementFileId) => MeasurementFileId = measurementFileId;
public Guid MeasurementFileId { get; }
}
But requests which don’t return anything are actually requests which return the type Unit (From mediatR github) Request Unit
So a simple request:
public sealed class UnzipDatabase : IRequest
{
public string TargetZipFile { get; }
public UnzipDatabase(string targetZipFile) => TargetZipFile = targetZipFile;
}
Will not be executed when sent through MediatR, and will produce the following exception:
```System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ExecutionEngineException: Attempting to call method 'MediatR.Internal.RequestHandlerWrapperImpl`2[OurAssembly.UnzipDatabase, OurAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null],[MediatR.Unit, MediatR, Version=9.0.0.0, Culture=neutral, PublicKeyToken=null]]::.ctor' for which no ahead of time (AOT) code was generated.```
But will work when I introduce some response type which is a class:
public sealed class UnzipDatabase : IRequest<Result>
{
public string TargetZipFile { get; }
public UnzipDatabase(string targetZipFile) => TargetZipFile = targetZipFile;
}
public sealed class Result
{
private Result()
{
IsSuccessful = true;
Messages = ImmutableArray<string>.Empty;
}
private Result(ImmutableArray<string> messages)
{
IsSuccessful = false;
Messages = messages;
}
public bool IsSuccessful { get; }
public ImmutableArray<string> Messages { get; }
public static Result Success { get; } = new Result();
public static Result Failure(string message) => new Result(ImmutableArray.Create(message));
public static Result Failure(Result previous, string message) => new Result(previous.Messages.Add(message));
}
The class used by MediatR which will not get it’s constructor generated can be found here.
Is this a bug in IL2CPP? (In the editor which uses the mono backend everything works fine. I have tried both Debug and Master builds for IL2CPP).