BadImageFormatException when using serverless.AspNetCoreWebApp template with Mock Lambda Test Tool
See original GitHub issueDescription
Can’t make request to lambda using Mock Lambda Test Tool when project has reference to Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation Nuget package.
Reproduction Steps
-
Install templates:
dotnet new -i "Amazon.Lambda.Templates::*" -
Create new project:
dotnet new serverless.AspNetCoreWebApp -
Add Nuget reference to
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation -
Edit
ConfigureServicesin startup to enable razor pages to be loaded:public void ConfigureServices(IServiceCollection services) { services.AddControllers(); var builder = services.AddRazorPages(); builder.AddRazorRuntimeCompilation(); } -
Configure debug configuration for mock test tool as shown in the readme for Rider: Executable and executable path: “<Configured to home directory>\.dotnet\tools\.store\amazon.lambda.testtool-3.1\0.11.4\amazon.lambda.testtool-3.1\0.11.4\tools\netcoreapp3.1\any\Amazon.Lambda.TestTool.BlazorTester.dll”
-
Create a controller and use attribute routing
-
Debug application and send in request using Mock Test Tool blazor interface
Request I’m using: { "httpMethod": "GET", "path": "/api/Home" }
Without Nuget reference:

With Nuget reference:

Logs
Environment
- Build Version: Amazon.Lambda.AspNetCoreServer 7.0.1
- OS Info: Windows 10
- Build Environment: Jetbrains Rider
- Targeted .NET Platform: netcoreapp3.1
Resolution
- Remove reference to Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation (no use for local debugging)
A similar issue also happens when I reference Microsoft.AspNetCore.Mvc.WebApiCompatShim too, but the error is that it can’t find System.Json.
This is a 🐛 bug-report
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (10 by maintainers)

Top Related StackOverflow Question
This issues strikes us as well. We are creating a lambda function for building emails and for that, we are using Razor as a templating engine. Unfortunately, this does not work well with the test tool as we getting the same exception as described in the issue. The fix would be deeply appreciated!
@coultonluke Removing that reference resolves the issue. I would have team look at this issue to see if something could be done, before deciding to close the issue.