简体   繁体   中英

Azure Function V3 Could not load file or assembly 'Microsoft.Extensions.DependencyModel, Version=3.1.6.0

We are trying to use the DependencyContext.Default.RuntimeLibraries to get all our project assemblies and load the types we want into the ServiceCollection. This code runs fine on asp.net core web application but when starting one of our Azure Function projects locally we get the following error:

System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.Extensions.DependencyModel, Version=3.1.6.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.'

Our Function is configured as below:

<Project Sdk="Microsoft.NET.Sdk">   
<PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <AzureFunctionsVersion>v3</AzureFunctionsVersion>
        <UserSecretsId>...</UserSecretsId>   </PropertyGroup>   
<ItemGroup>
        <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
        <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.5.1" />
        <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="4.3.0" />
        <PackageReference Include="Microsoft.Azure.WebJobs.Host.Storage" Version="4.0.1" />
        <PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.27" />
        <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="3.1.18" />
        <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.13" />
        <PackageReference Include="System.Text.Json" Version="4.7.2" />   
</ItemGroup>
<ItemGroup>
        <None Update="host.json">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
        <None Update="local.settings.json">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
          <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>   
</ItemGroup>   
<ItemGroup>   
</ItemGroup> 
</Project>

The package is referenced through another project in the solution and we also tried referencing directly with no luck.

在此处输入图像描述

Does anyone know a workaround/fix to this problem?

Add the function preserved dependency in.csproj file of the function

<ItemGroup>
    <FunctionsPreservedDependencies Include="Microsoft.Extensions.DependencyModel.dll" />
</ItemGroup>

Thank you @Steven,Posting your Comment as an Answer might help other community members. Add a setting that defines the binding redirect in local.settings.json file.

{    
  "IsEncrypted": false,    
  "Values": {    
  "AzureWebJobsStorage": "UseDevelopmentStorage=true",    
  "BindingRedirects": "[ { \"ShortName\": \"Microsoft.Extensions.DependencyModel\", \"RedirectToVersion\": \"3.1.6.0\", \"PublicKeyToken\": \"adb9793829ddae60\" } ]"    
}    
}

Add the below lines (AutoGenerateBindingRedirects and GenerateBindingRedirectsOutputType) in.csproj file

<PropertyGroup>
 <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

We need to Add app setting in Azure Portal. Go to Azure Portal --> Functions App --> Click on Application Settings. Add an entry for BindingRedirects and add the same value that was added to the local settings file.

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