When building a .net core application that attempts to uses dotfuscator we can successful build and obfuscate using msbuild.exe however any build or publish using the .net core cli fails.
These issue exist using a local command line and also in our devops build pipelines.
examples
msbuild.exe "the project file.csproj" <- this works and engages dotfuscator
dotnet build "the project file.csproj" <- this throws the error below.
dotnet publish "the project file.csproj" <- also throws the error below.
D:\Repos\PF\tools\dotfuscator\PreEmptive.Dotfuscator.Common.targets(262,5): error MSB4062: The "FindProjectAssemblies" task could not be loaded from the assembly D:\Repos\PF\tools\dotfuscator\PreEmptive.Dotfuscator.Internal.Tasks.dll. Could not load file or assembly 'Microsoft.Build.Utilities.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. [D:\Repos\PF\source\xxxxx.xxxxx.xxxxx.API\xxxxx.xxxxxx.xxxxxx.API.csproj]
Looks very much like this issue. https://github.com/Microsoft/msbuild/issues/2111
Update (2019-12-20): We have released a beta version of Dotfuscator Professional 6.0.0 with support for running Dotfuscator on non-Windows operating systems. This includes support for the dotnet
commands.
Dotfuscator can protect .NET Core assemblies that can then be run under .NET Core (i.e., cross-platform). However, as of this writing, Dotfuscator itself only runs on the classic, Windows-based .NET Framework.
It looks like you have integrated Dotfuscator Professional into your build in the recommended way, which uses Dotfuscator's MSBuild targets and tasks. The instructions include adding this <Import>
to your project file:
<Import Project="$(MSBuildExtensionsPath)\PreEmptive\Dotfuscator\4\PreEmptive.Dotfuscator.Common.targets"/>
When I did this for a .NET Core project, I could build in Visual Studio or via msbuild /t:Publish /p:Configuration=Release
and get a protected app. But if I tried to run dotnet build MyApp.csproj
or dotnet publish MyApp.csproj
, the build failed. I got a different error message than you at first:
error MSB4019: The imported project "C:\Program Files\dotnet\sdk\2.1.503\PreEmptive\Dotfuscator\4\PreEmptive.Dotfuscator.Common.targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.
This is because Dotfuscator does not install its MSBuild targets into the .NET Core location, only the .NET Framework and Visual Studio locations, so the <Import>
above doesn't find the needed files.
If I instead copy the MSBuild files from C:\Program Files (x86)\MSBuild
to my own location, such as C:\DotfuscatorMSBuildCopy
, and change the <Import>
accordingly:
<Import Project="C:\DotfuscatorMSBuildCopy\PreEmptive\Dotfuscator\4\PreEmptive.Dotfuscator.Common.targets"/>
then I see your error:
error MSB4062: The "FindProjectAssemblies" task could not be loaded from the assembly C:\DotfuscatorMSBuildCopy\PreEmptive\Dotfuscator\4\PreEmptive.Dotfuscator.Internal.Tasks.dll. Could not load file or assembly 'Microsoft.Build.Utilities.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.
The solution is to build using msbuild /t:Publish /p:Configuration=Release
instead of dotnet
.
(Note: I work for the Dotfuscator team and am answering in that capacity.)