SmartAssembly 6

Using SmartAssembly with ClickOnce and MSI

This page explains how to use SmartAssembly with Microsoft Setup and Deployment projects in Visual Studio (MSI and Microsoft ClickOnce).

If you use SmartAssembly after you have built your project, you will not be able to deploy it using MSI and ClickOnce. This is because SmartAssembly has modified the assembly.

To use MSI or ClickOnce to deploy your assembly, integrate SmartAssembly directly into the build process in MSBuild.

  1. Open your solution in Visual Studio, change the build output path to obj\Release\ (or obj\x86\Release if your platform target is x86) and build the project in release mode.
    You can confirm from the build output window if your project is successfully building to this path.

  2. Create your project using the SmartAssembly project settings page as normal. 

    Make sure that the input assembly path is the \obj\Release folder (or obj\x86\Release if your platform target is x86), and NOT \bin\Release or bin\x86\Release. The location of the destination assembly does not matter.

  3. Open your C#/VB.NET project with an XML editor (or Notepad). 

    The project file looks like the following:

    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     <PropertyGroup>
      ...
     </PropertyGroup>
     ...
     <ItemGroup>
      ...
     </ItemGroup>
     <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
     <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
     Other similar extension points exist, see Microsoft.Common.targets.
      <Target Name="BeforeBuild">
      </Target>
      <Target Name="AfterBuild">
      </Target>
     -->
    </Project>
  4. Add a few lines after the <Import Project> node:

    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    <UsingTask TaskName="SmartAssembly.MSBuild.Tasks.Build" AssemblyName="SmartAssembly.MSBuild.Tasks, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7f465a1c156d4d57" />
    <Target Name="BeforeBuild" Condition=" '$(Configuration)' == 'Release' ">
      <CreateProperty Value="true">
        <Output TaskParameter="Value" PropertyName="RunSmartAssembly"/>
      </CreateProperty>
    </Target>
    <Target Name="AfterCompile" Condition=" '$(RunSmartAssembly)' != '' ">
      <SmartAssembly.MSBuild.Tasks.Build ProjectFile="c:\temp\TestMSBuild\TestMSBuild.saproj" OverwriteAssembly="True" />
    </Target>

    The first XML node adds a reference to SmartAssembly.MSBuild.Tasks.dll, which is installed into the GAC:

    <UsingTask TaskName="SmartAssembly.MSBuild.Tasks.Build" AssemblyName="SmartAssembly.MSBuild.Tasks, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7f465a1c156d4d57" />

    The second XML node tells MSBuild to set the RunSmartAssembly property in the BeforeBuild event, when the project is fully built in release mode:

    <Target Name="BeforeBuild" Condition=" '$(Configuration)' == 'Release' ">
      <CreateProperty Value="true">
        <Output TaskParameter="Value" PropertyName="RunSmartAssembly"/>
       </CreateProperty>
    </Target>

    If the RunSmartAssembly property is set, the last XML node tells MSBuild to protect the assembly in the AfterCompile event:

    <Target Name="AfterCompile" Condition=" '$(RunSmartAssembly)' != '' ">
       <SmartAssembly.MSBuild.Tasks.Build ProjectFile="c:\temp\TestMSBuild\TestMSBuild.saproj" OverwriteAssembly="True" />
    </Target>

You must set the following properties:

ProjectFile

Shows the filename of the SmartAssembly project.

OverwriteAssembly

Set to True if you want to overwrite the original assembly with the obfuscated one. If this option is not set, or set to False, SmartAssembly uses the destination file name from the ProjectFile project.

Other options are:

Input

Specify an input file name, instead of the one from the ProjectFile project.

Output

Specify the output file name for the obfuscated assembly. This property is ignored if the OverwriteAssembly property is set to True.

AssemblyVersion

Use a specific version number for the assembly. This changes the version for the AssemblyName, not the file version.

When outputting an MSI, if you use the Project Output option to embed a file into the program files folder of your deployment project, the embedded file might not be protected inside the MSI. For more information, see Visual Studio Deployment Projects include unprotected builds.

If you have multiple projects

  1. Ensure that your SA project file is stored in the $(SolutionDir)\Build\ folder. It must have the same name as the target assembly. For example, if your assembly is MyTestAssembly.dll then the SmartAssembly project must be named MyTestAssembly.saproj
  2. Create a new file called SmartAssembly.targets in the SmartAssembly installation folder
  3. Paste the following code in the new file:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <UsingTask TaskName="SmartAssembly.MSBuild.Tasks.Build" AssemblyName="SmartAssembly.MSBuild.Tasks, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7f465a1c156d4d57" />
      <Target Name="BeforeBuild" Condition=" '$(Configuration)' == 'Release' ">
        <CreateProperty Value="true">
          <Output TaskParameter="Value" PropertyName="RunSmartAssembly" />
        </CreateProperty>
      </Target>
      <Target Name="AfterCompile" Condition=" '$(RunSmartAssembly)' != '' ">
        <Copy SourceFiles=".\obj\Release\$(TargetFileName)" DestinationFiles=".\obj\Release\$(TargetName).temp$(TargetExt)" />
        <SmartAssembly.MSBuild.Tasks.Build ProjectFile="$(ProjectDir)\$(TargetName).saproj" Input="$(ProjectDir)obj\Release\$(TargetName).exe" OverwriteAssembly="True" />
      </Target>
    </Project>
  4. Edit your C# or VB.NET project with an XML editor (or notepad). Add the following line:

    <Import Project="$(PathToSmartAssembly)\SmartAssembly.targets" />




Didn't find what you were looking for?