Skip to content

Trimming and Native AOT

MassiveJobs never looks for jobs by reflection. A source generator, shipped in the MassiveJobs.Core package, runs in every project that references it and writes the code that registers and calls its jobs. So:

  • both libraries are free of trimming and AOT warnings on .NET, and apps that use them can be trimmed or compiled with Native AOT;
  • a class that can’t be a job is a compile error, not a startup failure;
  • nothing scans assemblies at startup.

The generator needs a C# compiler from the .NET 8 SDK or Visual Studio 2022 17.8 on. The code it writes is C# 7.3, so .NET Framework projects work too.

  • Classes derived from Job or JobAsync. Every concrete, non-generic one in the project, listed for WithJobsFromAssembly. They run through their base class.
  • Plain classes passed to WithJob<T>(). For each, the generator writes the call to its Perform. Only calls it can see at compile time count: WithJob<T>() with a concrete type, in your code.

If you wrap WithJob in a generic method of your own, mark its type parameter with [JobTypeArgument] (and an arguments type parameter with [JobArgsTypeArgument]): the generator then looks at the calls of your method instead.

using System.Diagnostics.CodeAnalysis;
using MassiveJobs.Core;
using MassiveJobs.Core.Generated;
public static class JobRegistrations
{
public static MassiveJobsBuilder WithAuditedJob<
[JobTypeArgument, DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TJob>(
this MassiveJobsBuilder builder) where TJob : class
=> builder.WithJob<TJob>();
}

DynamicallyAccessedMembers tells the trimmer to keep the job’s constructors, which MassiveJobs calls.

Id Severity
MJ0001 error The class can’t be a job: no public Perform method of a supported shape.
MJ0002 error Perform methods for several argument types: register it with WithJob<TJob, TArgs>().
MJ0003 error No Perform method for the arguments type given to WithJob<TJob, TArgs>().
MJ0004 error The job type is a type parameter the generator can’t follow: pass a concrete type, or mark the parameter [JobTypeArgument].
MJ0005 warning A private or protected nested job class: WithJobsFromAssembly can’t register it. Make it internal or public.
MJ0006 error A private or protected class passed to WithJob: make it internal or public.
MJ0007 warning A method’s [JobTypeArgument] type parameter can be inferred from its arguments. The generator sees only calls that name it (AddJob<MyJob>(…)): name it in every call.

The arguments are serialized with System.Text.Json. Without reflection it needs metadata for their types: list them in a JsonSerializerContext and give it to MassiveJobs.

[JsonSerializable(typeof(ResizeArgs))]
[JsonSerializable(typeof(InvoiceArgs))]
internal partial class JobsJsonContext : JsonSerializerContext
{
}
builder.Services.AddMassiveJobs(o => o.JsonTypeInfoResolver = JobsJsonContext.Default)
.WithJobsFromAssemblyOf<Program>()
.UseRabbitMq();

Strings and other primitive types need no metadata. MassiveJobs checks at startup that every registered job’s arguments type has some, and fails with the names of those that don’t. Without a resolver, reflection-based serialization is used where it’s enabled.