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.

Advertisements
Azure API Management, Azure App Service

How to Get Started with Azure API Management : Part 1

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.

How to Get Started with Azure API Management : Part 1

The first question we get in our mind is, why to use Azure API management, what benefits it would give us, and why should we use it.
Azure API Management provide us the ability to create API gateway and developer portal, which is scalable, secure and can work with any host or API and it gives us insights of our APIs as well. The one thing most of us API developers struggle is, how to better manage on boarding process, documentation and provide easiest integration experience to our API’s end users /consumers/developers. This is the part, where Azure API management shines, it give us developer portal to manage the subscription of the services and provides a first-class developer experience. We will discuss all the functionality that Azure API Management provides and how to get most of it.

Let us create our Azure API management Instance first, usually it take a little bit longer around 30 mins to complete creation process.

Go to Azure portal dashboard, click create a new resource button | Search for API management | select API management | click create.

Now we need to fill the form, choose the pricing tier and click create. Let us look at pricing tier first; we have developer, basic, standard and premium tiers.

For Production, either go for basic or high tier since it will give you 99.9+ SLA uptime and options for scalability as well. Every tier support max request per second limitation, if you have only 1 API and 250 subscribers to the service then Basic tier will be sufficient if all 250 users send 2 request per second. From Developer to standard tier, it will only support single region, you can check which region is nearest to your customers/consumers by latency. If you have requirement to support multiple regions, then your only option is to go with Premium tier, and the price per month will increase drastically. Every tier also give us Redis cache as well and capacity is mentioned per instance, if you go for standard tier and you scale it up to 2 instances then you will have 1GB Redis Cache per instance totaling to 2 GB. Other things to consider is AAD integration and virtual network support, if you have need to connect with ADD then go for specific tier after evaluating price to value ratio and also consider virtual network support if you have API service hosted in VM in virtual network then consider it when you make any decision.

For our purpose, we are going with Developer tier, since we are not using it for production and it is giving us almost all the functionalities that other tiers give us except the scaling.

By default, the API management service will have a public URL yourdomain.azure-api.net but we can configure our own custom domain as well.

Creation process will take about 30+ minutes; it took around 38 mins to create in our case. Once the API Management Service is ready, you can see that

Click on developer portal link, will see the screen like below, currently developer/end users can sign up in this portal, the default sign in is configured by default to accept username and password. You can change this.

Go to your API Management Service in Azure Portal | Security | Identity, as you can see we have all the popular options such as Azure Active Directory, Facebook, Google, Microsoft as Identity provider. You may need to add or remove the Providers according to your business needs.

Go back to the Developer Portal and register with any secondary email, you will get confirmation email with link, click on the link to activate the user. Now login to the portal and navigate to the API tab. By Default, we have Echo API listed.

This window will show all the API that are available to consume, click on Echo API, you will see the window like below. By default, first resource is selected and in order to call this resource we need Ocp-Apim-Subscription-Key, you can get this key by subscribing to the Api. You can also click on Try it button to call the resource in the browser, we also have API Definition button if you want to download the API definition in WADL or Open API format.

Let us get our subscription ID, click on Products tabs, choose unlimited for now, we will talk about product later on and click subscribe and confirm the subscription.

Once you are done, a thank you email will be sent to you and you will see screen link below. Right now our unlimited product requires confirmation from Administrator side. So let us go to the Azure Portal and activate our subscription.

Back in the Azure Portal, click on Products | select Unlimited | subscriptions

We have two subscribers here, first one the API Management Service owner who have created this service, and second subscription entry is that we have just submitted earlier. Click on … button and choose Activate subscription and then click yes.

Now go back to Developer portal and refresh, you can see that subscription state is active now and you can also see the Primary key and secondary key.

Next step is to test the Echo service, click on APIS tab, select echo API and click on try. You can see now that the subscription key is automatically populated for you, you just need to click on send.

The response will be shown underneath of the send button.

Now let us create our own API and configure it with the Azure API Management, coming up next