Search code examples
azureazure-web-app-serviceazure-api-apps

Using API Apps with Swagger in dev/test/production environments


I'm migrating a combined Azure Website (with both Controllers and ApiControllers) to a split Web App and API App. Let's call it MyApp.

I've created MyAppDevApi, MyAppTestApi, and MyAppProductionApi API Apps (in different App Services) to host the three environments, expecting to promote code from one environment to another.

So far, I've only deployed to MyAppDevApi, since I'm just getting started.

When I do Add/Azure API App Client to my UI-only project to start referring to the API app, and I point it to MyAppDevApi, it uses AutoRest to create classes in my code. These classes now all have the name MyAppDevApi, rather than just MyAppApi, which is the actual namespace of the code I'm deploying to every environment. Obviously, I can't check that in... how can I promote that through Test and Prod?

There's nothing in the Swagger JSON that refers to this name, so it must be on the AutoRest side (I think).

Has anyone come up with a strategy or work-around to deal with this multiple-environment promotion issue with API Apps?

Edit

So far the best thing I've come up with is to download the Swagger from the API App to a local file (which, again, has only the namespace from the original code, not from the name of the API App), and then import it into the Web App. This will generate classes in the Web App that have the naming I expect.

The problem is that I then have to edit the generated MyAppApi.cs file's _baseUri property to pull from an AppSetting, have the different web.config.dev, .test, .prod, and then do the web.config transform. I mean, that'll work, but then every time I change the API App's interface, I'll regenerate... and then I'll have remember to change the _baseUri again... and someone, sometime is going to forget to do this and then deploy to production. It's really, really fragile.

So... anyone have a better idea?


Solution

  • Well, here's how I've solved this:

    1. Download Swagger file from API App to local hard drive.
    2. Import local Swagger file into Web App to generate classes that have the naming from my code, not from the environment.
    3. Use AppSettings to specify the environment-specific settings to point to the API App. This can be either a web.config transform, or you can just specify them in the Azure Portal on the Web App in Application Settings.
    4. Instantiate the generated API App Client using the constructor that takes in a URL to point to the API App (these are at class level, hence static):
        private readonly static Uri apiAppUrl = new Uri(CloudConfigurationManager.GetSetting("ApiAppUrl"));
        private readonly static MyAppApi myAppApi = new MyAppApi(apiAppUrl);
    

    I'd still love a solution to this that doesn't require downloading the Swagger file, but, all in all, if that's the only workaround necessary, it's not all that bad.