简体   繁体   English

有没有人有幸让 .NET 6 REST API 项目在 AWS Z04A7DA3C5B05CAD85DA1EEBB923 中工作?

[英]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我的程序集

See the example here in aws在 aws 中查看示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 AWS Lambda 中获取“内部流位置意外更改” - Getting "The inner stream position has changed unexpectedly" in AWS Lambda AmazonS3Client在AWS Lambda S3项目中不起作用 - AmazonS3Client does not work in AWS Lambda S3 Project AWS .net Core Lambda项目中的全局异常处理程序? - Global exception handler in AWS .net Core Lambda project? 是否有人在C#AWS Lambda函数或AWS Linux EC2实例中成功运行SQL Server连接器? - Has anyone successfully run a SQL Server connector within a C# AWS Lambda function, or AWS Linux EC2 instance? 有没有人在 C# 中遇到过这个 Resharper 2019 空间问题? - Has anyone had this Resharper 2019 space problem in C#? 在 lambda 查询中使用“.Any()”会在 .net 核心 EF 项目中引发错误 - Using “.Any()” in lambda query throws error in .net core EF project 我可以在 AWS Lambda 上使用 .Net Framework 4.5 部署 RESTful API 吗? - Can I deploy a RESTful API with .Net Framework 4.5 on AWS Lambda? 无法将 AWS Lambda 中的 .net 核心 API 连接到 RDS - Cannot connect .net core API in AWS Lambda to RDS AWS Lambda使用.NET Core和Google表格API - AWS Lambda using .NET Core and Google Sheets API 适用于.Net微服务的AWS Lambda - AWS Lambda for .Net Microservices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM