We’ve started to use SmartAssembly to obfuscate the code for some of the SharePoint products that we’ve been working on, and I’d had a few problems with it.
SmartAssembly’s manual describes how to include the SmartAssembly project file (.saproj) into your build process. This is basically simple:
- Add the .saproj file to your Visual Studio Project
- Right click on your project within Solution Explorer and select ‘Unload Project’
- Right click on your project within Solution Explorer and select ‘Edit projectname.csproj’
- Scroll down to the bottom of the file – we now need to add some additional lines to the file to tigger obfuscation during the build process.
Add the following lines to the bottom of the file, just before </Project>. You will need to modify the .saproj filename accordingly:
<UsingTask TaskName="SmartAssembly.MSBuild.Tasks.Build" AssemblyName="SmartAssembly.MSBuild.Tasks, Version=22.214.171.124, Culture=neutral, PublicKeyToken=7f465a1c156d4d57" /> <Target Name="AfterCompile" Condition="'$(Configuration)' == 'Release'"> <SmartAssembly.MSBuild.Tasks.Build ProjectFile="$(ProjectDir)HelloWorld.saproj" OverwriteAssembly="True" /> </Target>
This is what worked for my colleagues – but not for my projects. I kept receiving an error:
SmartAssembly build failed: The system cannot find the file specified (Exception from HRESULT: 0x80070002)
There wasn’t much clue what that meant. Looking on the Redgate forums, it seems like I wasn’t the first person with this issue. The suggestion seemed to be to run from the command line, rather than using Redgate’s MSBuild task. That seems a shame, but I tried it:
<Target Name="AfterCompile" Condition="'$(Configuration)' == 'Release' AND '$(BuildingInsideVisualStudio)' != 'true'"> <Exec Command=""C:Program FilesRed GateSmartAssembly 6SmartAssembly.com" "$(ProjectDir)HelloWorld.saproj" /output=$self" /> </Target>
This seemed to work nicely. I know that the command has a fixed path in it, but this command should only be run on the build server (hence the bit about $(BuildingInsideVisualStudio) ). More on why I did that in my next post.