question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Azure functions dependency injection - error when referencing class library

See original GitHub issue

I have a Azure Functions project that has a reference to a class library (netstandard 2.0). The class library contains a reference to Microsoft.Extensions.DependencyInjection.Abstractions. The class library has the following extension method:

using JOS.MyApp.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace JOS.MyApp.Application
{
    public static class ApplicationConfigurator
    {
        public static void AddApplication(this IServiceCollection services)
        {
              // Do stuff
        }
    }
}

My functions startup looks like this: Startup.cs

using JOS.MyApp.Functions;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

[assembly: FunctionsStartup(typeof(Startup))]
namespace JOS.MyApp.Functions
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
              // It does not matter if this call is here or not, it crashes because of the reference to the class library anyway.
               builder.Services.AddApplication();
        }
    }
}

When the Azure Functions project references the class library, the following error occurs:

Azure Functions Core Tools (2.7.1158 Commit hash: f2d2a2816e038165826c7409c6d10c0527e8955b)
Function Runtime Version: 2.0.12438.0
SKipping 'FUNCTIONS_CORETOOLS_ENVIRONMENT' because value is null
[2019-05-14 20:10:32] Starting Rpc Initialization Service.
[2019-05-14 20:10:32] Initializing RpcServer
[2019-05-14 20:10:32] Building host: startup suppressed:False, configuration suppressed: False
[2019-05-14 20:10:32] A host error has occurred
[2019-05-14 20:10:32] JOS.MyApp.Functions: Method not found: 'Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Azure.Functions.Extensions.DependencyInjection.IFunctionsHostBuilder.get_Services()'.
Value cannot be null.
Parameter name: provider

If I remove the reference to the class library, the function app starts as expected. So the problem appears to be that im using the Microsoft.Extensions.DependencyInjection.Abstractions package in my class library.

To Reproduce

Steps to reproduce the behavior:

  1. Create a new functions app
  2. Create a new class library
  3. Setup DI for your functions app as described here: https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection
  4. Add the same extension method as above to the class library
  5. Add a reference to the class library from the Azure Functions project
  6. Try to run the functions project

Expected behavior

The functions project starts without any issues.

Additional context

Host (useful for support):
  Version: 3.0.0-preview5-27626-15
  Commit:  61f30f5a23

.NET Core SDKs installed:
  2.1.602 [C:\Program Files\dotnet\sdk]
  2.1.700-preview-009597 [C:\Program Files\dotnet\sdk]
  2.1.700-preview-009601 [C:\Program Files\dotnet\sdk]
  2.1.700-preview-009618 [C:\Program Files\dotnet\sdk]
  2.2.202 [C:\Program Files\dotnet\sdk]
  2.2.203 [C:\Program Files\dotnet\sdk]
  2.2.300-preview-010046 [C:\Program Files\dotnet\sdk]
  2.2.300-preview-010050 [C:\Program Files\dotnet\sdk]
  2.2.300-preview-010067 [C:\Program Files\dotnet\sdk]
  3.0.100-preview5-011568 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0-preview5-19227-01 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0-preview5-27626-15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.0.0-preview5-27626-15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14

github_iconTop GitHub Comments

15reactions
cfe84commented, Oct 7, 2019

✔️ Had the same issue. Just to clarify how to fix it for anyone who would get that out of the box following the instructions on DI with .net Functions - downgrade version of nuget package Microsoft.Extensions.DependencyInjection from 3.0.0 to 2.2.0

3reactions
VaclavEliascommented, May 14, 2019

Just a guess, can you mix your class library which is referencing Microsoft.Extensions.DependencyInjection.Abstractions 3.0.0-preview5.19227.9 with Azure Function netcoreapp2.1?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Referencing class library .dll file in Azure Function, do I ...
2 Answers. That depends on you. If you want to use Dependency Injection of Azure function(it's recommended), you should always setup services ...
Read more >
Use dependency injection in .NET Azure Functions
Azure Functions supports the dependency injection (DI) software design pattern, which is a technique to achieve Inversion of Control (IoC) ...
Read more >
Develop C# class library functions using Azure Functions
Understand how to use C# to develop and publish code as class libraries that run in-process with the Azure Functions runtime.
Read more >
Guide for running C# Azure Functions in an isolated worker ...
NET class library functions run in the same process as the host. ... Dependency injection can be used to interact with other Azure...
Read more >
Differences between in-process and isolate worker ...
Compares features and functionality differences between running .NET Functions in-process or as an isolated worker process.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found