Azure / azure-functions-host

The host/runtime that powers Azure Functions
https://functions.azure.com
MIT License
1.92k stars 442 forks source link

Unable to use Entity Framework Cosmos with Functions #6949

Closed stimms closed 7 months ago

stimms commented 3 years ago

Creating a new functions project and installing Microsoft.EntityFramworkCore.Cosmos results in a project that is unable to run due to problems with Microsoft.Extensions.Logging.Abstractions.

[2020-11-29T19:49:57.089Z] A host error has occurred during startup operation '50ed1cd8-7515-4b1b-b71f-3dd886666496'.
[2020-11-29T19:49:57.091Z] System.Private.CoreLib: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
Value cannot be null. (Parameter 'provider')

Investigative information

Azure Functions Core Tools Core Tools Version: 3.0.2996 Commit hash: c54cdc36323e9543ba11fb61dd107616e9022bba Function Runtime Version: 3.0.14916.0

It has been reported in other issues that adding

<PropertyGroup>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
</PropertyGroup>

To the project file helps solve some overly aggressive binary cleaning. However adding this simply moves the problem and we now get

[2020-11-29T19:51:59.063Z] Microsoft.Azure.WebJobs.Host: Error indexing method 'Function1'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'log' to type ILogger. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
[2020-11-29T19:51:59.111Z] Error indexing method 'Function1'

Project file now looks like

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>
  <PropertyGroup>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Cosmos" Version="5.0.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.7" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

Updating functions to 3.0.11 does not address this problem nor does moving to .NET 5.

Repro steps

  1. Create a new functions project
  2. Install Microsoft.EntityFrameworkCore.Cosmos
  3. Launch the project

Expected behavior

Project will run and accept Http traffic (for the default http function)

Actual behavior

Everything is broken and things are awful.

Known workarounds

Downgrade to the 3.1.10 version of Microsoft.EntityFramworkCore.Cosmos

AartBluestoke commented 3 years ago

https://github.com/Azure/azure-functions-host/issues/6893#issuecomment-726386105

Consequence: if you need a library that is a direct or indirect dependency of azure functions you can only use the version that is loaded. This is a case where azure functions is incorrectly reporting its hard dependencies.

eg: if you are using azure functions 3.0.7, then you must use a library binary compatible with logging.abstractions 2.1.0 When your code (eg if you have a asp.netcore dependencency) asks for something that is only found in Abstractions >=5.0 the runtime (correctly) responds "sorry, that function isn't there", because all it has is the 2.1.0 dll in memory.

image

Therefore the many tickets that will be raised that are like this can only be solved by upgrading azure functions to a newer version which has a dependency on the 5.x line of the abstractions.

In this particular case, if azure functions used Microsoft.Extensions.Logging.Abstractions 5.0, then it would be fine because that library appears to be backwards compatible so the reverse error doesn't occur (no runtime failure if you expect 2.1 and actually get 5.0).

If that is not true for all libraries in common between azure functions and the libraries your code uses, then you will be pinned in terms of your dependencies and azure functions versions.

satvu commented 7 months ago

Closing as answered/stale.