[英]Has anyone had any luck getting a .NET 6 REST API project to work in an AWS Lambda?
I have a very simple ASP.NET 6.0 Web API application, with a Home controller with one Get
method returning text:我有一个非常简单的 ASP.NET 6.0 Web API 应用程序,带有一个 Home Get
方法返回文本:
[ApiController]
[Route("[controller]")]
public class HomeController : Controller
{
// GET
[HttpGet]
public IActionResult Get()
{
return Ok(new { message = "Hello, World!" });
}
}
I've been able to get ASP.NET projects < 6.0 to work, but with .NET 6 I'm running into issues.我已经能够让 ASP.NET 项目 < 6.0 工作,但是使用 .NET 6 我遇到了问题。 There's no longer a Startup
class;不再有Startup
class; that functionality moved to the implicit Program
class.该功能移至隐式Program
class。 So in my LambdaEntryPoint
class (which inherits from APIGatewayProxyFunction
) I'm using Program
as the Startup:所以在我的LambdaEntryPoint
class (继承自APIGatewayProxyFunction
)中,我使用Program
作为启动:
protected override void Init(IWebHostBuilder builder)
{
builder.UseStartup<Program>();
}
I'm getting an error when manually testing from the AWS console: Amazon.Lambda.RuntimeSupport.ExceptionHandling.LambdaValidationException: Unable to load assembly.
从 AWS 控制台手动测试时出现错误: Amazon.Lambda.RuntimeSupport.ExceptionHandling.LambdaValidationException: Unable to load assembly.
I believe that my naming is correct: MyAssembly::MyAssembly.LambdaEntryPoint::FunctionHandlerAsync
我相信我的命名是正确的: MyAssembly::MyAssembly.LambdaEntryPoint::FunctionHandlerAsync
The only thing different about my Program
class is that I had to add public partial class Program { }
at the bottom so the unit tests would could find Program
and run.我的Program
class 唯一不同的是我必须在底部添加public partial class Program { }
以便单元测试可以找到Program
并运行。
My Test event looks like this:我的测试事件如下所示:
{
"resource": "/Home",
"path": "/Home",
"httpMethod": "GET",
"isBase64Encoded": true
}
It is a cut down version of the default Amazon API Gateway AWS Proxy
它是默认Amazon API Gateway AWS Proxy
的精简版
The fact that you don't have a Startup class doesn't mean you can't make one, I made a Startup class for my API in .NET 6 and it works fine The fact that you don't have a Startup class doesn't mean you can't make one, I made a Startup class for my API in .NET 6 and it works fine
You do not need to add a Startup.cs class.您不需要添加 Startup.cs class。 The only thing you have to do if you are working with NET6 is in your Program.cs file you have to add this line of code.如果您使用 NET6,您唯一需要做的就是在 Program.cs 文件中添加这行代码。
This is if you are using a rest API or API GATEWAY如果您使用的是 rest API 或 API GATEWAY
builder.Services.AddAWSLambdaHosting(LambdaEventSource.RestApi); builder.Services.AddAWSLambdaHosting(LambdaEventSource.RestApi);
This is if you are using your lambda with a url这是如果您将 lambda 与 url 一起使用
builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi); builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi);
..and in aws you just have to change the handler with the name of your project and that's it. ..在aws中,您只需使用项目名称更改处理程序即可。 MyAssembly我的程序集
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.