Adding dependency injection to Azure Functions via Startup.cs results in host error

Viewed 1458

I have the following Startup.cs file

using System;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Namespace.Functions.Services;

[assembly: FunctionsStartup(typeof(Namespace.Functions.Startup))]

namespace Namespace.Functions
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddHttpClient();
            builder.Services.AddSingleton<INamespaceApiService, QuizService>();
        }
    }
}

After adding this, when I run the functions app I get the following in output

[10/15/2020 5:31:33 PM] Loaded extension 'Startup' (1.0.0.0)
[10/15/2020 5:31:33 PM] A host error has occurred during startup operation 'f456f2c1-5fd7-4253-859c-c8dc5bc3b2ca'.
[10/15/2020 5:31:33 PM] System.Private.CoreLib: Could not load type 'Microsoft.Azure.WebJobs.Hosting.IWebJobsStartup2' from assembly 'Microsoft.Azure.WebJobs.Host, Version=3.0.17.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

This is my .csproj file

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.1.1" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.9" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Host.Storage" Version="3.0.14" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="4.1.0" />
    <PackageReference Include="SendGrid" Version="9.16.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="4.0.2" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="3.0.7" />
    <PackageReference Include="Twilio" Version="5.44.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.8" />
    <PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.22" />
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Services\" />
  </ItemGroup>
</Project>

If I remove the Startup.cs file things run fine - what do I need to do to resolve this error?

2 Answers

This issue seems to suggest that the error might be able to be resolved by downgrading the Microsoft.Azure.Functions.Extensions package to version 1.0.0.

I can confirm that I have an Azure Function using dependency injection and .NET Core with the following versions of the required packages:

<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.7" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.9" />

Another issue seems to suggest that you will need to the version of Azure Function Core Tools that is installed on your system.

npm i -g azure-functions-core-tools@3 --unsafe-perm true

What does unsafe-perm in npm actually do.

TL;DR

You have two options:

  • Downgrade your Microsoft.Azure.Functions.Extensions package to version 1.0.0
  • OR
  • Upgrade the currently installed version of Azure Function Core Tools

A colleague of mine had this issue as well (while it was working fine on my Mac); his Visual Studio for Mac was using an older version of the Azure Functions Core Tools (3.0.2358). It's not immediately obvious how to update those; you need to update the list of templates you have when creating a new Azure Function in your solution/project.

Credits to @MattWard here.

Related