Adding SmartAssembly obfuscation to a Visual Studio Project

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:

  1. Add the .saproj file to your Visual Studio Project
  2. Right click on your project within Solution Explorer and select ‘Unload Project’
  3. Right click on your project within Solution Explorer and select ‘Edit projectname.csproj’
  4. 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=, Culture=neutral, PublicKeyToken=7f465a1c156d4d57" />
 <Target Name="AfterCompile" Condition="'$(Configuration)' == 'Release'">
 <SmartAssembly.MSBuild.Tasks.Build ProjectFile="$(ProjectDir)HelloWorld.saproj" OverwriteAssembly="True" />

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="&quot;C:Program FilesRed GateSmartAssembly; &quot;$(ProjectDir)HelloWorld.saproj&quot; /output=$self" />

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.

Adding SmartAssembly obfuscation to a Visual Studio Project

6 thoughts on “Adding SmartAssembly obfuscation to a Visual Studio Project

  1. Hi Andy; sorry about that. This is a bug in the installer where it doesn’t put the path to SmartAssembly in the 64-bit registry key, only in the 32-bit WoW key. I’m guessing your msbuild task is running as 64-bit, which is why the task can’t find SmartAssembly exe.

    This will be fixed in the next release; in the meantime, you can manually add the necessary registry key to HKLMSoftwareRed GateSmartAssembly 6, name ‘Path’, string data ‘C:Program FilesRed GateSmartAssembly 6’ (or wherever the SmartAssembly.exe file is on your system).

    1. Ah! Okay, yes, that’s exactly the case – we’re mostly doing SharePoint 2010 development, which is all 64-bit. Good to know about the registry key, I might need to give that a try.

      My colleagues were using SmartAssembly with a SharePoint 2007 project, which was 32-bit, so that’s probably why it worked for them!

      Look forward to the update!

  2. Chris says:

    Thanks for this article, I am having some issues with building WSP using VS and also if using WSPBuilder, the WSP is not built correctly and only shows dll and maniifext, not the element files.. Is there anything I am missing? Thanks alot for any help!

  3. Hmm. Are your Features, etc. under a 12TEMPLATE or 14TEMPLATE folder within your visual studio project. That would be the first thing to check.

  4. Chris says:

    Hi Andy, thanks so much for your reply, that helped me figure it out I think I also just needed some sleep… I realized the WSPBuilder uses different method to map folders, as I think it needed the “14” whereas the msbuild just uses the ‘mapped’ folder automatically (thanks for your clue to that!). After mucking around I realized the WSPBuilder just failed to find the layouts and images mapped folders I had.. So in the end I am going to try to stick to msbuild to do this and use your method or the one by red gate to integrate and obfuscate.. all is well again (until the next issue), thank you soo much!
    Chris A.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.