![](/img/trans.png)
[英]Bad Gateway (502) error while calling locahost .Net Core API from another locahost .Net Core API
[英]Why does my .NET Core Web API (deployed on AWS Lambda) return a 502 Bad Gateway error?
我是.NET Core,Entity Framework,AWS以及這里幾乎所有內容的新手(請發表評論,並讓我知道是否需要在帖子中添加任何內容)。
我要嘗試的是使用AWS Lambda部署.NET Core Web API。 經過測試,我的API似乎在本地運行(我有一個Angular前端應用程序,通過它我可以成功執行所有API)。 但是,當我使用AWS進行部署並在Postman上運行一些API時,會出現“ 502 Bad Gateway”錯誤。
我的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;}
}
}
我的DbContext文件如下所示:
public class DatabaseContext:DbContext {
public DatabaseContext(DbContextOptions<DatabaseContext> options):base(options) {
}
public DbSet<Student> Students {get;set;}
}
}
我的控制器中有兩種方法:
[HttpGet("temp")]
public ActionResult<List<String>> GetTemp() {
return Ok("here");
}
[HttpGet]
public ActionResult<IEnumerable<Student>> GetStudent()
{
return Ok(_context.Students);
}
其中第一個工作順利(如預期)。 但是,當我嘗試返回數據庫中的學生列表時,似乎遇到了問題。
我也嘗試在本地使用第二個API,並收到了相應的響應:
[
{
"StudentId": 1,
"StudentFirstName": "John",
"StudentLastName": "Doe"
}
]
我還檢查了AWS CloudWatch Logs,其中顯示了以下內容:
[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仍然有效。 但是,在使用serverless deploy -v
部署它之后,我仍然面臨上一個問題。 按照建議,我嘗試修改數據庫的權限,盡管我不確定自己在這里是否做對了。 我所做的是:
這些似乎都沒有解決我的問題。
您可能使用了與本地Db中用於測試的連接字符串相同的字符串,因此應將其更改為使用api可通過aws訪問的某些數據庫。 如果是這種情況,那么您的api會嘗試訪問本地數據庫(顯然不會找到),這將解釋超時問題。
為了對其進行修復,您可能還應該將數據庫部署到AWS上,並將項目的連接字符串更改為AWS內部該數據庫的連接字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.