C# 使用fsutil硬链接创建而不是慢速复制任务加速msbuild

C# 使用fsutil硬链接创建而不是慢速复制任务加速msbuild,c#,.net,msbuild,C#,.net,Msbuild,我正在尝试加速我们的构建(csharp、msbuild、.NET3.5)。将副本替换为fsutil硬链接创建 以前,我几乎是通过在sln文件上运行脚本并使dll引用private=false,然后让后期构建事件创建硬链接来实现的。问题是不包括可传递依赖项。因此,我认为我需要引用msbuild中的任务,以获取需要硬链接的可传递依赖项 有什么想法吗 此人尝试了同样的方法,但没有发布最终解决方案 需要明确的是:我想要的是保持单独的bin目录,而不是将文件从一个复制到另一个,以创建从源(引用或依赖项)到

我正在尝试加速我们的构建(csharp、msbuild、.NET3.5)。将副本替换为fsutil硬链接创建

以前,我几乎是通过在sln文件上运行脚本并使dll引用private=false,然后让后期构建事件创建硬链接来实现的。问题是不包括可传递依赖项。因此,我认为我需要引用msbuild中的任务,以获取需要硬链接的可传递依赖项

有什么想法吗

此人尝试了同样的方法,但没有发布最终解决方案


需要明确的是:我想要的是保持单独的bin目录,而不是将文件从一个复制到另一个,以创建从源(引用或依赖项)到目标(当前项目的bin)的硬链接。这要快得多,并且提供与复制大致相同的效果。

您是否尝试定义在ResolveAssemblyReferences目标之后运行的自己的目标?ResolveAssemblyReferences运行后,您应该能够使用@(ReferencePath)项来驱动链接创建。考虑实现SeaPraceReleServices,它在微软.Cuff.Objor只是一个空的占位符供您重写。

< P>这在VS 2010中得到支持。但不是2008年。请参阅UseHardLinksIsFable选项,以便在_CopyFilesMarkedCopyLocal中复制

另请参见,了解上下文

重写_CopyFilesMarkedCopyLocal目标。我们在底部的csproj文件中添加了类似的内容:
(在每个完整的nant构建中,它都会自动添加到文件中,并带有nant任务,因此每个项目都会受益)

我们的新目标文件是:

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <UsingTask TaskName="CopyWithHardlinkOption" AssemblyFile="..\lib\TWBuildOptimization\TW.Hardlinker.dll" /> <!-- ============================================================ _CopyFilesMarkedCopyLocal Overridden in order to allow hardlinking with our custom Copy Task. Hardlinking is a major performance improvement. Sometimes 50% of the time compared to copying. Copy references that are marked as "CopyLocal" and their dependencies, including .pdbs, .xmls and satellites. ============================================================ --> <Target Name="_CopyFilesMarkedCopyLocal"> <CopyWithHardlinkOption SourceFiles="@(ReferenceCopyLocalPaths)" DestinationFiles="@(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')" SkipUnchangedFiles="true" UseHardlinksIfPossible="true" OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)"> <Output TaskParameter="DestinationFiles" ItemName="FileWritesShareable"/> </CopyWithHardlinkOption> </Target> </Project> 还必须添加以下内容:

// decompiled from 4.0
internal static class TwNativeMethods
{
    [DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
    internal static extern bool CreateHardLink(string newFileName, string exitingFileName, IntPtr securityAttributes);
}

// decompiled from 4.0
internal static class FileUtilities
{
    internal static void DeleteNoThrow(string path)
    {
        try
        {
            File.Delete(path);
        }
        catch (Exception exception)
        {
            if (ExceptionHandling.NotExpectedException(exception))
            {
                throw;
            }
        }
    }
}

// decompiled from 4.0
internal static class ExceptionHandling
{
    // Methods
    internal static bool IsCriticalException(Exception e)
    {
        return (((e is StackOverflowException) || (e is OutOfMemoryException)) || ((e is ExecutionEngineException) || (e is AccessViolationException)));
    }

    internal static bool NotExpectedException(Exception e)
    {
        return (((!(e is UnauthorizedAccessException) && !(e is ArgumentNullException)) && (!(e is PathTooLongException) && !(e is DirectoryNotFoundException))) && ((!(e is NotSupportedException) && !(e is ArgumentException)) && (!(e is SecurityException) && !(e is IOException))));
    }

    internal static bool NotExpectedReflectionException(Exception e)
    {
        return ((((!(e is TypeLoadException) && !(e is MethodAccessException)) && (!(e is MissingMethodException) && !(e is MemberAccessException))) && ((!(e is BadImageFormatException) && !(e is ReflectionTypeLoadException)) && (!(e is CustomAttributeFormatException) && !(e is TargetParameterCountException)))) && (((!(e is InvalidCastException) && !(e is AmbiguousMatchException)) && (!(e is InvalidFilterCriteriaException) && !(e is TargetException))) && (!(e is MissingFieldException) && NotExpectedException(e))));
    }
}

谢谢,最后我过度浏览了MsBuild的复制本地任务,以创建硬链接,而不是复制引用的传递闭包。如果它能帮助其他人,我希望它能开源。并计划发布更详细的回复,并标记问题的答案。为什么不设置一个到所有bin文件夹的DEVPATH呢。然后,除了关闭copylocal,您不需要做任何事情。有没有办法强制msbuild将文件(或为此创建硬链接)从\obj\debug移动到\bin\debug而不是复制?@ya23我想\obj下的dll/exe与\bin中的不一样。
// decompiled from 4.0
internal static class TwNativeMethods
{
    [DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
    internal static extern bool CreateHardLink(string newFileName, string exitingFileName, IntPtr securityAttributes);
}

// decompiled from 4.0
internal static class FileUtilities
{
    internal static void DeleteNoThrow(string path)
    {
        try
        {
            File.Delete(path);
        }
        catch (Exception exception)
        {
            if (ExceptionHandling.NotExpectedException(exception))
            {
                throw;
            }
        }
    }
}

// decompiled from 4.0
internal static class ExceptionHandling
{
    // Methods
    internal static bool IsCriticalException(Exception e)
    {
        return (((e is StackOverflowException) || (e is OutOfMemoryException)) || ((e is ExecutionEngineException) || (e is AccessViolationException)));
    }

    internal static bool NotExpectedException(Exception e)
    {
        return (((!(e is UnauthorizedAccessException) && !(e is ArgumentNullException)) && (!(e is PathTooLongException) && !(e is DirectoryNotFoundException))) && ((!(e is NotSupportedException) && !(e is ArgumentException)) && (!(e is SecurityException) && !(e is IOException))));
    }

    internal static bool NotExpectedReflectionException(Exception e)
    {
        return ((((!(e is TypeLoadException) && !(e is MethodAccessException)) && (!(e is MissingMethodException) && !(e is MemberAccessException))) && ((!(e is BadImageFormatException) && !(e is ReflectionTypeLoadException)) && (!(e is CustomAttributeFormatException) && !(e is TargetParameterCountException)))) && (((!(e is InvalidCastException) && !(e is AmbiguousMatchException)) && (!(e is InvalidFilterCriteriaException) && !(e is TargetException))) && (!(e is MissingFieldException) && NotExpectedException(e))));
    }
}