Azure-functions-host: Extensions.json not generated when using Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator 1.1.4

Created on 18 Feb 2020  路  15Comments  路  Source: Azure/azure-functions-host

Taking @jeffhollan's words into action, this is the main issue I've seen not only on my personal projects, but elsewhere too.

Error while running the functions via Visual Studio:

Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type

Error while deployed on a brand new Function v3 app in Azure:

System.InvalidOperationException: Unable to resolve service for type 'Project.Hidden.Domain.Interfaces.Service.IAzureStorageService' while attempting to activate 'Project.Hidden.Serverless.Importacion.VerificarFunction'.
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
   at lambda_method(Closure , IServiceProvider , Object[] )
   at Microsoft.Azure.WebJobs.Host.Executors.DefaultJobActivator.CreateInstance[T](IServiceProvider serviceProvider) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\DefaultJobActivator.cs:line 37
   at Microsoft.Azure.WebJobs.Host.Executors.DefaultJobActivator.CreateInstance[T](IFunctionInstanceEx functionInstance) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\DefaultJobActivator.cs:line 32
   at Microsoft.Azure.WebJobs.Host.Executors.ActivatorInstanceFactory`1.<>c__DisplayClass1_1.<.ctor>b__0(IFunctionInstanceEx i) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\ActivatorInstanceFactory.cs:line 20
   at Microsoft.Azure.WebJobs.Host.Executors.ActivatorInstanceFactory`1.Create(IFunctionInstanceEx functionInstance) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\ActivatorInstanceFactory.cs:line 26
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.CreateInstance(IFunctionInstanceEx functionInstance) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs:line 44
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ParameterHelper.Initialize() in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 846
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.TryExecuteAsyncCore(IFunctionInstanceEx functionInstance, CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 116

Only working when...

If I run the project from the cli with func start --build, all the functions work, but it logs the following errors on the console for each function:

[2020-02-18 12:30:49 AM] Error indexing method 'ImportarcionVerificarFunction'
[2020-02-18 12:30:49 AM] Microsoft.Azure.WebJobs.Host: Error indexing method 'ImportarcionVerificarFunction'. System.Private.CoreLib: Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified. System.Private.CoreLib: Could not load the specified file.
[2020-02-18 12:30:49 AM] Function 'ImportarcionVerificarFunction' failed indexing and will be disabled.
..
..
[2020-02-18 12:30:49 AM] The 'ImportarcionFinalizarFunction' function is in error: Unable to load one or more of the requested types.
[2020-02-18 12:30:49 AM] Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.

My system setup and configurations

Everything worked fine until I migrated to .NET Core 3

 - Visual Studio 16.5.0 Preview 2
 - [email protected]
 - [email protected]

global.json

{
  "sdk": {
    "version": "3.1.101"
  }
}

.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="3.1.1" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />
    <PackageReference Include="Utf8Json" Version="1.3.7" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\Project.Hidden.Data\Project.Hidden.Data.csproj" />
    <ProjectReference Include="..\Project.Hidden.Domain\Project.Hidden.Domain.csproj" />
    <ProjectReference Include="..\Project.Hidden.Services\Project.Hidden.Services.csproj" />
  </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

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Project.Hidden.Data;
using Project.Hidden.Domain.Interfaces.Repository;

[assembly: FunctionsStartup(typeof(Project.Hidden.Functions.Startup))]

namespace Project.Hidden.Functions
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSingleton<IAzureStorageService, AzureStorageService>();
        }
    }
}

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "FUNCTIONS_EXTENSION_VERSION": "~3"
  }
}

host.json

{
  "version": "2.0",
  "healthMonitor": {
    "enabled": true,
    "healthCheckInterval": "00:00:10",
    "healthCheckWindow": "00:02:00",
    "healthCheckThreshold": 6,
    "counterThreshold": 0.80
  },
  "functionTimeout": "00:10:00"
}

Most helpful comment

I've just pushed new versions of Microsoft.Net.Sdk.Functions to nuget. Versions 1.0.34 (for v1 and v2) and 3.0.5 (for v3) should be indexed and available shortly.

Please give those a try and let us know if you run into issues with generated extensions.json (or DI or bindings) with this package.

All 15 comments

Here's a reproduction of the error using the examples listed in the official documentation.

Before I created this project, I ran the following command:

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

And also updated to the latest version of VS:

Microsoft Visual Studio Enterprise 2019 Preview
Version 16.5.0 Preview 5.0

There are no compilation errors but when trying to hit the endpoint, I get this exception when running the function with Visual Studio:

