Home
%3CLINGO-SUB%20id%3D%22lingo-sub-807918%22%20slang%3D%22en-US%22%3EFunction%20App%20using%20Dependency%20Injection%20throwing%20%22System.ArgumentNullException%E2%80%9D%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-807918%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EIntroduction%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20article%20shows%20you%20how%20to%20troubleshoot%20a%20specific%20symptom%20that%20you%20may%20encounter%20when%20you%20have%20Durable%20Function%20with%20Dependency%20Injection.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EDurable%20Functions%26nbsp%3Bare%20an%20extension%20of%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-functions%2Ffunctions-overview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Functions%3C%2FA%3E%26nbsp%3Bthat%20lets%20you%20write%20stateful%20functions%20in%20a%20server%20less%20environment.%20The%20extension%20manages%20state%2C%20checkpoints%2C%20and%20restarts%20for%20you.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20know%20that%20the%20output%20of%20one%20function%20becomes%20the%20input%20of%20another%20functions.%20But%20what%20if%20the%20first%20function%20output%20is%20%E2%80%9CNull%E2%80%9D%3F%20You%20see%20exception%20as%20%E2%80%9CValue%20cannot%20be%20Null%E2%80%9D.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ESymptom%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EYou%20may%20see%20%E2%80%9CSystem.ArgumentNullException%E2%80%9D%20for%20your%20Function%20App%20which%20uses%20Dependency%20Injection%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ECause%20of%20the%20Issue%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhile%20using%20the%20Dependency%20Injection%2C%20it%20is%20recommended%2C%20and%20best%20practice%20to%2C%20not%20use%20%E2%80%9Cstatic%E2%80%9D%20variable%20which%20could%20change.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20issue%20is%20that%20the%20initialization%20code%20is%20%3CSTRONG%3Enot%20always%20called%20%3C%2FSTRONG%3Eon%20a%20given%20instance%20of%20the%20function%20app.%20This%20is%20also%20why%20we%20see%20functions%20always%20succeeding%20or%20never%20succeeding%20on%20a%20given%20instance.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt%E2%80%99s%20extremely%20important%20to%20note%20that%20instance%20setup%3CSTRONG%3Eshould%20not%3C%2FSTRONG%3Ebe%20done%20by%20an%20unrelated%20function%2C%20because%20there%20are%20no%20guarantees%20about%20which%20functions%20run%20where%20(that%E2%80%99s%20the%20power%20of%20server%20less%20scale).%20On%20startup%20of%20the%20instance%20or%20through%20%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdocs.microsoft.com%252Fen-us%252Fazure%252Fazure-functions%252Ffunctions-dotnet-dependency-injection%26amp%3Bdata%3D02%257C01%257Cmahoeger%2540microsoft.com%257Ce6e1da0b7db94bc2b68e08d6f45137ca%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636965027149826944%26amp%3Bsdata%3Dr%252F8rNt3qgEaNUdsWZiJ3OuQWtGt9V0vqp8xjCMqpx9o%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Edependency%20injection%3C%2FA%3E%2C%20any%20initialization%20code%20should%20be%20done.%20It%E2%80%99s%20dangerous%20to%20use%20a%20static%20variable%20that%20can%20be%20changed%2C%20again%2C%20because%20there%20are%20no%20guarantees%20about%20which%20functions%20run%20where%20and%20when.%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EMitigation%2FWorkaround%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3ETo%20mitigate%20the%20issue%2C%20we%20can%20add%20the%20%E2%80%9CrunOnStartup%20%3D%20true%E2%80%9D%20on%20configuration.%3C%2FP%3E%0A%3CP%3EThis%20is%20a%20very%20hacky%20solution%20for%20initialization%20code%2C%20but%20this%20is%20a%20quick%20way%20to%20mitigate%20their%20issue%20while%20a%20longer-term%20solution%20is%20put%20in%20place.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EReference%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdocs.microsoft.com%252Fen-us%252Fazure%252Fazure-functions%252Ffunctions-dotnet-dependency-injection%26amp%3Bdata%3D02%257C01%257Cmahoeger%2540microsoft.com%257Ce6e1da0b7db94bc2b68e08d6f45137ca%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636965027149826944%26amp%3Bsdata%3Dr%252F8rNt3qgEaNUdsWZiJ3OuQWtGt9V0vqp8xjCMqpx9o%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-functions%2Ffunctions-dotnet-dependency-injection%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdocs.microsoft.com%252Fen-us%252Fazure%252Fazure-functions%252Ffunctions-bindings-timer%2523configuration%26amp%3Bdata%3D02%257C01%257Cmahoeger%2540microsoft.com%257Cbd33863e336b42ec27cf08d6f451b434%257C72f988bf86f141af91ab2d7cd011db47%257C1%257C0%257C636965029225770860%26amp%3Bsdata%3DE9C6J5inuvTXKCEktsJ1fPflz1ghnQ1VIKa8B0qW0A0%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-functions%2Ffunctions-bindings-timer%23configuration%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20right%3B%22%3E%3CSTRONG%3EAuthored%20By%20-%20Shashank%20Ranjan%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft

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