aws-lambda-dotnet: BadImageFormatException when using serverless.AspNetCoreWebApp template with Mock Lambda Test Tool
Description
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
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 19 (18 by maintainers)
@coultonluke I understand the concern, but the error is thrown by ASP.NET runtime unable to find assemblies and the possible reason might be the design of Lambda Test Tool where it loads project assemblies at runtime. There is some work being done to make test tool better, hence it would be better to open a new feature request to get it reviewed by the team. We would look forward for the feature request.
Thanks, Ashish
@coultonluke Thanks for your response. As mentioned earlier in https://github.com/aws/aws-lambda-dotnet/issues/1075#issuecomment-1031875085, the Lambda Test Tool is suitable for testing simple Lambda functions and API requests. From your error stack trace, looks like ASP.NET Razor engine is unable to find the assembly, perhaps because the current directory is the Lambda tools installation directory. We could try setting the current directory to project directory, but unsure if it would work due to the limitation mentioned in https://github.com/aws/aws-lambda-dotnet/issues/1482#issuecomment-2369635141, where we cannot create more than one AppDomain in .NET Core and later.
I would advise you to open a new feature request mentioning all the details which could be reviewed with the team.
Thanks, Ashish
@coultonluke Good morning. I was reviewing old open issues and came across this one. The issue doesn’t appear to be reproducible using latest .NET 8 AWS serverless template. Below is the code. .csproj
Startup.cs
Controllers\HomeController.cs
Debugging using latest version
0.15.2of Mock Lambda test tool and input{ "httpMethod": "GET", "path": "/Home" }appears to work fine:Please verify the same at your end and confirm if this issue could be closed.
Thanks, Ashish
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.
Hi @coultonluke,
Good morning.
I was able to reproduce the issue in Visual Studio (not Jetbrains Rider) by:
serverless.AspNetCoreWebAppprojectMicrosoft.AspNetCore.Mvc.Razor.RuntimeCompilationversion3.1.22(since this is the latest version compatible with NetCoreApp31)endpoints.MapControllers();)HomeController:I manually tried adding reference to
Microsoft.CSharp 4.0.0NuGet package, however, it gave the warningNU1701 | Package 'Microsoft.CSharp 4.0.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETCoreApp,Version=v3.1'. This package may not be fully compatible with your project..Kindly note that Mock Lambda Test Tool uses reflection to load all the dependent assemblies. I’m unsure if
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilationtries to loadMicrosoft.CSharp 4.0.0at runtime in a different context.Thanks, Ashish