将 .Net Core Web-API 迁移到 AWS Web API Gateway



我有一个用.Net开发的Web-API,Core.It 端点很少(GET/POST(。要求是将其移动到 AWS API-Gateway。该Web-API是使用分层架构构建的,它有一个与Db层通信的业务层,该层获得了一些实体框架存储库(后端数据库Postgres(。现在,我已经将我的解决方案重新创建为 AWS 无服务器解决方案(使用适用于 Visual Studio 的 AWS 工具包附带的模板项目之一(。

问题是如何使我的 Web API 方法 AWS API Gatway 启用?我尝试按原样将我的 web-api 发布到 AWS,但它在 api 网关中创建了一个空白 api(Visual Studio 说已成功发布(,这意味着由于某些原因,Api-Gateway 无法在我的解决方案中识别我的终端节点,我认为原因是我不知道如何正确配置它们并使它们启用 AWS-API 网关......

第二个问题是 模型绑定如何在 AWS API -GATEWAY 中工作。我应该使用映射模板来实现模型绑定还是内置的 .net Core Web API 模型绑定将起作用并且足够?

以下是开发并需要部署到 AWS-API-Gateway 的示例 Web API

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace TestApi
{
[Route("api/testapi")]
public class TestApiController : Controller
{
private ITestApiManager testApiManager;
public TestApiController(ITestApiManager testApiManager)
{
this.testApiManager = testApiManager;
}

// GET: api/testapi/data/D-001
[HttpGet]
[Route("data/{param}")]
public IActionResult SomeMethod(string param)
{
// This method access busines layer which calls data access layer to get the data from postgress database using entity framework
}

// There are some more similar GET and POST methods in this api as well
}
}

好的,我正在回答我自己的问题,以防其他人正在寻找相同的信息。我的终端节点在 API 网关上不可用的原因,我的 lambda 处理程序不是完全限定的,我必须在 serverless.template 文件中配置 Proxy+ 部分。

对于无服务器模板文件,请检查"资源> AspNetCoreFunction>处理程序"属性。它应该有这种格式

"Handler": "<your-web-api-project-name>::<namespace-for-your-lambda>.<lambda-class-name>::FunctionHandlerAsync"

我还必须添加这些内容,以使我的 API 在 AWS 网关上工作到无服务器模板中

"Events": {
"ProxyResource": {
"Type": "Api",
"Properties": {
"Path": "/{proxy+}",
"Method": "ANY"
}
},
"RootResource": {
"Type": "Api",
"Properties": {
"Path": "/",
"Method": "ANY"
}
}

您可以使用 API 网关代理您的请求,然后使用 AspNetCoreServer 将 API 网关请求转换为 ASP.NET 核心请求并转换传出响应,以便 API 网关能够理解它们。我写了一篇关于如何使用 AWS CDK 实现此目的和部署的详细文章。

最新更新