.NET Framework Developer Center > .NET Development Forums > MSBuild > MSBuild doesn't release custom tasks assemblies after compile?
Ask a questionAsk a question
 

AnswerMSBuild doesn't release custom tasks assemblies after compile?

  • Monday, January 22, 2007 7:57 PMTomerico Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,

    I've been experimenting with custom tasks, after reading the great article:

    http://msdn.microsoft.com/msdnmag/issues/06/06/InsideMSBuild/default.aspx

     

    My question is:

    I've built 2 projects, 1 containing a custom task, and the other containing this code in the csproj file:

    <Target Name="AfterBuild">

    <CustomTask />

    </Target>

    Everythings works fine the first time I build. But when editting the task and rebuilding, it says:

    Error 1 Unable to copy file "d:\projects\tests\CustomBuild\CustomCompiler\bin\Debug\CustomCompiler.dll" to "bin\Debug\CustomCompiler.dll". The process cannot access the file 'bin\Debug\CustomCompiler.dll' because it is being used by another process.

    I assume this is because <UsingTask> uses Assembly.LoadFrom(), and the AppDomain doesn't get unloaded after compilation.

    Is there any solution for the problem?

     

    Best Regards,

    Tomer

     

Answers

  • Monday, September 22, 2008 2:55 PMDanMoseley - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi there,
    Unfortunately this is because MSBuild loads task assemblies in the primary appdomain. The CLR does not allow assemblies to unload from an appdomain as this allows important optimizations on their part.
    The only workarounds I suggest is to call out to msbuild.exe to build the projects that use the task. To do this, create MSBuild.exe <<your project>> as an external tool in VS.
    Dan
    developer on msbuild
  • Saturday, October 31, 2009 8:51 AMMike FourieModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Try basing your task on the AppDomainIsolatedTask rather. http://msdn.microsoft.com/en-us/library/microsoft.build.utilities.appdomainisolatedtask.aspx

All Replies

  • Tuesday, February 06, 2007 6:05 PMRDollarhide Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I'm encountering the exact same problem.  Has anybody discovered a solution for this?  Right now, I have to close out VS2005 completely and restart it to have my custom task assembly released.

    Regards,

    RDollarhide

  • Thursday, February 08, 2007 4:39 PMDeepu_a Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I had this problem when the dll was registered in the GAC and you do a release and debug build. Then one is not able to deregister the DLL from the GAC.
  • Friday, February 16, 2007 2:09 AMMichaelD_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    "Me too"
  • Friday, February 16, 2007 9:56 AMDeepu_a Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Do you register the assembly for com interop? If so try without it.
  • Friday, February 16, 2007 1:11 PMMichaelD_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    That's the same as ComVisible, correct?  It's currently set to false.

  • Monday, September 01, 2008 6:23 PMRon Klein # Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Same here :-(
  • Monday, September 22, 2008 2:55 PMDanMoseley - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi there,
    Unfortunately this is because MSBuild loads task assemblies in the primary appdomain. The CLR does not allow assemblies to unload from an appdomain as this allows important optimizations on their part.
    The only workarounds I suggest is to call out to msbuild.exe to build the projects that use the task. To do this, create MSBuild.exe <<your project>> as an external tool in VS.
    Dan
    developer on msbuild
  • Friday, October 30, 2009 10:45 AMsgannon Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hey

    Another workaround, as opposed to shutting down VS, is to rename the DLL. But the problem will still remain.
  • Saturday, October 31, 2009 8:51 AMMike FourieModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Try basing your task on the AppDomainIsolatedTask rather. http://msdn.microsoft.com/en-us/library/microsoft.build.utilities.appdomainisolatedtask.aspx
  • Thursday, November 05, 2009 9:47 AMsgannon Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I gave AppDomainIsolatedTask a try but I still seem to be encountering a problem.

    In my scenario in Visual Studio I have ProjectB using ProjectA as a reference. ProjectA contains the task that is used by ProjectB's msbuild. Occasionally when compiling ProjectB I get an error along the lines of "Cannot copy ProjectA.dll to ProjectB/bin/debug/ProjectA.dll as another process is using this file".

    The structure of my task is 

    public class ProjectATask : Microsoft.Build.Utilities.AppDomainIsolatedTask
        {
            private string path = "";
    
            [Required]
            public string Path
            {.........}
    
            public override object InitializeLifetimeService()
            {
                return base.InitializeLifetimeService();
            }
    
            public override bool Execute()
            {..........}
        }
    


    Is there anything else I can do to fix this problem?
  • Thursday, November 05, 2009 1:11 PMMike FourieModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You say 'occasionally'... Can you run processmon to see what has the lock on it. I'm guessing its VS, but you may be lucky and its the indexing / antivirus service, in which case disable that. Maybe you could stick a sleep into the BeforeBuild target in ProjectB.... not tried this. Did the AppDomainIsolatedTask help at all?
  • Saturday, November 07, 2009 4:56 PMsgannon Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The process that has captured it is devenv.exe (part of VS). Sadly AppDomainIsolatedTask does not seem to have helped at all. Normally the dll is grabbed after I modify ProjectA (but not always). The task in question does two things 1) Checks some code in ProjectA to verify that its correct (I'm using reflection to map between two layers instead of a switch statement - hence getting this working would be like using the compiler to verify my code. e.g. Command A maps to Task B - ensure that Command A has a correct string mapping to Task B's method name (dangerous I know - but time saving)). 2) Check a properties file in ProjectB to ensure it has defined all the required keys. If I could get this working it would really put a shine on the project.