You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Functions Service Bus触发器本地构建运行时出现MSB3027与MSB3021错误求助

Azure Functions Service Bus触发器本地构建运行时出现MSB3027与MSB3021错误求助

大家好,我在实现Azure Functions的Service Bus触发器并尝试本地构建运行时遇到了构建错误,折腾了好久没解决,来求助一下!

错误日志信息

1> C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(4911,5): error MSB3027: Could not copy "C:\Users\Loc\.nuget\packages\system.security.cryptography.protecteddata\4.5.0\runtimes\win\lib\netstandard2.0\System.Security.Cryptography.ProtectedData.dll" to "bin\Release\net8.0\runtimes\win\lib\netstandard2.0\System.Security.Cryptography.ProtectedData.dll". Exceeded retry count of 10. Failed.
1> C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(4911,5): error MSB3021: Unable to copy file "C:\Users\Loc\.nuget\packages\system.security.cryptography.protecteddata\4.5.0\runtimes\win\lib\netstandard2.0\System.Security.Cryptography.ProtectedData.dll" to "bin\Release\net8.0\runtimes\win\lib\netstandard2.0\System.Security.Cryptography.ProtectedData.dll". Could not find a part of the path 'bin\Release\net8.0\runtimes\win\lib\netstandard2.0\System.Security.Cryptography.ProtectedData.dll'.
1> Done building project "WorkerExtensions.csproj" -- FAILED.

我的项目配置

.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" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.23.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.0.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="2.0.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.0.2" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.23.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.5" />
  </ItemGroup>
</Project>

Service Bus 触发器代码

[Function(nameof(Serviceannouncement))]
public async Task Run(
    [ServiceBusTrigger("mytopic", "mysubscription", Connection = "connstring")] 
    ServiceBusReceivedMessage message, 
    ServiceBusMessageActions messageActions)
{
    _logger.LogInformation("Message ID: {id}", message.MessageId);
    _logger.LogInformation("Message Body: {body}", message.Body);
    _logger.LogInformation("Message Content-Type: {contentType}", message.ContentType);

    // Complete the message
    await messageActions.CompleteMessageAsync(message);
}

解决方案建议

我整理了几个可能解决这个问题的步骤,你可以按顺序尝试:

  1. 手动创建目标目录
    错误提示找不到目标路径的一部分,你可以手动在项目根目录下创建bin\Release\net8.0\runtimes\win\lib\netstandard2.0这个多级目录,然后重新执行构建——有时候MSBuild无法自动生成过深的目录结构。

  2. 清理并重建项目环境

    • 先通过Visual Studio的「清理解决方案」功能清理输出,之后手动删除项目根目录下的bin和obj文件夹;
    • 清理NuGet本地缓存:打开命令提示符执行 dotnet nuget locals all --clear;
    • 重新还原NuGet包:执行 dotnet restore,之后再尝试构建项目。
  3. 排查文件锁定问题
    目标DLL文件可能被其他进程锁定(比如Visual Studio的调试进程、杀毒软件、文件资源管理器),你可以:

    • 关闭Visual Studio后重新打开,再尝试构建;
    • 暂时禁用杀毒软件或者安全工具,排除它们拦截文件写入的可能;
    • 用Process Explorer之类的工具检查是否有进程占用了目标文件,结束相关进程后再构建。
  4. 更新NuGet包版本
    你目前使用的Microsoft.Azure.Functions.Worker(2.0.0)和Microsoft.Azure.Functions.Worker.Sdk(2.0.5)版本比较旧,可能和.NET 8、Service Bus扩展包存在兼容性问题。建议将这些包更新到最新稳定版:

    • 把Microsoft.Azure.Functions.Worker更新到2.19.0+;
    • Microsoft.Azure.Functions.Worker.Sdk更新到2.15.0+;
    • 确保所有Functions相关包的版本相互匹配,更新后重新还原并构建。
  5. 切换构建配置测试
    暂时将构建配置从「Release」切换为「Debug」模式,看是否还会出现相同错误,以此排查是否是Release模式下的特定配置导致的问题。

  6. 手动复制文件
    如果以上方法都无效,可以手动从NuGet包路径C:\Users\Loc\.nuget\packages\system.security.cryptography.protecteddata\4.5.0\runtimes\win\lib\netstandard2.0\System.Security.Cryptography.ProtectedData.dll复制该DLL到目标目录bin\Release\net8.0\runtimes\win\lib\netstandard2.0\,之后再执行构建。

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 07:23:11