skip to Main Content

I created a new .NET 6 WebAPI solution, with the default WeatherForecastController class. The only thing I changed in the project is to install Amazon.AspNetCoreLambda.Hosting, and add the following line in my Program.cs file:

builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi);

I built the solution, zipped the files, and deployed it to an AWS Lambda. The error I’m getting in the logs is:

System.MissingMethodException: Cannot dynamically create an instance of type 'Example.Controllers.WeatherForecastController'. Reason: No parameterless constructor defined.

The controller is unchanged and looks like this:

using Microsoft.AspNetCore.Mvc;

namespace Example.Controllers;

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
    private static readonly string[] Summaries = new[]
    {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

    private readonly ILogger<WeatherForecastController> _logger;

    public WeatherForecastController(ILogger<WeatherForecastController> logger)
    {
        _logger = logger;
    }

    [HttpGet(Name = "GetWeatherForecast")]
    public IEnumerable<WeatherForecast> Get()
    {
        return Enumerable.Range(1, 5)
            .Select(
                index => new WeatherForecast
                {
                    Date = DateTime.Now.AddDays(index),
                    TemperatureC = Random.Shared.Next(-20, 55),
                    Summary = Summaries[Random.Shared.Next(Summaries.Length)]
                })
            .ToArray();
    }
}

The Program.cs file looks like this:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi);

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

The AWS Lambda is set up with the following configuration:

  • Hander: Example::Example.Controllers.WeatherForecastController::Get
  • Runtime: dotnet6

Runtime: dotnet6

I can’t see what step I’m missing as I’ve been following tutorials online and can’t get it to work.

What am I missing?

2

Answers


  1. Your error message states that there is no parameterless constructor defined. So let’s create one! Here is an example:

    public WeatherForecastController()
    {
        // This constructor can be empty or initialize any required fields.
    }
    

    When using dependency injection in ASP.NET Core, the framework requires a parameterless constructor to instantiate the controller. In your code, you only have a constructor that takes an ILogger parameter.

    You can instantiate a parameterless constructor in this controller while still setting up that logger no problem.

    Add this line under the .AddControllers() line:

    builder.Services.AddLogging();
    

    This should take care of your error! Hope this helps.

    Login or Signup to reply.
  2. You specified the handler name as Example::Example.Controllers.WeatherForecastController::Get, which points to the GET endpoint method in the weather controller. However, to serve the WEB API, you must let the lambda function execute the Program.cs file first so that all the necessary services will be properly configured.

    Therefore, to host a .NET Core Web API in the lambda function, you have to set the assembly name of your application in the handler section. In your case, it should be Example.

    p.s. Based on your controller namespace, I assume that the assembly name of your application is Example. However, if you use a different assembly name, make sure to use that name in the handler section instead.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search