![](/img/trans.png)
[英]Getting "The inner stream position has changed unexpectedly" in AWS Lambda
[英]Has anyone had any luck getting a .NET 6 REST API project to work in an AWS Lambda?
我有一個非常簡單的 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!" });
}
}
我已經能夠讓 ASP.NET 項目 < 6.0 工作,但是使用 .NET 6 我遇到了問題。 不再有Startup
class; 該功能移至隱式Program
class。 所以在我的LambdaEntryPoint
class (繼承自APIGatewayProxyFunction
)中,我使用Program
作為啟動:
protected override void Init(IWebHostBuilder builder)
{
builder.UseStartup<Program>();
}
從 AWS 控制台手動測試時出現錯誤: Amazon.Lambda.RuntimeSupport.ExceptionHandling.LambdaValidationException: Unable to load assembly.
我相信我的命名是正確的: MyAssembly::MyAssembly.LambdaEntryPoint::FunctionHandlerAsync
我的Program
class 唯一不同的是我必須在底部添加public partial class Program { }
以便單元測試可以找到Program
並運行。
我的測試事件如下所示:
{
"resource": "/Home",
"path": "/Home",
"httpMethod": "GET",
"isBase64Encoded": true
}
它是默認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
您不需要添加 Startup.cs class。 如果您使用 NET6,您唯一需要做的就是在 Program.cs 文件中添加這行代碼。
如果您使用的是 rest API 或 API GATEWAY
builder.Services.AddAWSLambdaHosting(LambdaEventSource.RestApi);
這是如果您將 lambda 與 url 一起使用
builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi);
..在aws中,您只需使用項目名稱更改處理程序即可。 我的程序集
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.