.NET 7下Azure Function启动失败:缺失System.ComponentModel程序集
问题:Azure Functions .NET 7环境下无法加载System.ComponentModel程序集
我使用Rider创建Azure Functions应用,通过继承FunctionsStartup的类配置依赖注入(DI),启动调试时应用失败,报错无法加载程序集System.ComponentModel, Version=7.0.0.0。将目标框架切换为.NET 6后,无需额外修改即可正常运行。
尝试安装System.ComponentModel 4.3.0版本,问题依旧;改用Program.cs替代FunctionsStartup配置服务,还是出现相同的缺失错误。
调试日志
C:\Users\Asus.AzureToolsForIntelliJ\AzureFunctionsCoreTools\v4\4.40.0\func.exe host start --pause-on-error Azure Functions Core Tools Core Tools Version: 4.0.5198 Commit hash: N/A (64-bit) Function Runtime Version: 4.21.1.20667 [2023-06-22T13:33:53.116Z] Found C:\Users\Asus\Source\Repos\COMPANY\Sources\COMPANY.Functions.Emails\COMPANY.Functions.Emails.csproj. Using for user secrets file configuration. [2023-06-22T13:33:54.755Z] Error configuring services in an external startup class. [2023-06-22T13:33:54.756Z] Error configuring services in an external startup class. COMPANY.Functions.Emails: Could not load file or assembly 'System.ComponentModel, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. [2023-06-22T13:33:55.322Z] A host error has occurred during startup operation '8cfbbf94-d585-4f40-a284-56fd665093d8'. [2023-06-22T13:33:55.323Z] Microsoft.Azure.WebJobs.Script: Error configuring services in an external startup class. COMPANY.Functions.Emails: Could not load file or assembly 'System.ComponentModel, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. Value cannot be null. (Parameter 'provider') Press any key to continue....[2023-06-22T13:33:56.407Z] Error configuring services in an external startup class. [2023-06-22T13:33:56.408Z] Error configuring services in an external startup class. COMPANY.Functions.Emails: Could not load file or assembly 'System.ComponentModel, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. [2023-06-22T13:33:56.424Z] A host error has occurred during startup operation 'd4774568-11f7-4823-a195-772d543c35e8'. [2023-06-22T13:33:56.425Z] Microsoft.Azure.WebJobs.Script: Error configuring services in an external startup class. COMPANY.Functions.Emails: Could not load file or assembly 'System.ComponentModel, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
初始Startup配置代码
using COMPANY.Functions.Emails; using COMPANY.Functions.Emails.DataAccess; using COMPANY.Functions.Emails.Services; using Microsoft.Azure.Functions.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; [assembly: FunctionsStartup(typeof(Startup))] namespace COMPANY.Functions.Emails { internal class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { var configuration = BuildConfiguration(builder.GetContext().ApplicationRootPath); builder.Services.AddScoped(_ => configuration); builder.Services.AddMediatR(config => config.RegisterServicesFromAssemblyContaining<Startup>()); builder.Services.AddScoped(s => new SqlConnectionFactory(configuration.GetConnectionString("CustomerConnection"), s.GetRequiredService<CustomerIdContainer>())); builder.Services.AddScoped<Repository>(); builder.Services.AddScoped<CustomerIdContainer>(); builder.Services.AddScoped<CustomerSmtpSettingsFactory>(); builder.Services.AddScoped<CustomerEmailClientFactory>(); builder.Services.AddScoped(_ => new BlobClientFactory(configuration.GetConnectionString("BlobStorageConnection"))); } private IConfiguration BuildConfiguration(string applicationRootPath) { var config = new ConfigurationBuilder() .SetBasePath(applicationRootPath) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddJsonFile("settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); return config; } } }
更新:安装System.ComponentModel后的csproj配置
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net7.0</TargetFramework> <AzureFunctionsVersion>V4</AzureFunctionsVersion> <LangVersion>11</LangVersion> </PropertyGroup> <ItemGroup> <PackageReference Include="Azure.Messaging.ServiceBus" Version="7.15.0" /> <PackageReference Include="Azure.Storage.Blobs" Version="12.16.0" /> <PackageReference Include="Dapper" Version="2.0.143" /> <PackageReference Include="FluentEmail.Mailgun" Version="3.0.2" /> <PackageReference Include="MediatR" Version="12.0.1" /> <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" /> <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.11.0" /> <PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.1" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" /> <PackageReference Include="Microsoft.Graph" Version="5.13.0" /> <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" /> <PackageReference Include="System.ComponentModel" Version="4.3.0" /> </ItemGroup> <ItemGroup> <None Update="host.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> <None Update="local.settings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>Never</CopyToPublishDirectory> </None> </ItemGroup> <ItemGroup> <ProjectReference Include="..\COMPANY.Models\COMPANY.Models.csproj" /> </ItemGroup> </Project>
更新:改用Program.cs配置后的代码及csproj
Program.cs
using COMPANY.Functions.Emails.DataAccess; using COMPANY.Functions.Emails.Services; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; var builder = new HostBuilder() .ConfigureFunctionsWorkerDefaults(); builder.ConfigureAppConfiguration(configuration => { configuration .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddJsonFile("settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); }); builder.ConfigureServices((context, services) => { services.AddMediatR(config => config.RegisterServicesFromAssemblyContaining<Program>()); services.AddScoped(s => new SqlConnectionFactory(context.Configuration.GetConnectionString("CustomerConnection"), s.GetRequiredService<CustomerIdContainer>())); services.AddScoped<Repository>(); services.AddScoped<CustomerIdContainer>(); services.AddScoped<CustomerSmtpSettingsFactory>(); services.AddScoped<CustomerEmailClientFactory>(); services.AddScoped(_ => new BlobClientFactory(context.Configuration.GetConnectionString("BlobStorageConnection"))); }); var host = builder.Build(); host.Run();
对应的csproj配置
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net7.0</TargetFramework> <AzureFunctionsVersion>V4</AzureFunctionsVersion> <LangVersion>11</LangVersion> <OutputType>Exe</OutputType> </PropertyGroup> <ItemGroup> <PackageReference Include="Azure.Messaging.ServiceBus" Version="7.15.0" /> <PackageReference Include="Azure.Storage.Blobs" Version="12.16.0" /> <PackageReference Include="Dapper" Version="2.0.143" /> <PackageReference Include="FluentEmail.Mailgun" Version="3.0.2" /> <PackageReference Include="MailKit" Version="4.1.0" /> <PackageReference Include="MediatR" Version="12.0.1" /> <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" /> <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.11.0" /> <PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.1" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" /> <PackageReference Include="Microsoft.Graph" Version="5.13.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.14.1" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.10.0" /> </ItemGroup> <ItemGroup> <None Update="host.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> <None Update="local.settings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>Never</CopyToPublishDirectory> </None> </ItemGroup> <ItemGroup> <ProjectReference Include="..\COMPANY.Models\COMPANY.Models.csproj" /> </ItemGroup> </Project>
内容的提问来源于stack exchange,提问作者Yehor Androsov
相关产品推荐
相关产品推荐

