CLI: codegen Command
JasperFx provides a codegen command through its CLI tooling that lets you manage generated code ahead of time. This is essential for production deployments where you want the fastest possible startup by avoiding runtime compilation.
Setup
To use the codegen command, your application must integrate the JasperFx command line extensions:
public static async Task<int> SetupWithCodegenCommand(string[] args)
{
return await Host
.CreateDefaultBuilder()
.ApplyJasperFxExtensions()
.RunJasperFxCommands(args);
// Run with: dotnet run -- codegen preview
// Run with: dotnet run -- codegen write
// Run with: dotnet run -- codegen delete
}TypeLoadMode
The TypeLoadMode enum on GenerationRules controls how generated types are loaded at runtime.
Runtime compilation is a separate package
Compiling generated code at runtime, which Dynamic always does and Auto does when a pre-built type is missing, needs the JasperFx.RuntimeCompiler package and its Roslyn dependencies, registered with services.AddRuntimeCompilation(). Marten and Wolverine register it for you when they need it. codegen write needs it too, but an application that only ever runs in Static mode doesn't, and leaving it out keeps Roslyn out of the deployment. See Publishing AOT.
In the net11.0 build, runtime-compiled code uses .NET 11's runtime-async methods instead of compiler-built async state machines, which allocates less. Turn it off with GenerationRules.UseRuntimeAsync = false (Wolverine exposes it as opts.CodeGeneration.UseRuntimeAsync) if a profiler or APM agent misbehaves with it. It has no effect in the net10.0 build.
Dynamic
public static void ConfigureDynamicMode()
{
// Always generate types at runtime. Best for development.
var rules = new GenerationRules("MyApp.Generated")
{
TypeLoadMode = TypeLoadMode.Dynamic
};
}Always generates and compiles types at runtime. This is the default and is the best choice during development because changes to configuration are immediately reflected without a separate build step.
Auto
public static void ConfigureAutoMode()
{
// Try to load pre-built types first, fall back to runtime generation
var rules = new GenerationRules("MyApp.Generated")
{
TypeLoadMode = TypeLoadMode.Auto
};
}Tries to load pre-built types from the application assembly first. If the types are not found, falls back to runtime generation and exports the new source code. This is a good middle ground for staging environments.
Static
public static void ConfigureStaticMode()
{
// Types must be pre-built in the application assembly.
// Throws if generated types are missing.
var rules = new GenerationRules("MyApp.Generated")
{
TypeLoadMode = TypeLoadMode.Static
};
}Types must already exist in the pre-built application assembly. If any expected type is missing, the application fails at startup with an ExpectedTypeMissingException that says whether nothing generated reached the binary at all or only that type is missing, rather than at first use. This provides the fastest startup time and is recommended for production.
Commands
codegen preview
dotnet run -- codegen previewGenerates all source code and writes it to the console without modifying any files. Use this to inspect the generated code before committing it.
codegen write
dotnet run -- codegen writeGenerates all source code and writes the files into the project directory. The generated files are then compiled into the assembly on the next dotnet build. After writing, you can switch to TypeLoadMode.Static or TypeLoadMode.Auto to load the pre-built types.
For the Docker pattern that runs codegen write inside the build stage so a production image ships with pre-generated artifacts, see Pre-generating Codegen in Docker.
codegen delete
dotnet run -- codegen deleteRemoves all previously generated source files from the project directory. Use this to clean up before regenerating or when switching back to TypeLoadMode.Dynamic.
Recommended Workflow
- During development, use
TypeLoadMode.Dynamicso types are always regenerated as you change configuration. - Before deploying, run
dotnet run -- codegen writeto persist the generated source files. - In production, switch to
TypeLoadMode.Staticfor the fastest startup. The pre-built types load directly from the compiled assembly with no Roslyn overhead.
Environment-Based Configuration
A common pattern is to select the TypeLoadMode based on the hosting environment:
var rules = new GenerationRules("MyApp.Generated")
{
TypeLoadMode = builder.Environment.IsDevelopment()
? TypeLoadMode.Dynamic
: TypeLoadMode.Static
};This gives you runtime generation during development and pre-built types in production without any manual switching.
