.netvisual-studio-2015t4shadow-copy

Visual Studio 2015, T4 template: Assembly directive does not use Shadow Copying


When rebuild the solution, I got error message:-

Error       Unable to copy file "obj\Debug\TargetTest.dll" to "bin\Debug\TargetTest.dll". The process cannot access the file 'bin\Debug\TargetTest.dll' because it is being used by another process.    

When I remove the assembly directive, the solution rebuilds:-

<#@ template debug="true" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="C:\\Code\\T4Template\\bin\\Debug\\TargetTest.dll" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Reflection" #>
<#@ output extension=".cs" #>

How to fix this? I guess it is related to shadow copying configuration in visual studio but I could not locate it.


Solution

  • T4 design time template is processed in different App domain under the same process of visual studio. When rebuild the solution Visual Studio tries to replace the referenced DLL, and it cannot replace it because it is still in use.

    I work around this issue by deleting the AppDomain in which T4 template is processed. See msdn