gpt4 book ai didi

asp.net-web-api - Net Web API 发布到 Azure API 管理服务后出现 500 错误

转载 作者:行者123 更新时间:2023-12-05 04:48:34 27 4
gpt4 key购买 nike

我正在尝试使用 Microsoft Azure 创建远程 SQL Server 数据库和将连接到该数据库的 Net Web API 服务。我已经使用 Microsoft Azure 成功创建了一个数据库,并已关注 This Tutorial将 Net Web API 导入到 Azure 中的 API 管理服务。我有一个帖子,当我在本地运行它时可以正常运行,通过 Postman 和 SQL Server 进行测试,但是在发布后在 Azure 门户中进行测试时,我收到 500 错误。

我在发布时遇到了一些困难并解决了它:

  • 基于 Stackoverflow 答案 Here
  • 向我的 Web API 项目添加了一个 global.json 文件
  • 将其放入其中:{ "sdk": { "version": "3.1.406", "rollForward": "latestPatch"} }
  • 然---dotnet工具还原

这是测试发布请求后的跟踪:

   api-inspector (0.257 ms)
{
"request": {
"method": "POST",
"url": "https://psiteapi.azure-api.net/api/demo/AzureTest",
"headers": [
{
"name": "sec-ch-ua",
"value": "\" Not;A Brand\";v=\"99\",\"Google Chrome\";v=\"91\",\"Chromium\";v=\"91\""
},
{
"name": "sec-ch-ua-mobile",
"value": "?0"
},
{
"name": "Ocp-Apim-Subscription-Key",
"value": "9fc0d4d53a3a41e89ca863de5b2f4668"
},
{
"name": "Sec-Fetch-Site",
"value": "cross-site"
},
{
"name": "Sec-Fetch-Mode",
"value": "cors"
},
{
"name": "Sec-Fetch-Dest",
"value": "empty"
},
{
"name": "X-Forwarded-For",
"value": "172.58.144.45"
},
{
"name": "Cache-Control",
"value": "no-cache, no-store"
},
{
"name": "Content-Length",
"value": "0"
},
{
"name": "Content-Type",
"value": "text/plain;charset=UTF-8"
},
{
"name": "Accept",
"value": "*/*"
},
{
"name": "Accept-Encoding",
"value": "gzip,deflate,br"
},
{
"name": "Accept-Language",
"value": "en-US,en;q=0.9"
},
{
"name": "Host",
"value": "psiteapi.azure-api.net"
},
{
"name": "Referer",
"value": "https://apimanagement.hosting.portal.azure.net/"
}
]
}
}
api-inspector (0.003 ms)
{
"configuration": {
"api": {
"from": "/",
"to": {
"scheme": "https",
"host": "psiteapi.azurewebsites.net",
"port": 443,
"path": "/",
"queryString": "",
"query": {},
"isDefaultPort": true
},
"version": null,
"revision": "1"
},
"operation": {
"method": "POST",
"uriTemplate": "/api/demo/{str}"
},
"user": "-",
"product": "-"
}
}
cors (0.017 ms)
"Origin header was missing or empty and the request was classified as not cross-domain. CORS policy was not applied."


forward-request (0.095 ms)
{
"message": "Request is being forwarded to the backend service. Timeout set to 300 seconds",
"request": {
"method": "POST",
"url": "https://psiteapi.azurewebsites.net/api/demo/AzureTest",
"headers": [
{
"name": "Host",
"value": "psiteapi.azurewebsites.net"
},
{
"name": "Content-Length",
"value": 0
},
{
"name": "sec-ch-ua",
"value": "\" Not;A Brand\";v=\"99\",\"Google Chrome\";v=\"91\",\"Chromium\";v=\"91\""
},
{
"name": "sec-ch-ua-mobile",
"value": "?0"
},
{
"name": "Ocp-Apim-Subscription-Key",
"value": "9fc0d4d53a3a41e89ca863de5b2f4668"
},
{
"name": "Sec-Fetch-Site",
"value": "cross-site"
},
{
"name": "Sec-Fetch-Mode",
"value": "cors"
},
{
"name": "Sec-Fetch-Dest",
"value": "empty"
},
{
"name": "X-Forwarded-For",
"value": "172.58.144.45,13.91.254.72"
},
{
"name": "Cache-Control",
"value": "no-cache, no-store"
},
{
"name": "Content-Type",
"value": "text/plain;charset=UTF-8"
},
{
"name": "Accept",
"value": "*/*"
},
{
"name": "Accept-Encoding",
"value": "gzip,deflate,br"
},
{
"name": "Accept-Language",
"value": "en-US,en;q=0.9"
},
{
"name": "Referer",
"value": "https://apimanagement.hosting.portal.azure.net/"
}
]
}
}
forward-request (752.347 ms)
{
"response": {
"status": {
"code": 500,
"reason": "Internal Server Error"
},
"headers": [
{
"name": "Set-Cookie",
"value": "ARRAffinity=1f6f99dc36715fce63518df2e3c0e912c4961391f63790097a7130fac11b8655;Path=/;HttpOnly;Secure;Domain=psiteapi.azurewebsites.net,ARRAffinitySameSite=1f6f99dc36715fce63518df2e3c0e912c4961391f63790097a7130fac11b8655;Path=/;HttpOnly;SameSite=None;Secure;Domain=psiteapi.azurewebsites.net"
},
{
"name": "Server",
"value": "Microsoft-IIS/10.0"
},
{
"name": "X-Powered-By",
"value": "ASP.NET"
},
{
"name": "Date",
"value": "Wed, 16 Jun 2021 15:59:30 GMT"
},
{
"name": "Content-Length",
"value": "0"
}
]
}
}


