dat1dev
dat1dev

Reputation: 81

Net Web API Post 500 Error After Publishing To Azure API Management Service

I Am Attempting To Use Microsoft Azure To Create A Remote SQL Server Database And Net Web API Services That Will Connect To That Database. I Have Successfully Created A Database With Microsoft Azure, And Have Followed This Tutorial To Import A Net Web API To API Management Services In Azure. I Have A Post That Functions When I Run It Locally, Tested Via Postman And SQL Server, However Upon Testing In The Azure Portal After Publishing I Receive A 500 Error.

I Ran Into Some Difficulty On Publishing And Resolved It Like So:

Here's The Trace After Testing The Post Request:

   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"
    }

Here Are The Packages I Installed In My Net Web API:

How Can I Import My Net Web API To Azure API Management And Have It Successfully Make Requests To The SQL Server Also Setup Within Azure Given The Issues Described Above?

Upvotes: 3

Views: 619

Answers (1)

dat1dev
dat1dev

Reputation: 81

I Never Setup The Database Firewall Properly, Heres The Solution:

Azure Portal -> SQL Databases -> Your Database -> Set Server Firewall -> Allow Azure Services And Resources To Access This Server - Yes

Upvotes: 2

Related Questions