Top 10 Ways to Enhance Your APIs with Azure API Gateway

Jennie Lee
4 min readApr 9, 2024

--

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

Introduction to Azure API Gateway and ASP.NET Core Web API

Azure API Gateway is a fully managed service that provides a centralized hub for managing, securing, and monitoring APIs. It acts as an entry point for all API requests and offers numerous capabilities such as request throttling, caching, and authentication enforcement. By using Azure API Gateway, developers can simplify the process of building, deploying, and scaling their APIs.

On the other hand, ASP.NET Core Web API is a framework for building HTTP-based APIs using the ASP.NET Core runtime. It is a lightweight and cross-platform framework that allows developers to quickly build secure, scalable, and high-performance APIs. ASP.NET Core Web API supports various features such as routing, model binding, and content negotiation, making it an ideal choice for building APIs.

Efficient response times are crucial for API calls as they directly impact the user experience. Slow response times can lead to frustrated users and decreased performance of dependent systems. In this article, we will explore the issue of slow response times in Azure API Gateway and how we can enhance our APIs to improve performance.

Understanding the Issue with Slow Response Times in Azure API Gateway

Recently, I encountered an issue where the response times of my ASP.NET Core Web API, when called through Azure API Gateway, were slower than expected. The issue was not reproducible when calling the APIs directly through Swagger or Postman, but the performance degraded significantly when the APIs were called from another service within the authorization filter.

To get a better understanding of the issue, let’s take a look at some relevant code snippets from my solution.

ApiClient Class

public class ApiClient
{
private readonly HttpClient _httpClient;

public ApiClient(HttpClient httpClient)
{
_httpClient = httpClient;
}

public async Task<T> GetAsync<T>(string url)
{
var response = await _httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(responseBody);
}
}

AuthorizeProjectFilter Class

public class AuthorizeProjectFilter : IAuthorizationFilter
{
private readonly ApiClient _apiClient;

public AuthorizeProjectFilter(ApiClient apiClient)
{
_apiClient = apiClient;
}

public void OnAuthorization(AuthorizationFilterContext context)
{
// Code for authorization logic using ApiClient
}
}

After examining the code, the issue seemed unrelated to the implementation of the ApiClient or the AuthorizeProjectFilter class.

I conducted further investigation and discovered a clue in the Azure diagnostics trace. I noticed that most of the time was being spent on BLOCKED_TIME within the system.net.http namespace. This trace indicated that there might be something beyond the control of my application causing the slow response times.

Investigating Possible Causes for Slow Response Times in Azure API Gateway

To troubleshoot the issue, I started exploring potential causes that could contribute to the slow response times in Azure API Gateway. One possibility was network latency, but I ruled it out because the issue persisted even after multiple API calls. Another possibility was authentication problems, but the issue occurred even when making unauthenticated requests.

To make HTTP requests, I used the HttpClient class, which was added to the application using the IHttpClientFactory for proper management and performance optimization. The code worked perfectly fine when tested locally, but the slow response times were only experienced when deployed to Azure App Service.

Assistance from the Community and Solution Identification

To seek help and gain additional insights, I turned to the online community for assistance. In the top comments, a user suggested checking the corporate VNet that the application was connected to. They mentioned that it could be a potential cause of the slow response times.

Taking this suggestion into consideration, I investigated further and found that indeed the issue was related to the corporate VNet and its own DNS. The DNS was routing outgoing external requests through a service that checked for blacklisted addresses, causing additional delays before reaching the API.

The routing issue was responsible for the slow response times experienced in Azure API Gateway. Although this solution was specific to my situation, it highlighted the importance of thoroughly investigating all layers of the application stack when encountering performance issues.

Workaround and Conclusion

After extensively researching and not finding a direct solution to the issue, I decided to implement a workaround using event-driven architecture. Instead of relying solely on an external service for authorization, I introduced an event-driven approach within the application. Whenever an action requiring authorization was triggered, an event was raised and processed internally without making an external API call. This made the system more fault-tolerant and reduced the reliance on problematic services.

In conclusion, enhancing your APIs with Azure API Gateway can provide numerous benefits such as centralized management, security, and monitoring. However, when encountering issues with slow response times, it is crucial to thoroughly investigate potential causes such as network latency, authentication problems, or routing issues. By leveraging the support of the online community and implementing workarounds like event-driven architecture, you can overcome performance hurdles and provide an optimal API experience.

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

--

--

Jennie Lee
Jennie Lee

Written by Jennie Lee

Software Testing Blogger, #API Testing

No responses yet