C# 为什么我的.NET核心Web API(部署在AWS Lambda上)返回502错误网关?
我对.NETCore、EntityFramework、AWS以及这里的几乎所有内容都是新手(如果需要在文章中添加任何内容,请发表评论并告诉我) 我想做的是用AWS Lambda部署一个.NET核心Web API。我的API似乎在本地工作,因为我测试了它(我有一个Angular前端应用程序,通过它我可以成功地执行所有API)。然而,当我使用AWS部署并在Postman上运行一些API时,会出现“502坏网关”错误 我的C# 为什么我的.NET核心Web API(部署在AWS Lambda上)返回502错误网关?,c#,entity-framework,aws-lambda,asp.net-core-webapi,rds,C#,Entity Framework,Aws Lambda,Asp.net Core Webapi,Rds,我对.NETCore、EntityFramework、AWS以及这里的几乎所有内容都是新手(如果需要在文章中添加任何内容,请发表评论并告诉我) 我想做的是用AWS Lambda部署一个.NET核心Web API。我的API似乎在本地工作,因为我测试了它(我有一个Angular前端应用程序,通过它我可以成功地执行所有API)。然而,当我使用AWS部署并在Postman上运行一些API时,会出现“502坏网关”错误 我的serverless.yml文件如下所示: service: angularaw
serverless.yml
文件如下所示:
service: angularawardwebapi
provider:
name: aws
runtime: dotnetcore2.1
region: us-east-1
package:
artifact: bin/release/netcoreapp2.1/deploy-package.zip
functions:
api:
handler: angularawardwebapi::angularawardwebapi.LambdaEntryPoint::FunctionHandlerAsync
events:
- http:
path: /{proxy+}
method: ANY
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace angularawardwebapi.Models {
public class Student {
[Key]
public int StudentId{get;set;}
[Required]
[Column(TypeName="nvarchar(50)")]
public string StudentFirstName{get;set;}
[Required]
[Column(TypeName="varchar(50)")]
public string StudentLastName{get;set;}
}
}
[Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting GET https://[myurl]/dev/api/Student/
[Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Route matched with {action = "GetStudent", controller = "Student"}. Executing action WebApi.Controllers.StudentController.GetStudent ([myprojectname])
[Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executing action method WebApi.Controllers.StudentController.GetStudent ([myprojectname]) - Validation state: Valid
[Information] Microsoft.EntityFrameworkCore.Infrastructure: Entity Framework Core 2.1.8-servicing-32085 initialized 'DatabaseContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
END RequestId: f57417d3-4075-4680-a78d-e90e2c710760
REPORT RequestId: f57417d3-4075-4680-a78d-e90e2c710760 Duration: 6006.80 ms Billed Duration: 6000 ms Memory Size: 1024 MB Max Memory Used: 141 MB
2019-06-06T14:19:41.116Z f57417d3-4075-4680-a78d-e90e2c710760 Task timed out after 6.01 seconds
我的模型类如下所示:
service: angularawardwebapi
provider:
name: aws
runtime: dotnetcore2.1
region: us-east-1
package:
artifact: bin/release/netcoreapp2.1/deploy-package.zip
functions:
api:
handler: angularawardwebapi::angularawardwebapi.LambdaEntryPoint::FunctionHandlerAsync
events:
- http:
path: /{proxy+}
method: ANY
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace angularawardwebapi.Models {
public class Student {
[Key]
public int StudentId{get;set;}
[Required]
[Column(TypeName="nvarchar(50)")]
public string StudentFirstName{get;set;}
[Required]
[Column(TypeName="varchar(50)")]
public string StudentLastName{get;set;}
}
}
[Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting GET https://[myurl]/dev/api/Student/
[Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Route matched with {action = "GetStudent", controller = "Student"}. Executing action WebApi.Controllers.StudentController.GetStudent ([myprojectname])
[Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executing action method WebApi.Controllers.StudentController.GetStudent ([myprojectname]) - Validation state: Valid
[Information] Microsoft.EntityFrameworkCore.Infrastructure: Entity Framework Core 2.1.8-servicing-32085 initialized 'DatabaseContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
END RequestId: f57417d3-4075-4680-a78d-e90e2c710760
REPORT RequestId: f57417d3-4075-4680-a78d-e90e2c710760 Duration: 6006.80 ms Billed Duration: 6000 ms Memory Size: 1024 MB Max Memory Used: 141 MB
2019-06-06T14:19:41.116Z f57417d3-4075-4680-a78d-e90e2c710760 Task timed out after 6.01 seconds
我的DbContext文件如下所示:
public class DatabaseContext:DbContext {
public DatabaseContext(DbContextOptions<DatabaseContext> options):base(options) {
}
public DbSet<Student> Students {get;set;}
}
}
我还检查了AWS CloudWatch日志,其中显示了如下内容:
service: angularawardwebapi
provider:
name: aws
runtime: dotnetcore2.1
region: us-east-1
package:
artifact: bin/release/netcoreapp2.1/deploy-package.zip
functions:
api:
handler: angularawardwebapi::angularawardwebapi.LambdaEntryPoint::FunctionHandlerAsync
events:
- http:
path: /{proxy+}
method: ANY
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace angularawardwebapi.Models {
public class Student {
[Key]
public int StudentId{get;set;}
[Required]
[Column(TypeName="nvarchar(50)")]
public string StudentFirstName{get;set;}
[Required]
[Column(TypeName="varchar(50)")]
public string StudentLastName{get;set;}
}
}
[Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting GET https://[myurl]/dev/api/Student/
[Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Route matched with {action = "GetStudent", controller = "Student"}. Executing action WebApi.Controllers.StudentController.GetStudent ([myprojectname])
[Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executing action method WebApi.Controllers.StudentController.GetStudent ([myprojectname]) - Validation state: Valid
[Information] Microsoft.EntityFrameworkCore.Infrastructure: Entity Framework Core 2.1.8-servicing-32085 initialized 'DatabaseContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
END RequestId: f57417d3-4075-4680-a78d-e90e2c710760
REPORT RequestId: f57417d3-4075-4680-a78d-e90e2c710760 Duration: 6006.80 ms Billed Duration: 6000 ms Memory Size: 1024 MB Max Memory Used: 141 MB
2019-06-06T14:19:41.116Z f57417d3-4075-4680-a78d-e90e2c710760 Task timed out after 6.01 seconds
当我在本地运行API时,不会看到这个超时问题,因为我会立即得到正确的响应(如上所述)
更新 所以我现在有了一个RDS数据库,我可以成功地连接到它,并在SSMS中使用它。在本地运行API也仍然有效。但是,在我使用
无服务器部署-v
部署它之后,我仍然面临前面的问题。正如建议的那样,我尝试修改DB的权限,尽管我也不确定我在那里做的事情是否正确。我所做的是:
- 进入SSMS并通过右键单击服务器、进入权限、选择我的登录名(在配置RDS时设置)修改数据库权限,并授予“更改任何数据库”选项。然后我转到数据库本身并修改了一些权限(删除、插入和更新)
- 我还启用了Windows防火墙规则中的Windows Management Instrumentation(WMI)
这两个似乎都没有解决我的问题 您可能正在使用与在本地数据库中测试时使用的连接字符串相同的连接字符串,您应该将其更改为使用api可以从aws访问的数据库。 如果是这种情况,那么您的api正在尝试访问本地db(显然它找不到),这将解释超时问题
为了修复此问题,您可能还应该将数据库部署到AWS,并将项目的连接字符串更改为AWS中该数据库的连接字符串。您可能使用的连接字符串与您在本地数据库中测试时使用的连接字符串相同,您应该将其更改为使用api可以从aws访问的数据库。 如果是这种情况,那么您的api正在尝试访问本地db(显然它找不到),这将解释超时问题
为了解决此问题,您可能还应该将数据库部署到AWS,并将项目的连接字符串更改为AWS内部该数据库的连接字符串。您的生产数据库与本地数据库中有多少学生?现在,这两种方法我都使用本地数据库。是什么让我产生了这样的问题:数据库似乎不是完全不可访问的,否则错误就不同了。超时闻起来好像数据库需要太多的时间来传输结果——例如,如果你喜欢大量的点击,情况就是这样。如何托管数据库?在AWS或其他完全不同的地方?谢谢!我想我必须先用Elastic Beanstalk设置我的DB实例和所有东西,对吗?然后按照我设置本地数据库的相同步骤进行操作。@Fildor感谢您先前的帮助。我仍然遇到这个问题,但原因似乎不同。根据下面我与carlos chourio的对话,我现在已经创建了一个RDS数据库,并更新了我的.NET代码以连接到它。但是,我仍然遇到超时问题。与本地数据库相比,您的生产数据库中有多少学生?现在,我将本地数据库用于这两个方面。是什么让我产生了这样的问题:数据库似乎并非完全无法访问,否则错误会有所不同。超时闻起来好像数据库需要太多的时间来传输结果——例如,如果你喜欢大量的点击,情况就是这样。如何托管数据库?在AWS或其他完全不同的地方?谢谢!我想我必须先用Elastic Beanstalk设置我的DB实例和所有东西,对吗?然后按照我设置本地数据库的相同步骤进行操作。@Fildor感谢您先前的帮助。我仍然遇到这个问题,但原因似乎不同。根据下面我与carlos chourio的对话,我现在已经创建了一个RDS数据库,并更新了我的.NET代码以连接到它。但是,我仍然遇到超时问题。因此问题仍然存在。我现在已经建立了一个RDS数据库,并修改了连接字符串以连接到该数据库。在本地使用它会产生正确的结果,因此我已经正确地配置了它。但是,一旦我使用
serverless deploy-v
部署它并尝试相同的方法,它会导致与以前相同的超时。您是否检查过允许通过api连接数据库?您可能需要设置一些权限才能连接。抱歉,回复太晚。我该怎么设置呢?我已经在项目中设置了IAM令牌,但这似乎不是唯一的问题。我不认为我可以帮助你,我已经回答了,因为我在将API部署到Azure时遇到了同样的问题,但我不熟悉AWS。在azure中,您必须转到防火墙设置并启用允许访问azure服务选项。AWS应具有类似的配置。您还可以向防火墙添加一条规则,允许您从web api的ip进行连接,并检查其是否工作,以便问题持续存在。我现在已经建立了一个RDS数据库,并修改了连接字符串以连接到该数据库。在本地使用它会产生正确的结果,因此我已经正确地配置了它。但是,一旦我使用serverless deploy-v
部署它并尝试相同的方法,它会在相同的时间