azure-functions-host: Microsoft.AspNetCore.DataProtection - IOC error

After updating my function to v3 I get this error on startup:

[08/01/2020 15:11:34] Loading startup extension 'Startup'
[08/01/2020 15:11:34] Loaded extension 'Startup' (1.0.0.0)
[08/01/2020 15:11:34] A host error has occurred during startup operation 'fba5bef3-a5ce-4006-a20a-160977aca2d8'.
[08/01/2020 15:11:34] func: Invalid host services. Microsoft.Azure.WebJobs.Script.WebHost: The following service registrations did not match the expected services:
[08/01/2020 15:11:34]   [Invalid] ServiceType: Microsoft.Extensions.Hosting.IHostedService, Lifetime: Singleton, ImplementationType: Microsoft.AspNetCore.DataProtection.Internal.DataProtectionHostedService.
Value cannot be null. (Parameter 'provider')

This is my current setup:

  • Microsoft Visual Studio Professional 2019 Version 16.4.2
  • azure-functions-core-tools@3.0.2009

And this is my .csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.DataProtection.AzureKeyVault" Version="3.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.DataProtection.AzureStorage" Version="3.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" Version="3.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>
</Project>

Startup.cs file:

using System;
using FunctionApp1;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Azure.Storage.Auth;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Extensions.DependencyInjection;


[assembly: FunctionsStartup(typeof(Startup))]

namespace FunctionApp1
{
    internal class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var storageUri = new Uri("https://potato");

            var blobClient = new CloudBlobClient(storageUri, new StorageCredentials("accountname", "pass"));
            var container = blobClient.GetContainerReference("test");

            builder.Services.AddDataProtection()
                .PersistKeysToAzureBlobStorage(container, "test")
                .ProtectKeysWithAzureKeyVault("test", "test", "test");
        }
    }
}

It was definitely working in 2.2 Downgrading Microsoft.AspNetCore.DataProtection packages do not help. Running from VS or in the command line returns the same error.

The only solution is to do not upgrade to the v3 function app 😦

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 34 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Any update on this issue? I just need to implement AAD role based authentication in Azure function v3 but when calling AddAuthentication never works failed with HostService error. If there is any other solutions for AAD role based authentication in AzFunctions v3 let me know

For those implicitly using the DataProtectionHostedService i.e. by using AddIdentity or similar you can remove it by using this line after

builder.Services.Remove(builder.Services.First(x => x.ImplementationType?.Name == "DataProtectionHostedService"));

This works around the issue for my use case, but this definitely needs a proper solution as this is an undocumented breaking change between V2 and V3, nothing about it on the migration guide.

Just an update on this. We’ll be removing this restriction for data protection. @brettsam can share more information if needed, but this restriction will be lifted in the release for the current sprint (set to begin at the end of next week)

@fabiocav Can you special case that concrete type? https://github.com/dotnet/aspnetcore/blob/6255c1ed960f5277d2e96ac2d0968c2c7e844ce2/src/DataProtection/DataProtection/src/Internal/DataProtectionHostedService.cs#L35

This will at least unblock people for now while we figure out what a longer term solution looks like.

We don’t allow you to register any IHostedService as a part of your Startup operation.

Why not?

Yes it looks like this is now deployed to production – I don’t believe that the core tools (or VS tools) have been updated yet but that will be coming soon.

Also ran into this issue when trying to migrate to v3. I am registering RazorPages/RazorViewEngine which registers the DataProtection library.

@zebslc this is a Functions specific limitation.

Surely this should be listed in https://docs.microsoft.com/en-us/dotnet/core/compatibility/2.2-3.1 ? - the only mentionto data protection is about a change in storage namespace but this breaks our application totally. 😦