Azure functions dependency injection - error when referencing class library
See original GitHub issueI 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:
- Create a new functions app
- Create a new class library
- Setup DI for your functions app as described here: https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection
- Add the same extension method as above to the class library
- Add a reference to the class library from the Azure Functions project
- 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:
- Created 4 years ago
- Comments:14
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
✔️ 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
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?