I'm trying to develop a custom MSBuild task and test it by invoking it from another project in the same solution. Originally I set up UsingTask
in the test project to reference the .dll in the task project:
<UsingTask TaskName="FooTask" AssemblyFile="..\Task\bin\$(Configuration)\Task.dll"/>
<Target Name="RunFooTask" BeforeTargets="Compile">
<FooTask/>
</Target>
This mostly works. But frequently and seemingly at random, building the test project leaves the task project .dll locked by MSBuild and/or Visual Studio, which requires killing the rogue MSBuild process and restarting Visual Studio. This sounds similar to Visual Studio 2008 locks custom MSBuild Task assemblies and Visual Studio 2008 locks dll in bin folder and doesn't let go of it. But my task is already an AppDomainIsolatedTask
and I've tried setting GenerateResourceNeverLockTypeAssemblies
. This does not solve the problem.
So I tried adding a project reference from the test project to the target project, thinking it would copy the .dll into the test project and UsingTask
could find it there:
<UsingTask TaskName="FooTask" AssemblyFile="$(OutputPath)Task.dll" />
This seems to solve the problem with the .dll being left locked. The problem now is that the .dll has not been copied yet when the task is invoked. If I understand correctly, BeforeTargets="Compile"
means any time before compile, not necessarily immediately before compile. So I think I need to also specify AfterTargets
with whatever target copies the .dll from the referenced project. What target would that be?
Incidentally, I also tried changing BeforeTargets="Compile"
to BeforeTargets="Build"
. Now the .dll is copied before the task runs, but that's because it runs after the build! I boiled this down to a test target that doesn't involve UsingTask
at all:
<Target Name="Hello" BeforeTargets="Build">
<Message Importance="high" Text="Hello" />
</Target>
Output:
1>------ Rebuild All started: Project: Test, Configuration: Debug Any CPU ------
1> Test -> C:\[redacted]\MSBuildTest2\Test\bin\Debug\Test.exe
1> Hello
This makes me doubt everything I thought I knew about what's going on.