【问题标题】:Azure Function V3 Could not load file or assembly 'Microsoft.Extensions.DependencyModel, Version=3.1.6.0Azure Function V3 无法加载文件或程序集“Microsoft.Extensions.DependencyModel,版本=3.1.6.0
【发布时间】:2021-09-07 13:39:07
【问题描述】:

我们正在尝试使用 DependencyContext.Default.RuntimeLibraries 来获取我们所有的项目程序集并将我们想要的类型加载到 ServiceCollection 中。此代码在 asp.net 核心 Web 应用程序上运行良好,但在本地启动我们的 Azure Function 项目之一时,我们收到以下错误:

System.IO.FileNotFoundException:'无法加载文件或程序集'Microsoft.Extensions.DependencyModel,版本 = 3.1.6.0,文化 = 中性,PublicKeyToken = adb9793829ddae60'。系统找不到指定的文件。'

我们的函数配置如下:

<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>

该包是通过解决方案中的另一个项目引用的,我们也尝试过直接引用,但没有成功。

有人知道这个问题的解决方法/解决方法吗?

【问题讨论】:

  • 可能是绑定重定向问题。也许this 有帮助。

标签: c# dependency-injection azure-functions


【解决方案1】:

谢谢@Steven,将您的评论作为答案发布可能会对其他社区成员有所帮助。 在local.settings.json 文件中添加定义绑定重定向的设置。

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

在 .csproj 文件中添加以下行(AutoGenerateBindingRedirects 和 GenerateBindingRedirectsOutputType)

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

我们需要在 Azure 门户中添加应用设置。 转到 Azure 门户 --> 功能应用程序 --> 单击应用程序设置。 为 BindingRedirects 添加一个条目,并添加与本地设置文件相同的值。

【讨论】:

    【解决方案2】:

    在函数的.csproj文件中添加函数保留依赖

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

    【讨论】:

      猜你喜欢
      • 2021-06-26
      • 1970-01-01
      • 2020-04-28
      • 2020-07-01
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      相关资源
      最近更新 更多