transfer-response (0.171 ms)
{
"message": "Response has been sent to the caller in full"
}


Inbound
(0.277 ms)
api-inspector (0.257 ms)
{
"request": {
"method": "POST",
"url": "https://psiteapi.azure-api.net/api/demo/AzureTest",
"headers": [
{
"name": "sec-ch-ua",
"value": "\" Not;A Brand\";v=\"99\",\"Google Chrome\";v=\"91\",\"Chromium\";v=\"91\""
},
{
"name": "sec-ch-ua-mobile",
"value": "?0"
},
{
"name": "Ocp-Apim-Subscription-Key",
"value": "9fc0d4d53a3a41e89ca863de5b2f4668"
},
{
"name": "Sec-Fetch-Site",
"value": "cross-site"
},
{
"name": "Sec-Fetch-Mode",
"value": "cors"
},
{
"name": "Sec-Fetch-Dest",
"value": "empty"
},
{
"name": "X-Forwarded-For",
"value": "172.58.144.45"
},
{
"name": "Cache-Control",
"value": "no-cache, no-store"
},
{
"name": "Content-Length",
"value": "0"
},
{
"name": "Content-Type",
"value": "text/plain;charset=UTF-8"
},
{
"name": "Accept",
"value": "*/*"
},
{
"name": "Accept-Encoding",
"value": "gzip,deflate,br"
},
{
"name": "Accept-Language",
"value": "en-US,en;q=0.9"
},
{
"name": "Host",
"value": "psiteapi.azure-api.net"
},
{
"name": "Referer",
"value": "https://apimanagement.hosting.portal.azure.net/"
}
]
}
}
api-inspector (0.003 ms)
{
"configuration": {
"api": {
"from": "/",
"to": {
"scheme": "https",
"host": "psiteapi.azurewebsites.net",
"port": 443,
"path": "/",
"queryString": "",
"query": {},
"isDefaultPort": true
},
"version": null,
"revision": "1"
},
"operation": {
"method": "POST",
"uriTemplate": "/api/demo/{str}"
},
"user": "-",
"product": "-"
}
}
cors (0.017 ms)
"Origin header was missing or empty and the request was classified as not cross-domain. CORS policy was not applied."
Backend
(752.442 ms)↑ Back to top
forward-request (0.095 ms)
{
"message": "Request is being forwarded to the backend service. Timeout set to 300 seconds",
"request": {
"method": "POST",
"url": "https://psiteapi.azurewebsites.net/api/demo/AzureTest",
"headers": [
{
"name": "Host",
"value": "psiteapi.azurewebsites.net"
},
{
"name": "Content-Length",
"value": 0
},
{
"name": "sec-ch-ua",
"value": "\" Not;A Brand\";v=\"99\",\"Google Chrome\";v=\"91\",\"Chromium\";v=\"91\""
},
{
"name": "sec-ch-ua-mobile",
"value": "?0"
},
{
"name": "Ocp-Apim-Subscription-Key",
"value": "9fc0d4d53a3a41e89ca863de5b2f4668"
},
{
"name": "Sec-Fetch-Site",
"value": "cross-site"
},
{
"name": "Sec-Fetch-Mode",
"value": "cors"
},
{
"name": "Sec-Fetch-Dest",
"value": "empty"
},
{
"name": "X-Forwarded-For",
"value": "172.58.144.45,13.91.254.72"
},
{
"name": "Cache-Control",
"value": "no-cache, no-store"
},
{
"name": "Content-Type",
"value": "text/plain;charset=UTF-8"
},
{
"name": "Accept",
"value": "*/*"
},
{
"name": "Accept-Encoding",
"value": "gzip,deflate,br"
},
{
"name": "Accept-Language",
"value": "en-US,en;q=0.9"
},
{
"name": "Referer",
"value": "https://apimanagement.hosting.portal.azure.net/"
}
]
}
}
forward-request (752.347 ms)
{
"response": {
"status": {
"code": 500,
"reason": "Internal Server Error"
},
"headers": [
{
"name": "Set-Cookie",
"value": "ARRAffinity=1f6f99dc36715fce63518df2e3c0e912c4961391f63790097a7130fac11b8655;Path=/;HttpOnly;Secure;Domain=psiteapi.azurewebsites.net,ARRAffinitySameSite=1f6f99dc36715fce63518df2e3c0e912c4961391f63790097a7130fac11b8655;Path=/;HttpOnly;SameSite=None;Secure;Domain=psiteapi.azurewebsites.net"
},
{
"name": "Server",
"value": "Microsoft-IIS/10.0"
},
{
"name": "X-Powered-By",
"value": "ASP.NET"
},
{
"name": "Date",
"value": "Wed, 16 Jun 2021 15:59:30 GMT"
},
{
"name": "Content-Length",
"value": "0"
}
]
}
}
Outbound
(0.171 ms)↑ Back to top
transfer-response (0.171 ms)
{
"message": "Response has been sent to the caller in full"
}

这是我在 My Net Web API 中安装的包:

  • Microsoft.AspNetCore.Mvc.NewtonsoftJson (3.1.0)
  • Swashbuckle.AspNetCore (6.1.4)
  • System.Data.SqlClient (4.8.2)
  • System.Runtime (4.3.1)

鉴于上述问题,我如何将我的 Net Web API 导入到 Azure API 管理并让它成功地向也在 Azure 中设置的 SQL Server 发出请求?

最佳答案

我从来没有正确设置数据库防火墙,这是解决方案:

Azure 门户 -> SQL 数据库 -> 你的数据库 -> 设置服务器防火墙 -> 允许 Azure 服务和资源访问此服务器 - 是

关于asp.net-web-api - Net Web API 发布到 Azure API 管理服务后出现 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68006896/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com