PostSharp build-time error: "System.IO.IOException: The process cannot access the file 'C:\...\MyProjectOutputDll.dll'"

PostSharp build-time error: "System.IO.IOException: The process cannot access the file 'C:\...\MyProjectOutputDll.dll'"

我有几个项目依赖 PostSharp。很多时候(超过 50% 的时间)我的构建以这个错误结束:

Error   19  Unhandled exception (4.0.34.0, 32 bit, CLR 4.5, Release): System.IO.IOException: The process cannot access the file 'C:\SolutionPath\ProjectPath\obj\Debug\ProjectName.dll' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at ^Up6wxUIUnN6a.^8KD6D2SV(String _0)
   at ^cyC/TYSlPGB/.^wvPm(^RtksFin57NJ8 _0, ^8gP93Xsl\+Imn _1)
   at PostSharp.Sdk.Extensibility.Tasks.CompileTask.Execute()
   at PostSharp.Sdk.Extensibility.Project.ExecutePhase(String phase)
   at PostSharp.Sdk.Extensibility.Project.Execute()
   at PostSharp.Hosting.PostSharpObject.ExecuteProjects()
   at PostSharp.Hosting.PostSharpObject.InvokeProject(ProjectInvocation projectInvocation). C:\SolutionPath\ProjectPath\ProjectFile.csproj  ProjectName

并且错误中提到的项目发生变化,有时是(例如)ProjectA,有时是 SomeOtherProject,依此类推。很多时候这个错误在几个项目中出现两次甚至更多次。

我在某些地方重写了 LocationInterceptionAspect.CompileTimeValidate 方法,会不会因此产生一些碰撞?

我也使用写入日志文件的记录器(OnMethodBoundaryAspectOnExceptionAspect),但它没有编译时方法...

如何解决?

该错误意味着 PostSharp 无法将输出程序集写入磁盘。这不应该单独发生,因为 MSBuild 保证在依赖完成编译后执行依赖项目。

最有可能的情况是您在 MSBuild 外部访问程序集而没有设置适当的项目依赖项(请参阅 Visual Studio 中解决方案的上下文菜单选项)。

请检查以下内容:

  1. C#/VB 生成事件。
  2. 自定义构建步骤。
  3. 方面方法中的动态程序集加载。

所有这些都不应该访问当前项目没有依赖集的项目的输出文件。如果是这样,那么失败将是随机的,正如您所描述的那样。