Function App using Dependency Injection throwing “System.ArgumentNullException”

Introduction


 


This article shows you how to troubleshoot a specific symptom that you may encounter when you have Durable Function with Dependency Injection.


 


Durable Functions are an extension of Azure Functions that lets you write stateful functions in a server less environment. The extension manages state, checkpoints, and restarts for you.


 


We know that the output of one function becomes the input of another functions. But what if the first function output is “Null”? You see exception as “Value cannot be Null”.


 


 


Symptom


You may see “System.ArgumentNullException” for your Function App which uses Dependency Injection


 


 


Cause of the Issue


 


While using the Dependency Injection, it is recommended, and best practice to, not use “static” variable which could change.


 


The issue is that the initialization code is not always called on a given instance of the function app. This is also why we see functions always succeeding or never succeeding on a given instance.


 


It’s extremely important to note that instance setup should not be done by an unrelated function, because there are no guarantees about which functions run where (that’s the power of server less scale). On startup of the instance or through dependency injection, any initialization code should be done. It’s dangerous to use a static variable that can be changed, again, because there are no guarantees about which functions run where and when.


 


Mitigation/Workaround


To mitigate the issue, we can add the “runOnStartup = true” on configuration.


This is a very hacky solution for initialization code, but this is a quick way to mitigate their issue while a longer-term solution is put in place.


 


Reference


https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection


https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer#configuration


 


Authored By – Shashank Ranjan

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.