aws-lambda-dotnet icon indicating copy to clipboard operation
aws-lambda-dotnet copied to clipboard

BadImageFormatException when using serverless.AspNetCoreWebApp template with Mock Lambda Test Tool

Open coultonluke opened this issue 3 years ago • 12 comments

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

  1. Install templates: dotnet new -i "Amazon.Lambda.Templates::*"

  2. Create new project: dotnet new serverless.AspNetCoreWebApp

  3. Add Nuget reference to Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation

  4. Edit ConfigureServices in startup to enable razor pages to be loaded:

     public void ConfigureServices(IServiceCollection services)
     {
         services.AddControllers();
         var builder = services.AddRazorPages();
         builder.AddRazorRuntimeCompilation();
     }
    
  5. 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"

  6. Create a controller and use attribute routing

  7. Debug application and send in request using Mock Test Tool blazor interface

Request I'm using: { "httpMethod": "GET", "path": "/api/Home" }

Without Nuget reference: image

With Nuget reference: image

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: bug-report

coultonluke avatar Feb 07 '22 12:02 coultonluke

Hi @coultonluke,

Good morning.

I was able to reproduce the issue in Visual Studio (not Jetbrains Rider) by:

  • Creating serverless.AspNetCoreWebApp project
  • Adding reference to Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation version 3.1.22 (since this is the latest version compatible with NetCoreApp31)
  • Enabling controller endpoints (endpoints.MapControllers();)
  • Adding a minimal HomeController:
[Route("api/[controller]")]
public class HomeController : Controller
{
    [HttpGet]
    public string Index()
    {
        return "Hello";
    }
}
  • Debug using Mock Lambda Test Tool.

I manually tried adding reference to Microsoft.CSharp 4.0.0 NuGet package, however, it gave the warning NU1701 | 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.RuntimeCompilation tries to load Microsoft.CSharp 4.0.0 at runtime in a different context.

Thanks, Ashish

ashishdhingra avatar Feb 07 '22 18:02 ashishdhingra

Thank you for looking into this for me. I appreciate it. I'm trying to get going with the Lambda Test Tool using an existing project, so do you think I'll just have to remove that reference and it's functionality in order to use the tool?

coultonluke avatar Feb 07 '22 19:02 coultonluke

Thank you for looking into this for me. I appreciate it. I'm trying to get going with the Lambda Test Tool using an existing project, so do you think I'll just have to remove that reference and it's functionality in order to use the tool?

@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.

ashishdhingra avatar Feb 07 '22 19:02 ashishdhingra

Are razor views generally supported when using the Lambda Test Tool even without the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation reference?

I've created a basic project in the same way and the views work completely fine when running as a normal project, but when I use the Lambda Test Tool the Views cannot be found. I've tried setting a ContentRootPath and also setting them to copy but to no avail.

Repository ==> https://github.com/coultonluke/riderlambdatest.git

image

I thought I knew a thing or two about .NET Core MVC as I use it every day, but this stuff is leaving me stumped cause even basic stuff isn't working. Perhaps it's my own fault for sticking with 3.1 for soo long 😂

coultonluke avatar Feb 07 '22 19:02 coultonluke

{ "httpMethod": "GET", "path": "/api/Home" }

@coultonluke I'm unsure why you are using Mock Lambda Test tool to test the Razor pages. Mock Lambda Test tool is ideal for testing simple Lambda functions and API requests. Example Requests dropdown in the tool provides a good reference for the JSON to be used for common requests. Normal razor pages could be tested in IIS/IIS Express environment.

ashishdhingra avatar Feb 07 '22 20:02 ashishdhingra

Its because I'm wanting to host my existing website MVC application in AWS Lambda. When I run the lambda in AWS Lambda it returns views just fine, but I am struggling to replicate it locally using the test tool. I want to be able to debug any issues that are faced by running the LambdaEntryPoint as would be running in the real environment.

I was just looking at the code base and I was going to suggest adding an example ALB request to the list of common request types. The Amazon.Lambda.AspNetCoreServer supports ApplicationLoadBalancerFunction but there is no test request for it from what I can see? I was thinking this might be a useful pull request to add a request like the example request provided in the documentation.

I see what you're saying, but even with an API endpoint you could just use normal debugging and postman or whatever to test requests, you don't need to use the Mock Lambda Test Tool, but the test tool is intended to test it and return the response as if it were hosted in lambda?

coultonluke avatar Feb 07 '22 20:02 coultonluke

provided in the documentation

@coultonluke Thanks for your reply. As pointed out earlier, Mock Lambda test tool is not suitable for testing Razor pages; local IIS/IIS Express testing could be used to test Razor pages.

As far as adding an example ALB request to the list of common request types is concerned, it could be a good candidate for feature request. Feel free to test and submit a PR for review if possible, which could then be reviewed by the team.

Also refer Lambda Test Tool Readme for additional details and Known Limitations.

ashishdhingra avatar Feb 07 '22 21:02 ashishdhingra

Thank you again. I will look into posting a pull request.

I must be misunderstanding the need for the tool somewhat I suppose, because it seems like an odd limitation for the tool to care what is returned back from an API request as long as there is something that was returned, whether the body is HTML or JSON should it matter when you're essentially working with a web app?

The important thing for me is to be able to debug it as if it were in the lambda environment, I'm not the only one returning HTML back from a lambda surely 😐. I think the BBC have all of their sites hosted in lambda nowadays.

coultonluke avatar Feb 07 '22 21:02 coultonluke

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Feb 07 '22 21:02 github-actions[bot]

I didn't mean to press close. I have reopened this.

coultonluke avatar Feb 08 '22 08:02 coultonluke

@ashishdhingra After some sleep I think I understand what you're saying now. Are you saying that I can still debug through the code even if I don't have the view coming back in the lambda test tool? If that's the case then I do agree, it's not that important to be able to see the response back in the blazor app really. It just surprised me that it's a limitation. Thanks again

coultonluke avatar Feb 08 '22 12:02 coultonluke

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!

Pankraty avatar May 27 '22 06:05 Pankraty