-
Notifications
You must be signed in to change notification settings - Fork 475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BadImageFormatException when using serverless.AspNetCoreWebApp template with Mock Lambda Test Tool #1075
Comments
Hi @coultonluke, Good morning. I was able to reproduce the issue in Visual Studio (not Jetbrains Rider) by:
[Route("api/[controller]")]
public class HomeController : Controller
{
[HttpGet]
public string Index()
{
return "Hello";
}
}
I manually tried adding reference to Kindly note that Mock Lambda Test Tool uses reflection to load all the dependent assemblies. I'm unsure if Thanks, |
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. |
Are razor views generally supported when using the Lambda Test Tool even without the 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 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 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. |
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 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 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. |
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. |
|
I didn't mean to press close. I have reopened this. |
@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 |
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 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. <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
<!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<!-- Generate ready to run images during publishing to improve cold start time. -->
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.0.10" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\images\" />
</ItemGroup>
</Project> Startup.cs namespace AWSServerlessNet8;
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddMvc(o => o.EnableEndpointRouting = false);
var builder = services.AddRazorPages();
builder.AddRazorRuntimeCompilation();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseMvc();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
} Controllers\HomeController.cs
Debugging using latest version Please verify the same at your end and confirm if this issue could be closed. Thanks, |
I have just been looking at this again. I've got it set up from the template with an MVC controller, but returning HTML instead of JSON in the result still results in an error when using the test tool. I want to return HTML, because I am hosting a website in lambda, as previously discussed, it seems like an odd limitation for it not to work just because it's an MVC controller and HTML is returned, when it's just a string returned in the "body" property of the JSON. The body should be able to be any string, whether it's HTML or JSON. Thanks for looking into this. |
Just so I'm clear on it. I can confirm that the inclusion of that Nuget package no longer causes and error when calling an API controller. So that's good! But returning a view response from an MVC controller results in an error. |
@coultonluke Thanks for your response. As mentioned earlier in #1075 (comment), 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 #1482 (comment), 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, |
Sorry to be a pain, I hope this comes across the right way - but surely the point in the test tool is be able to run any endpoint and to see the response as if it were running in the AWS lambda environment? If I return a random string from it, or HTML, the string shouldn't matter, right? When you return an MVC response from a lambda, you'd still want to see the response that the lambda will return. Whether that be headers, a HTML string or base64 etc etc. If it would work in lambda, then it should work in the test tool right? As mentioned I will take your advice and raise another ticket if you don't think it's covered within this one. |
@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, |
Comments on closed issues are hard for our team to see. |
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
ConfigureServices
in startup to enable razor pages to be loaded:Configure debug configuration for mock test tool as shown in the readme for Rider:
Executable and executable path: "\.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
Resolution
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
The text was updated successfully, but these errors were encountered: