Logo

dev-resources.site

for different kinds of informations.

Returning Correct StatusCodes in Azure Function Apps

Published at
9/14/2024
Categories
azurefunctions
csharp
functionapp
azure
Author
agbagbarao
Author
10 person written this
agbagbarao
open
Returning Correct StatusCodes in Azure Function Apps

I was recently involved in working on some middleware which recieves requests from an asset management system , tranform the data and send it over to an on premise deployment of business management solution.

This middleware has to be implemented as a C# function app. One key feature of this middleware, was to use the guaranteed delivery pattern.

Implementing this as C# function app is quite a trivial which involves.

  1. Receive the request.
  2. Validate request, throw a validation error if neccessary and send response.
  3. Send the request to Service Bus and return an HTTP response

In C# function apps, it is possible to return a HTTP response and send the request to a Service Bus using a class

Image description

And this class can be used as follows

Image description

However when it comes to returning errors, I encountered a problem using

await response.WriteAsJsonAsync("Some Text");
Enter fullscreen mode Exit fullscreen mode

Using this statement results in the response status code always being 200 OK.
Gone are the days when developers would return a 200 OK status code regardless of the fact that an error has occurred. It is now a must to return the right status code when building APIs.

Code Segments to Trigger Errors

Returning Errors

Bad Request with 200 OK Status Code.

Bad Request with 200 OK status code

Internal ServerError with 200 OK Status Code

Internal Server Error with 200 OK status code

Solution

I found that the solution to this is to use

response.WriteString("Some String"); 
Enter fullscreen mode Exit fullscreen mode

and to set the following in the Program.cs

services.Configure<KestrelServerOptions>(options =>{
            options.AllowSynchronousIO  = true;
        });
Enter fullscreen mode Exit fullscreen mode

Updated Code

Image description

Status Codes Being Returned Correctly

Image description

Image description

This took me a while to find this solution, and I hope it saves you a lot of time and headache in trying to debug what the issue.

azurefunctions Article's
30 articles in total
Favicon
Secrets of a Successful Data Engineer
Favicon
Flex Consumption is not cheap (when in private VNET)
Favicon
🚀 Azure Function App: The Technical Backbone of Modern Applications
Favicon
Azure Function App (Flex Consumption) PowerShell Modules solution
Favicon
Azure functions isolated worker HTTP trigger: custom header disappears from response.
Favicon
The importance of release testing & questionable compiler optimizations
Favicon
Azure Functions with Python: Triggers
Favicon
Catching the Bus? How a Service Bus and Azure Functions Can Help Your Integration Reliability
Favicon
How to Create Timer Trigger Azure Functions with .NET 9: Step-by-Step Guide for Beginners
Favicon
Azure Function App (Flex Consumption) in private VNET via IaC
Favicon
Migrating Azure Function Calls to Minimal API with FastEndpoints
Favicon
Serverless Functions: Unlocking the Power of AWS Lambda, Azure Functions, and More
Favicon
Why Students Should Explore Microsoft Azure: The Cloud Platform for Your Future 🚀
Favicon
Cost Management in Azure: A Student-Friendly Guide to Managing Cloud Costs
Favicon
Azure Functions in .NET (C#) — The Ultimate Fun Version 🥳
Favicon
Building Scalable Applications with Azure Functions: Best Practices and Tips
Favicon
CREATING AN AZURE RESOURCE GROUP AND CLOUD STORAGE
Favicon
Returning Correct StatusCodes in Azure Function Apps
Favicon
Unlocking the Power of Azure Functions Flex Consumption Plan with Pulumi
Favicon
Implementing a Visitor Counter on Azure Resume Challenge
Favicon
Ready to Explore AI? Start with Azure AI Fundamentals!
Favicon
Handling Missing Configuration with Fallback Values in Azure Functions
Favicon
You don't need Dockerfile to containerise Azure Functions
Favicon
Azure Functions Hosting Models: In-Process vs. Isolated Process - What's Best for Your Project?
Favicon
How to Create Custom Middleware in Azure Functions: A Step-by-Step Guide with .NET 8
Favicon
Serverless Computing with .NET Core and Azure Functions
Favicon
Azure | Azure Functions By Example
Favicon
Improving Azure Functions Cold Boot
Favicon
Future-proof software development with the Azure Serverless Modulith
Favicon
Building business processes with Azure Durable Functions

Featured ones: