【问题标题】:Moving .Net Core Web-API to AWS Web Api Gateway将 .Net Core Web-API 迁移到 AWS Web Api 网关
【发布时间】:2019-12-17 08:05:59
【问题描述】:

我有一个使用 .Net Core 开发的 Web-API。它的端点很少( GET / POST )。要求是将其移至 AWS API-Gateway。该 Web-API 是使用分层架构构建的,它有一个与 Db 层对话的业务层,该 Db 层有一些实体框架存储库(后端数据库 Postgres)。现在,我已将我的解决方案重新创建为 AWS 无服务器解决方案(使用 AWS Toolkit for Visual Studio 附带的模板项目之一)。

问题是如何使我的 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

    }
}

【问题讨论】:

    标签: amazon-web-services aws-api-gateway asp.net-core-webapi


    【解决方案1】:

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

    对于您的 serverless.template 文件,检查 Resources > AspNetCoreFunction > Handler 属性。它应该具有这种格式

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

    我还必须添加这些以使我的 API 在 AWS Gateway 上工作到 serverless.template

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

    【讨论】:

      【解决方案2】:

      您可以使用 API Gateway 代理您的请求,然后使用 AspNetCoreServer 将 API Gateway 请求转换为 ASP.NET Core 请求并转换传出响应,以便 API Gateway 能够理解它们。我写了a detailed article 关于如何实现这一点并使用 AWS CDK 进行部署。

      【讨论】:

        猜你喜欢
        • 2020-01-26
        • 1970-01-01
        • 2018-01-28
        • 2021-06-23
        • 2018-09-03
        • 2022-01-25
        • 2020-07-24
        • 2018-05-08
        • 2017-03-28
        相关资源
        最近更新 更多