Introducing PostSharp to your team

PostSharp is AOP (Aspect-Oriented-Programming) framework (www.sharpcrafters.com).
It transparently inserts itself in the build process and post-processes the compiled assembly.

To enable PostSharp in your project you need to download and run the PostSharp installer, and add appropriate references in your project.

However if your project is in SVN, and you are not the only one compiling it (other developers, CI machine) most likely you don’t want to run PostSharp installer on all the machines.

There is a way to introduce PostSharp transparently to your project.

  • Put all the PostSharp files in the LibPostSharp folder of your project and add this folder to SVN.
  • Modify the .csproj file: After the last ItemGroup following xml must be inserted:
  <propertyGroup>
    <dontImportPostSharp>True</dontImportPostSharp>
    <!-- Add the next line if you are using Visual Studio 2010 -->
    <!-- <postSharpUseCommandLine>True</postSharpUseCommandLine>-->
  </propertyGroup>
  • Modify the .csproj file: After the
<import Project="$(MSBuildToolsPath)Microsoft.CSharp.targets" />

following xml must be inserted:

<import Project="..LibPostSharpPostSharp.targets" />

The whole change should be similar to this sample:

</itemGroup>
  <propertyGroup>
    <dontImportPostSharp>True</dontImportPostSharp>
  </propertyGroup>
  <import Project="$(MSBuildToolsPath)Microsoft.CSharp.targets" />
  <import Project="..LibPostSharpPostSharp-1.5.targets" />
</project>

Tags: ,

4 Responses to “Introducing PostSharp to your team”

  1. Gael Fraiteur Says:

    Thank your for this blog.

    I would like to precise that this way of using PostSharp without using it results in slower build time operations because PostSharp is not ngenned. Sounds stupid, but JIT compilation time is not neglictible for an application that is triggers many times per second.

    Another thing:

    You should use True and not False. This ensures that PostSharp targets are not imported twice in case that PostSharp is installed globally on the machine.

    Finally, you way want to use True if you experience OutOfMemory exceptions in Visual Studio.

  2. Gael Fraiteur Says:

    Hm, XML in comments don’t work!!

    So one more time:

    You should use [DontImportPostSharp]True[/DontImportPostSharp]and not [DontImportPostSharp]False[/DontImportPostSharp]. This ensures that PostSharp targets are not imported twice in case that PostSharp is installed globally on the machine.

    Finally, you way want to use [PostSharpUseCommandLine]True[/PostSharpUseCommandLine] if you experience OutOfMemory exceptions in Visual Studio (although this may slow down build if images are not ngenned).

  3. Limilabs support Says:

    Thanks Gael,

    1.
    DontImportPostSharp updated to True

    2.
    Why is PostSharp not ngenned in such case?
    Shouldn’t it be ngenned after first usage?

    3.
    I just realized that in PostSharp 1.5 you should use PostSharp-1.5.targets instead of PostSharp.targets

  4. Gael Fraiteur Says:

    2. Ngen is not automatic. You have to ask (using the ngen utility). Ngenned images are used only when the command-line utility is used (PostSharpUseCommandLine=True).

Questions?

Consider using our Q&A forum for asking any questions.