Search code examples
c#postsharp

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


I have several projects that rely on PostSharp. very often (more than 50% of the times) my build ends with this error:

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

And the projects mentioned in the error change, sometime it's (e.g.) ProjectA, another time it's SomeOtherProject and so on. Many times this error appears twice or even more for several projects.

I override the LocationInterceptionAspect.CompileTimeValidate method in some places, could it be some collision as a result of it?

I also use a logger that writes to log files (OnMethodBoundaryAspect, OnExceptionAspect), but it has no compile-time methods...

How do I solve it?


Solution

  • The error means that PostSharp is not able to write the output assembly to the disk. This should not happen on its own as MSBuild guarantees that dependent projects are executed after the dependency is finished compiling.

    The most likely case is that you are accessing the assembly outside MSBuild without having proper project dependencies set (see the context menu options for solution in Visual Studio).

    Please check the following:

    1. C#/VB build events.
    2. Custom build steps.
    3. Dynamic assembly loading in aspect methods.

    All of these should not access output files of projects for which the current project does not have a dependency set. If it does, the failures will be random as you are describing them.