Site icon TheWindowsUpdate.com

A coding pitfall in implementing dependency injection in .NET azure functions

This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Tech Community.

.NET Azure Functions supports the dependency injection (DI) software design pattern to achieve Inversion of Control (IoC) between classes and their dependencies.  The official article explains how to implement dependency injection in Azure .NET Functions.

 

2 key points are summarized as below:

1.  Create a custom Startup.cs that inherits from FunctionsStartup class from the Microsoft.Azure.Functions.Extensions NuGet package.  This startup class is meant for setup and dependency registration only but not for using any of the registered services during startup process.   The startup class runs once and only once to build up the ServiceCollection when the function host starts up.

2.  Service lifetimes:

This blog will show you a coding pitfall in implementing dependency injection in .NET azure functions to help you better understand the registered service lifetime, which is one of the most important parts in the DI world.

 

Let us review the code first:

 

 

 

 

 

 

 

 

 

 

The Problem:

The fruit delivery service hosted on the Azure App Service is operating well for some time,  then it would be broken with consistent 400 errors "The size of the request headers is too long" or equivalent messages.  The problem can be mitigated by a function app restart which typically means it is an application layer issue.

 

Root Cause Analysis:

 

Fixes:

 

Take-aways:

 

Exit mobile version