6
votes

I've created an Azure API App and deployed it to Azure. At first, I had no problem getting the Swagger file from the portal (i.e. by clicking the "API Definition" button on the API App blade), but at some point it stopped working.

I've enabled the Swagger UI in App_Start\SwaggerConfig.cs. When I debug locally, I can navigate to http://localhost:12345/Swagger to get to the Swagger UI and download the Swagger file, that all works fine. When I go to the portal, I get a 500 error trying to get to https://microsoft-apiappad6cxxxxxxxxxxxx426c23a66.azurewebsites.net:443/swagger/docs/v1.

I've tried deleting the API App and redeploying it... no luck. I've tried starting a new project and creating the same interface with it and deploying that to Azure... that one works every time (of course).

I'll include the Swagger file here, in case it matters, but what I don't understand is: why would Swashbuckle work locally, but not in Azure?

{
"swagger": "2.0",
"info": {
    "version": "v1",
    "title": "StorefrontApi"
},
"host": "localhost:52912",
"schemes": [
    "http"
],
"paths": {
    "/api/v1/storefront/applications": {
        "get": {
            "tags": [
                "GET"
            ],
            "operationId": "Storefront_Applications",
            "consumes": [],
            "produces": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml"
            ],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "$ref": "#/definitions/Object"
                    }
                }
            },
            "deprecated": false
        }
    },
    "/api/v1/storefront/aurorastatus": {
        "get": {
            "tags": [
                "GET"
            ],
            "operationId": "Storefront_AuroraStatus",
            "consumes": [],
            "produces": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml"
            ],
            "parameters": [
                {
                    "name": "auroraSerialNumber",
                    "in": "query",
                    "required": true,
                    "type": "string"
                }
            ],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "$ref": "#/definitions/Object"
                    }
                }
            },
            "deprecated": false
        }
    },
    "/api/v1/storefront/order": {
        "post": {
            "tags": [
                "POST"
            ],
            "operationId": "Storefront_Order",
            "consumes": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml",
                "application/x-www-form-urlencoded"
            ],
            "produces": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml"
            ],
            "parameters": [
                {
                    "name": "requestBody",
                    "in": "body",
                    "required": true,
                    "schema": {
                        "type": "string"
                    }
                }
            ],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "$ref": "#/definitions/Object"
                    }
                }
            },
            "deprecated": false
        }
    },
    "/api/v1/storefront/register": {
        "post": {
            "tags": [
                "POST"
            ],
            "summary": "",
            "operationId": "Storefront_Register",
            "consumes": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml",
                "application/x-www-form-urlencoded"
            ],
            "produces": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml"
            ],
            "parameters": [
                {
                    "name": "requestBody",
                    "in": "body",
                    "description": "",
                    "required": true,
                    "schema": {
                        "type": "string"
                    }
                }
            ],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "$ref": "#/definitions/Object"
                    }
                }
            },
            "deprecated": false
        }
    },
    "/api/v1/storefront/removeapplication": {
        "post": {
            "tags": [
                "POST"
            ],
            "summary": "",
            "operationId": "Storefront_RemoveApplication",
            "consumes": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml",
                "application/x-www-form-urlencoded"
            ],
            "produces": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml"
            ],
            "parameters": [
                {
                    "name": "requestBody",
                    "in": "body",
                    "description": "",
                    "required": true,
                    "schema": {
                        "type": "string"
                    }
                }
            ],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "$ref": "#/definitions/Object"
                    }
                }
            },
            "deprecated": false
        }
    },
    "/api/v1/storefront/updateserialnumber": {
        "post": {
            "tags": [
                "POST"
            ],
            "operationId": "Storefront_UpdateSerialNumber",
            "consumes": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml",
                "application/x-www-form-urlencoded"
            ],
            "produces": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml"
            ],
            "parameters": [
                {
                    "name": "requestBody",
                    "in": "body",
                    "required": true,
                    "schema": {
                        "type": "string"
                    }
                }
            ],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "$ref": "#/definitions/Object"
                    }
                }
            },
            "deprecated": false
        }
    }
},
"definitions": {
    "Object": {
        "type": "object",
        "properties": {}
    }
}

}

This, of course, limits the ability to consume the API App in a Logic App, and also to right-click / Add Azure API App client to my projects. (Yes, I can get the Swagger file locally and then modify and use it in the project, but that's not the flow we're looking for).

Does anyone have any ideas about what causes API Apps to fail to produce valid Swagger (or to execute the Swashbuckle code properly)?

5
Have you tried restarting the gateway? From the portal, click on your API app. There should be a hyperlink for Gateway under Essentials. When you click on it, you can restart the gateway. If you look in your deployment output it may be complaining that the gateway has to be restarted manually.Dan Gartner
Thanks for the idea... yes, I have, no luck. And, like I said, I completely deleted the deployment (including the gateway) and redeployed it to a new container, and I still couldn't see the API Definition.Scott

5 Answers

6
votes

Try go to the new Azure portal (preview portal), browse to your API App and set the App Service Plan/Pricing Tier to Standard.

I am having problem too when I first deployed my API App in Azure with the default App Service Plan and it seems like there is no instance created for the API App when it's the Free plan.

My API App simply works after I change the plan to Standard. Hope this helps!

My API App with Swagger in Azure

Useful references:

Create API App Tutorial

Deploy API App Tutorial

Edit: The answer was found in a comment below. After I updated my Swashbuckle NuGet packages, the Swagger showed up properly in the Azure Portal.

3
votes

Ran across this recently and found that the app.UseSwaggerUI in the Config method of Startup.cs was wrapped in a If Debug Compiler directive. Not sure if it was a developer or part of an Automated template but thoutght it worth mentioning.

0
votes

The process I use when the deployed version does not work is.

  1. Set access to public - to remove gateway introduced issues.
  2. Check a known end point api - https:\|xxxxx\yourapi
  3. Turn on Remote Errors in web.config
  4. Resolve any missing references not included in build (set Copy to Output Directory to true)
  5. Re-deploy
  6. Access Swagger endpoint.
  7. Re-enable security through gateway.

This allow you remove multiple failure points and see if the simplest functionality works before adding the extra layers in the new Api apps.

0
votes

You have to just update the Swashbuckle version and everything working fine

  • Install-Package Swashbuckle -Version 5.2.2 is not working
  • Install-Package Swashbuckle -Version 5.2.1 is working perfectly
0
votes

I had the same issue and it was solved by upgrading the service plan and updating the nuget package.