12 comments on “Applying aspects to 3rd party assemblies using PostSharp

  1. Gleb, you can use this approach for MVC assemblies, yes. Obfuscated assemblies will most likely not work depending on what you’re trying to do. Sometimes the meta data is far too scrambled to be useful and PostSharp will error out. Please give it a try though.

  2. This looks exactly like what I want to do. I tried it out using this exact example (PostSharp is now at version, but it doesn’t work. The aspect and aspect provider classes build, and the CIL executable runs without apparent problem.

    The resulting code contains an ExampleClass.z__Aspects class, but it’s empty. And the disassembled methods don’t contain any aspecting code whatsoever.

    Any idea what might be going wrong here?

    I may be missing a parameter or two, but the CIL executable doesn’t seem to be documented, at least to the degree where the parameter options are enumerated.


    • Mystery solved. I ended up passing an object derived from IAspect (which is empty) instead of IOnMethodBoundaryAspect.
      It works quite nicely. Thanks very much.

  3. Pingback: planetgeek.ch » Unleash your code with PostSharp

  4. Hi when i am running your code i am getting following error as

    MyProviders.TraceAspectProvider’ does not implement interface member ‘PostSharp.Aspects.IAspectProvider.ProvideAspects(object)’. ‘MyProviders.TraceAspectProvider.ProvideAspects(object)’ cannot implement ‘PostSharp.Aspects.IAspectProvider.ProvideAspects(object)’ because it does not have the matching return type of ‘System.Collections.Generic.IEnumerable’.

    Do not know how to proceed….

    Waiting for your response

  5. Using the generic type ‘System.Collections.Generic.List’ requires 1 type arguments

    This error is also i am getting

  6. Pingback: Inject Aspect to a method using PostSharp IAspectProvider

  7. Hi,
    First I would like to thank you for this great article.
    I am currently working on 3rd party office PIA’s. I want to apply aspects to these assemblies
    for mainly logging the errors as described in this blog.
    this functionality is undocumented and not officially supported by postsharp.
    I am not sure about what kind of risks it may cause.
    I know the risk will depend on my PIA’s. What would be the general risks I need to consider before following this approach?

    Little background on my approach:
    MyProviders is very simple. I am only interested in OnException(MethoExceptionArgs args).

    public class SomeTracingAspect : IOnMethodBoundaryAspect
    public void OnEntry(MethodExecutionArgs args)


    public void OnException(MethodExecutionArgs args)

    myMessageBox custMsg = new myMessageBox( args.Exception.Message);


    public void OnExit(MethodExecutionArgs args)


    public void OnSuccess(MethodExecutionArgs args)

    public void RuntimeInitialize(MethodBase method)


    MyMessageBox class has these functionalities
    2.emails the error and stores in the database.


  8. Hello!
    I’m trying to apply aspects to 3rd party assemblies using PostSharp as defined in https://programmersunlimited.wordpress.com/2011/07/27/applying-aspec
    But when I try to run postsharp.4.0-x64-cil.exe as follows
    C:\ProgramData\PostSharp\3.1.46\bin.Release\postsharp.4.0-x64-cil.exe “DummyApp.exe” /P:Output=output\DummyApp.exe /Attach
    I get “Unhandled exception occurred in postsharp.4.0-x64-cil.exe”.
    I’m using PostSharp 3.1.46 on Windows 7 64bit and .NET 4.5
    I’m attaching the DummyApp but it is completely dummy and defined exactly as specified in the article.
    Thanks in advance for your help,

  9. Hi , when i try to run

    @”C:\Program Files (x86)\PostSharp 2.1\Release\postsharp.4.0-x64-cil.exe” “DummyApp.exe” /p:AspectProviders=MyProviders.TraceAspectProvider,MyProviders /p:Output=output\DummyApp.exe /attach
    @copy /y MyProviders.dll .\Output
    @copy /y PostSharp.dll .\Output

    I am getting this error :

    POSTSHARP : warning PS0131: The module ‘Example.API.dll’ does not contain any as
    pect or other transformation. For improved build-time performance, consider disa
    bling PostSharp for this module by setting the compilation symbol (aka constant)
    ‘SkipPostSharp’ in your project, or set the MSBuild property ‘SkipPostSharp=Tru
    POSTSHARP : error PS0152: Cannot load the external aspect provider’InternalToPub
    lic.AspectProvider,InternalToPublic’ because the type does not implement the int
    erface IAspectProvider.
    POSTSHARP : error PS0060: The processing of module “Example.API.dll” was not suc

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s