[2020-03-05 1:41:46 AM] Executed 'GetPosts' (Failed, Id=7fa531e7-ec70-46e0-be39-ad261aa6e527)
[2020-03-05 1:41:46 AM] Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'System.Net.Http.IHttpClientFactory' while attempting to activate 'MyNamespace.HttpTrigger'.
[2020-03-05 1:41:46 AM] An unhandled host error has occurred.
[2020-03-05 1:41:46 AM] Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'System.Net.Http.IHttpClientFactory' while attempting to activate 'MyNamespace.HttpTrigger'.

And this error when running from the command line (func start --build)

[2020-03-05 1:02:36 AM] Executed 'GetPosts' (Failed, Id=633c9caf-76d5-471a-b0e2-19955ea99933)
[2020-03-05 1:02:36 AM] System.Private.CoreLib: Could not load file or assembly 'Microsoft.Extensions.Http, Version=3.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified. System.Private.CoreLib: Could not load the specified file.

FunctionsDependencyInjection.zip

Thank you @eliashdezr this helps a ton. I was able to use your zip and repro. Now I'm digging into it some as I'm slightly confused as I just had DI working in v3 a few hours ago to update some other docs. But appreciate you providing such a concrete repro

@eliashdezr I was able to get your sample to work by using the Azure Functions SDK 3.0.3. When I bump to 3.0.4 I do hit issues as well:

<!-- This one works -->
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />

Let me flag @paulbatum @brettsam as that just went out 4 days ago and appears something off there.

The big difference between 3.0.3 and 3.04 is that we trim the output now -- @fabiocav, any ideas on this? Are we too aggressive with the trimming?

The big difference between 3.0.3 and 3.04 is that we trim the output now -- @fabiocav, any ideas on this? Are we too aggressive with the trimming?

@brettsam This may help to debug the problem, I was having this exact issue and when comparing extensions.json after the upgrade to 3.0.4 it looked like this (missing any startup references)

{
  "extensions":[
  ]
}

Yeah, extension metadata generation is completely broken in 3.0.4 -- I think we should hide it from nuget. Because Microsoft.Azure.WebJobs.Host isn't in the output folder anymore, all extensions fail to load and we fail to find them. This includes all bindings.

I see this in the build logs:

Unable to find fallback for assembly 'Microsoft.Azure.WebJobs.Host'. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Azure.WebJobs.Host, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

Note that publishing does work. If I do dotnet publish, the extensions.json looks correct.

I believe the issue is here -- this should look similar to the publish target right below it and happen after extensions are generated. https://github.com/Azure/azure-functions-host/blob/dev/tools/ExtensionsMetadataGenerator/src/ExtensionsMetadataGenerator/Targets/Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator.targets#L30

@Coops9 / @eliashdezr -- you can see the fix to the targets file I've made here: https://github.com/Azure/azure-functions-host/pull/5748/files#diff-8924132f0d2b330a1888e0f17c5fcdb4

While we wait for approval/build/publish to nuget, you can try this locally if you want to verify it'll fix you -- the local file for you is in {nuget package location}\packages\microsoft.azure.webjobs.script.extensionsmetadatagenerator\1.1.4\build\Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator.targets. If you change that line and rebuild, you should get the correct output.

I've just pushed new versions of Microsoft.Net.Sdk.Functions to nuget. Versions 1.0.34 (for v1 and v2) and 3.0.5 (for v3) should be indexed and available shortly.

Please give those a try and let us know if you run into issues with generated extensions.json (or DI or bindings) with this package.

... although now that I look back up at the original issue it was opened 17 days ago, which was before 3.0.4 had been published. Maybe there's another issue hiding behind this one. If so, @eliashdezr would you mind opening a new issue since we've repurposed this one for the build problem in 3.0.4?

@brettsam thanks for the quick responses and effort put on resolve this quickly!

I can confirm that all the issues related with the dependency injection have been resolved, locally and deployed using the version 3.0.5 of the Microsoft.NET.Sdk.Functions package.

Thanks again and @jeffhollan too for jumping on this also.

Note that I still get an error when running a V3 function on a Linux App Service.
When deploying the same code (3.0.3 or 3.0.5) on Windows App Service, it works fine.

Anyone else noticed this same behaviour also?

@StefH --

  • Can you share your project file?
  • Can you verify that the published output is the same between windows and linux?
  • What error are you getting, specifically?

@brettsam

Let's follow up on the linked issue where there's more context. Thanks!

Was this page helpful?
0 / 5 - 0 ratings