[英]ASP.NET Web API works locally but returning SqlException error on AWS Lambda
我正在尝试在 aws lambda 上托管 Web API。 API 连接到 AWS RDS 上的数据库。 数据库确实正确存储和提供数据。 问题是,当我在本地运行 API 时,它确实从数据库中正确返回了数据,但是在将其部署到 lambda 之后,它返回了这个错误:
错误类型:SqlException
它没有进一步解释错误,我是 AWS 新手,所以我对如何解决这个错误一无所知,我尝试在 AWS Api Gateway 上运行它,我得到了:
响应标头:
{ "Content-Type":[""],"ErrorType":["SqlException"],"X-Amzn-Trace-Id":["Root=1-62d2052c-c1250f296445a5f4aa4daded;Sampled=0"]}
日志:
Execution log for request 9a72a4d9-d4a0-4a54-913a-4bfc8e0e8ef6
Sat Jul 16 00:24:12 UTC 2022 : Starting execution for request: 9a72a4d9-d4a0-4a54-913a-4bfc8e0e8ef6
Sat Jul 16 00:24:12 UTC 2022 : HTTP Method: GET, Resource Path: /api/Tutors
Sat Jul 16 00:24:12 UTC 2022 : Method request path: {proxy=api/Tutors}
Sat Jul 16 00:24:12 UTC 2022 : Method request query string: {}
Sat Jul 16 00:24:12 UTC 2022 : Method request headers: {}
Sat Jul 16 00:24:12 UTC 2022 : Method request body before transformations:
Sat Jul 16 00:24:12 UTC 2022 : Endpoint request URI: https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:204758858648:function:TutorMeAPI-AspNetCoreFunction-feH0JQ7qc1x8/invocations
Sat Jul 16 00:24:12 UTC 2022 : Endpoint request headers: {X-Amz-Date=20220716T002412Z, x-amzn-apigateway-api-id=ls9fcxi4k1, Accept=application/json, User-Agent=AmazonAPIGateway_ls9fcxi4k1, Host=lambda.us-east-1.amazonaws.com, X-Amz-Content-Sha256=cc12d58899f939a039dd7463ba42b4d9f8e4646a3e0e42e4cce39c3b1649c782, X-Amzn-Trace-Id=Root=1-62d2052c-c1250f296445a5f4aa4daded, x-amzn-lambda-integration-tag=9a72a4d9-d4a0-4a54-913a-4bfc8e0e8ef6, Authorization=*********************************************************************************************************************************************************************************************************************************************************************************************************************************************9bb3fc, X-Amz-Source-Arn=arn:aws:execute-api:us-east-1:204758858648:ls9fcxi4k1/test-invoke-stage/GET/{proxy+}, X-Amz-Security-Token=IQoJb3JpZ2luX2VjEHgaCXVzLWVhc3QtMSJGMEQCICsQ6ZsgSb71mpvgkJSp932NXHuR0pli4knXT6Sbs+prAiBtOsSbcS8pwTB6eF56E/ARbK5e7Jf8qMiqKYwIdg8D5SrcBAix/ [TRUNCATED]
Sat Jul 16 00:24:12 UTC 2022 : Endpoint request body after transformations: {"resource":"/{proxy+}","path":"/api/Tutors","httpMethod":"GET","headers":null,"multiValueHeaders":null,"queryStringParameters":null,"multiValueQueryStringParameters":null,"pathParameters":{"proxy":"api/Tutors"},"stageVariables":null,"requestContext":{"resourceId":"wqoa6e","resourcePath":"/{proxy+}","httpMethod":"GET","extendedRequestId":"VVW-_GJ3IAMFqDQ=","requestTime":"16/Jul/2022:00:24:12 +0000","path":"/{proxy+}","accountId":"204758858648","protocol":"HTTP/1.1","stage":"test-invoke-stage","domainPrefix":"testPrefix","requestTimeEpoch":1657931052514,"requestId":"9a72a4d9-d4a0-4a54-913a-4bfc8e0e8ef6","identity":{"cognitoIdentityPoolId":null,"cognitoIdentityId":null,"apiKey":"test-invoke-api-key","principalOrgId":null,"cognitoAuthenticationType":null,"userArn":"arn:aws:iam::204758858648:root","apiKeyId":"test-invoke-api-key-id","userAgent":"aws-internal/3 aws-sdk-java/1.12.239 Linux/5.4.196-119.356.amzn2int.x86_64 OpenJDK_64-Bit_Server_VM/25.332-b08 java/1.8.0_332 [TRUNCATED]
Sat Jul 16 00:24:12 UTC 2022 : Sending request to https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:204758858648:function:TutorMeAPI-AspNetCoreFunction-feH0JQ7qc1x8/invocations
Sat Jul 16 00:24:33 UTC 2022 : Received response. Status: 200, Integration latency: 21292 ms
Sat Jul 16 00:24:33 UTC 2022 : Endpoint response headers: {Date=Sat, 16 Jul 2022 00:24:33 GMT, Content-Type=application/json, Content-Length=138, Connection=keep-alive, x-amzn-RequestId=60dad212-1480-4555-a950-b18629fac5cc, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-62d2052c-c1250f296445a5f4aa4daded;sampled=0}
Sat Jul 16 00:24:33 UTC 2022 : Endpoint response body before transformations: {"statusCode":500,"headers":{},"multiValueHeaders":{"Content-Type":[null],"ErrorType":["SqlException"]},"body":"","isBase64Encoded":false}
Sat Jul 16 00:24:33 UTC 2022 : Method response body after transformations:
Sat Jul 16 00:24:33 UTC 2022 : Method response headers: {Content-Type=null, ErrorType=SqlException, X-Amzn-Trace-Id=Root=1-62d2052c-c1250f296445a5f4aa4daded;Sampled=0}
Sat Jul 16 00:24:33 UTC 2022 : Successfully completed execution
Sat Jul 16 00:24:33 UTC 2022 : Method completed with status: 500
我还注意到它确实返回了自动生成的默认控制器的数据,但对于我连接到数据库的控制器,它们返回错误
这是我的控制器的一个例子:
namespace WebAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class GroupsController : ControllerBase
{
private readonly WebAPIContext _context;
public GroupsController(WebAPIContext context)
{
_context = context;
}
// GET: api/Groups
[HttpGet]
public async Task<ActionResult<IEnumerable<Group>>> GetGroup()
{
if (_context.Group == null)
{
return NotFound();
}
return await _context.Group.ToListAsync();
}
// GET: api/Groups/5
[HttpGet("{id}")]
public async Task<ActionResult<Group>> GetGroup(Guid id)
{
if (_context.Group == null)
{
return NotFound();
}
var @group = await _context.Group.FindAsync(id);
if (@group == null)
{
return NotFound();
}
return @group;
}
}
}
这在本地有效,没有问题。 lambda 可能有什么问题? 感谢您的时间
Lambda 是一个运行方法/函数的无服务器计算环境。 您正在为您的 ASP.NET Web API 寻找托管解决方案。 查看将您的 ASP.NET Web API 部署到 Elastic Beanstalk。 托管使用 IIS 的 Web 应用程序的任何人都可以使用适用于 .NET 的 Elastic Beanstalk。
详情在这里:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.