VS Code中无法运行基础Azure Function的问题求助
按下F5运行基于.NET 8.0 LTS创建的Azure Function应用时,出现以下错误:
C:\Users\Alex .nuget\packages\microsoft.azure.functions.worker.sdk\2.0.0\build\Microsoft.Azure.Functions.Worker.Sdk.targets(136,5): error MSB4018: The "GenerateFunctionMetadata" task failed unexpectedly.
[C:\Repos\k.Integration.ocommMessages\k.Integration.ocommMessages.csproj]C:\Users\Alex.nuget\packages\microsoft.azure.functions.worker.sdk\2.0.0\build\Microsoft.Azure.Functions.Worker.Sdk.targets(136,5):
error MSB4018: System.IO.IOException: The process cannot access the file '?\C: \Repos\k.Integration.ocommMessages\obj\Debug\net8.0\WorkerExtensions' because it is being used by another process. [C:\Repos\k.Integration.ocommMessages\k.Integration.ocommMessages.csproj]C:\Users\Alex.nuget\packages\microsoft.azure.functions.worker.sdk\2.0.0\build\Microsoft.Azure.Functions.Worker.Sdk.targets(136,5): error MSB4018:
at System.IO.FileSystem.RemoveDirectoryInternal(String fullPath, Boolean topLevel, Boolean allowDirectoryNotEmpty) [C:\Repos\k.Integration.ocommMessages\k.Integration.ocommMessages.csproj]
C:\Users\Alex.nuget\packages\microsoft.azure.functions.worker.sdk\2.0.0\build\Microsoft.Azure.Functions.Worker.Sdk.targets(136,5): error MSB4018:
at System.IO.FileSystem.RemoveDirectoryRecursive(String fullPath, WIN32_FIND_DATA& findData, Boolean topLevel) [C:\Repos\k.Integration.ocommMessages\k.Integration.ocommMessages.csproj]
C:\Users\Alex.nuget\packages\microsoft.azure.functions.worker.sdk\2.0.0\build\Microsoft.Azure.Functions.Worker.Sdk.targets(136,5): error MSB4018:
at System.IO.FileSystem.RemoveDirectory(String fullPath, Boolean recursive) [C:\Repos\k.Integration.ocommMessages\k.Integration.ocommMessages.csproj]
C:\Users\Alex.nuget\packages\microsoft.azure.functions.worker.sdk\2.0.0\build\Microsoft.Azure.Functions.Worker.Sdk.targets(136,5): error MSB4018:
at Microsoft.Azure.Functions.Worker.Sdk.ExtensionsCsprojGenerator.RecreateDirectory(String directoryPath) in D:\a_work\1\s\sdk\Sdk\ExtensionsCsprojGenerator.cs:line 54 [C:\Repos\k.Integration.ocommMessages\k.Integration.ocommMessages.csproj]
C:\Users\Alex.nuget\packages\microsoft.azure.functions.worker.sdk\2.0.0\build\Microsoft.Azure.Functions.Worker.Sdk.targets(136,5): error MSB4018:
at Microsoft.Azure.Functions.Worker.Sdk.ExtensionsCsprojGenerator.Generate() in D:\a_work\1\s\sdk\Sdk\ExtensionsCsprojGenerator.cs:line 46 [C:\Repos\k.Integration.ocommMessages\k.Integration.ocommMessages.csproj]
C:\Users\Alex.nuget\packages\microsoft.azure.functions.worker.sdk\2.0.0\build\Microsoft.Azure.Functions.Worker.Sdk.targets(136,5): error MSB4018:
at Microsoft.Azure.Functions.Worker.Sdk.Tasks.GenerateFunctionMetadata.Execute() in D:\a_work\1\s\sdk\Sdk\Tasks\GenerateFunctionMetadata.cs:line 53 [C:\Repos\k.Integration.ocommMessages\k.Integration.ocommMessages.csproj]
C:\Users\Alex.nuget\packages\microsoft.azure.functions.worker.sdk\2.0.0\build\Microsoft.Azure.Functions.Worker.Sdk.targets(136,5): error MSB4018:
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [C:\Repos\k.Integration.ocommMessages\k.Integration.ocommMessages.csproj]
C:\Users\Alex.nuget\packages\microsoft.azure.functions.worker.sdk\2.0.0\build\Microsoft.Azure.Functions.Worker.Sdk.targets(136,5): error MSB4018:
at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [C:\Repos\k.Integration.ocommMessages\k.Integration.ocommMessages.csproj]
我创建了新的Function应用,添加了.gitignore和几个C#类,尝试运行时出现上述错误。已重启VS Code和电脑,但问题依旧。原本无问题的其他Azure Function项目在VS Code中也出现相同错误,但该项目可在Visual Studio 2022中正常运行。
已尝试操作
dotnet add package Microsoft.Azure.Functions.Worker.Sdk --prerelease dotnet outdated dotnet tool install --global dotnet-outdated dotnet outdated --update
当前项目.csproj配置
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <AzureFunctionsVersion>v4</AzureFunctionsVersion> <OutputType>Exe</OutputType> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <FrameworkReference Include="Microsoft.AspNetCore.App" /> <!-- Application Insights isn't enabled by default. See https://aka.ms/AAt8mw4. --> <!-- <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" /> --> <!-- <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="2.0.0" /> --> <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.0.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.0.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.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> <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" /> </ItemGroup> </Project>
解决方案
- 终止占用文件的进程:打开任务管理器,结束所有
dotnet、func相关进程,再重新运行项目。 - 手动清理构建目录:删除项目根目录下的
obj和bin文件夹,执行dotnet clean && dotnet build后再按F5启动。 - 调整Worker SDK版本:当前使用的2.0.0版本存在文件占用问题,尝试切换到1.16.0稳定版:
dotnet add package Microsoft.Azure.Functions.Worker.Sdk --version 1.16.0 - 更新VS Code Azure Functions扩展:打开VS Code扩展面板,找到Azure Functions扩展,更新到最新版本;若问题依旧,可回退到之前的稳定版本。
- 禁用VS Code增量构建:在VS Code设置中搜索
dotnet build,关闭增量构建相关选项,改用终端手动执行完整构建后启动。
内容的提问来源于stack exchange,提问作者Alex Gordon

