简体   繁体   中英

Why is an Azure Function on .NET 6 looking for System.ComponentModel Version 6.0.0.0?

I am deploying an Azure Function called "Bridge" to Azure, targeting .NET 6. The project is referencing a class library called "DBLibrary" that I wrote, and that library is targeting .NET Standard 2.1. The Azure Function can be run locally on my PC without runtime errors.

When I publish the Azure Function to Azure, I see in Azure Portal a "Functions runtime error" which says:

Could not load file or assembly 'System.ComponentModel, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

I do not target System.ComponentModel directly, and I don't see a nuget package version 6.0.0 for "System.ComponentModel" available from any nuget feed. Why is the Azure function looking for this version 6.0.0 of System.ComponentModel? If that version does exist, why can't the Azure Function find it?

Here are the relevant parts of the csproj for the "Bridge" Azure Function:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
      <TargetFramework>net6.0</TargetFramework>
      <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    </PropertyGroup>
    <ItemGroup>
      <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
      <PackageReference Include="Microsoft.Extensions.Azure" Version="1.1.1" />
      <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
      <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
    </ItemGroup>
    <ItemGroup>
      <ProjectReference Include="..\DBLibrary\DBLibrary.csproj" />
    </ItemGroup>
</Project>

Here are the relevant sections of the csproj for the "DBLibrary" class library that is referenced by the Azure Function project:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
      <TargetFramework>netstandard2.1</TargetFramework>
      <ApplicationIcon />
      <OutputType>Library</OutputType>
      <StartupObject />
    </PropertyGroup>
    <ItemGroup>
      <PackageReference Include="Azure.Storage.Blobs" Version="12.10.0" />
      <PackageReference Include="Dapper" Version="2.0.123" />
      <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
      <PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
      <PackageReference Include="System.Text.Json" Version="6.0.2" />
    </ItemGroup>
</Project>

I have tried setting _FunctionsSkipCleanOutput to true in the Azure Functions csproj because that was offered as a potential solution to nuget package resolution issues here: https://github.com/Azure/azure-functions-host/issues/7061 That solution did not change the runtime error I saw in the Azure portal.

In my deployment pipeline, I was targeting functions runtime version ~2 for the Azure deployment when I should have been targeting version ~4 to support .NET 6. Changing to target version 4 of the Azure Functions runtime fixed the issue.

To find this setting on a deployed Function App, navigate to the Azure Portal (portal.azure.com) and search for the Function App resource's name there. Under the left navigation bar of the Function App, navigate to Settings > Configuration. After selecting the Configuration section, look for "Function runtime settings" at the top of the right pane to verify the Runtime version is "~4".

The function runtime version differences can be found here: https://learn.microsoft.com/en-us/azure/azure-functions/functions-versions?tabs=in-process%2Cv4&pivots=programming-language-csharp

This is the relevant part of the support table from the link above (as of February 2022):

Version Support Level Description
4.x GA Recommended runtime version for functions in all languages. Use this version to run C# functions on .NET 6.0.

If you have upgraded to .NET 6.0 you need to do the following to get past this error.

  1. Update the cs proj to v4 在此处输入图像描述

  2. Update the configuration in your function app in Azure to below在此处输入图像描述

  3. Update all the nuget packages for the solution

Clean the solution and then rebuild and it should work locally and should also work in Azure

The .net standard you are using 2.1 but, Microsoft.Azure.Functions.Extensions can be support upto .NET Standard 2.0

You should add the below package to your function app and deploy to Azure again.

 dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 5.0.0-rc.2.20475.6

Please refer this GitHub issue and this MICROSOFT BLOG by @ Jeremy for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM