Azure Function Apps with dependency injection

David Buckell 191 Reputation points
2021-04-08T17:35:37.667+00:00

I have an existing function app with multiple functions, which all work as expected when declared with the default static class method signature.
I now want to rebuild this to make use of dependency injection and make asynchronous calls to blob storage.
Having followed this guide https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection, my function apps don't appear to do anything and i'm having issues trying to debug them.
I previously would use https://<functionappname>.scm.azurewebsites.net/ and use the debug option but this no longer appears to exist.
I noticed that a couple of packages had pending nuget updates so I upgraded the latest packages to their latest versions:

  • microsoft.extensions.configuration.usersecrets
  • microsoft.extensions.dependencyinjection
  • microsoft.extensions.dependencyinjection.abstractions

now when i add a message to my queue in order to test my queue trigger function and view the monitor section i sometimes see a function failed log entry with no specific details, but nothing appears in the invocations list. my logging info from my app was not displayed so it appears the function just immediately bombed out even though the queuetrigger picked up the queue msg.

my next step will be to create a fresh function app and implement it step by step, for example just try ILogger first and then add my own dependencies one by one.

in the interim i was wondering whether anyone else has stumbled across this and has any experience with resolving this.

i am developing my function app in .net core 3.1 & and attempting to deploy to a linux hosted consumption plan.

so things that would help:

1) how can i now debug this? application insights does not appear to be an option
2) any pointers to implementing DI within function apps? i saw an alternative post suggesting having your startup class inherit from IWebJobsStartup instead, but that doesn't appear to help at all
3) does anyone have a better guide to follow? the microsoft blog post i followed was only written 2 weeks ago so i would expect to work

thanks

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,229 questions
0 comments No comments
{count} votes

Accepted answer
  1. David Buckell 191 Reputation points
    2021-04-08T18:09:16.313+00:00

    ah i have done a little more digging and have stumbled across the Investigate -> Failures section within the application insights section of the portal.
    initially loading this page displays "error displaying your content" with the following details so assumed this would not be able to help:

    Error: Failed to retrieve the blade definition for 'AspNetOverviewV3' from the server. Manual require of the following modules failed; ["_generated/Blades/AspNetOverviewV3" : {Error: Couldn't load "_generated/Blades/AspNetOverviewV3" at "https://afd.hosting.portal.azure.net/appinsights/Content/Dynamic/iE-E1AhCnKyg.js" as the following dependencies failed to load; ["Overview/ViewModels/AspNetOverviewBlade" : {Error: Couldn't load "Overview/ViewModels/AspNetOverviewBlade" at "https://afd.hosting.portal.azure.net/appinsights/Content/Dynamic/5I1DBHoYhADo.js" as the following dependencies failed to load; ["FxInternal/Controls/ProgressBar" : {Error: Couldn't load "FxInternal/Controls/ProgressBar"; error code 503},"FxInternal/Controls/ProgressBar" : {Error: Couldn't load "FxInternal/Controls/ProgressBar"; error code 503}];

    but the failures section does display the top 3 exception types and drilling down into this I can see a dependency of my dependency is null.
    having added this extra dependency to my startup class my function is now behaving as expected.

    i still did not see any output in the logs so would still like to know how to access these logs since the kudu webpage no longer appears to offer this option

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. David Buckell 191 Reputation points
    2021-04-10T22:14:30.787+00:00

    I set up a simple example that injects a service that simply logs to the console.
    Documented here: https://www.davidbuckell.com/blogs/development/azure/function-apps/creating-function-apps-with-dependency-injection-support

    My findings are that I am only able to reliably view my debug output via the emulator or the invocations tab. I would prefer to be able to access the full log files somehow.

    If anyone knows how to access function app log files I would be interested in learning how.

    0 comments No comments

  2. David Buckell 191 Reputation points
    2021-04-11T14:17:34.127+00:00

    Having played with application insights I have discovered how to run custom queries & this has provided me with a reliable way of viewing the debug output.
    I have updated the above blog post with details on how I achieved this.

    Hope this helps others in future.

    0 comments No comments