Azure API Management, Azure App Service

Auto documented API using Swagger in dot.net Core to integrate with Azure API Management : Part 2

In this post, we are discussing, how to Get Started with Azure API Management, How we can automate API subscription and management using Azure API Management.

This post is a part of the Azure Services series, in this series, we will discuss about Microsoft Cloud Application Architecture and common cloud computing patterns to make application resilient and scalable. We will be using Asp.net MVC as our framework for applicable examples and Azure as cloud services provider.

You can find all the Microsoft Azure Services; we have covered already at Azure series.

Auto documented API using Swagger in dot.net Core to integrate with Azure API Management : Part 2

In previous post we discussed, how the Azure API management tool work, briefly discussed Developer dashboard and subscription the Echo API.

In this post we are going to create our own API and set it up with Azure API management and consume it on client side.

Let us fire up our visual studio 2017, File Menu | New Project | Under Visual C# select .NET Core | Select Asp.net Core Web Application | click okay to create it.

Right click on Project in Visual Studio and choose the Manage NuGet Packages.

Click on browse tab, and search for Swagger, we will go for swachbuckle.aspnetcore, since we are using .net core so we need to choose Swachbukle.AspNetCore (this already include swaggerGen, UI and Swagger, so you don’t need to install each of them separately ) Package and install it.

Now add the following bold lines in the Startup.cs file

When we are using Swagger, we need to explicitly need to make sure our controller has Http verb attribute and for non-route parameters “From” body tag.

Under controller Directory | Open Values Controller | and verify that your code match as below:


 

Now we can run our application and see output. Since we have configured the c.RoutePrefix = “” that means the Swagger will open on the root of the site, navigate to the root site, in this example we will go to localhost:61300



As you can see under the Values, there are actions listed, but we do not have any documentation attached to them. Let us do just that.

Open the Values Controller class and add some details like below:

You can use triple forward slash to add summary tag for you, just press /// before the signature of the action and visual studio will add summary tags for you.


Now we need to enable these comments to show up in the swagger UI, Right click on Project | Properties | Build | check the XML documentation File.


Now we need to add using System.IO; in startup.cs

And add below lines of code as well, you may need to change the xml name if your xml file in build tab is different.

public
void ConfigureServices(IServiceCollection services, IHostingEnvironment env)

{


//services.AddMvc();

services.AddMvc().AddJsonOptions(options =>

{

options.SerializerSettings.Formatting = Formatting.Indented;

});

if (env.IsDevelopment())

{

services.ConfigureSwaggerGen(c =>

{


var xmlCommentsPath = Path.Combine(System.AppContext.BaseDirectory, “AzureAPIManagement.xml”);

c.IncludeXmlComments(xmlCommentsPath);

});

}

services.AddSwaggerGen(c =>

{

c.SwaggerDoc(“v1”, new Info { Title = “AzureAPIManagementV1”, Version = “v1” });

});

}

Now run the solution, you can see that the documentation is coming up.


Now our Basic API is ready, let us host it on Azure App Service, you can publish anywhere you want unless you get the Public URL of your API and then in next part we will configure it with API management and will use it on client side.

Right click on project | Publish | create new App Service | fill in the details | Click Create. It will take a little bit for time to publish for first time as it will create new app service for you.


After create | click on publish if it is not publishing already | you will see that after publish it automatically open the API URL in browser.


Next we will configure our API in Azure API Management, which is coming up next.