How to intercept method calls to interface defined in 3rd party assembly

ondrej.maler's Avatar

ondrej.maler

09 Feb, 2018 11:18 AM

Hi.

I am having problems to create working example with intercepting calls to interface defined in 3rd party assembly (external assembly). I am able to intercept method calls to class defined in 3rd party assembly but not to the interface.

Example is included in attachment (VS 2017, Postsharp 5.0.44).

In the PostSharpTest.AspectedClass.Test method I would like to intercept calls to IFaceProvider.IInterface. Intercepting of IFaceProvider.Implementation is working.

Thanks.

Ondrej

  1. Support Staff 1 Posted by PostSharp Techn... on 09 Feb, 2018 02:59 PM

    PostSharp Technologies's Avatar

    Dobrý den Ondřeji,

    this looks like a bug.

    The work-around is to use an aspect provider. (See http://doc.postsharp.net/iaspectprovider.)

    In case of the attached project, the provider can look like this:

    [PSerializable]
    public class MyAspectProvider : MethodLevelAspect, IAspectProvider
    {
        public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
        {
            yield return new AspectInstance(targetElement, new TestMethodInterceptionAspect());
        }
    }
    

    Then you apply this aspect instead of the TestMethodInterceptionAspect aspect.

    I've filed the bug internally as issue #15872. We will contact you as soon as the bug fix has been released.

    For more details on our support policies and prioritization of bug fixes, please visit https://www.postsharp.net/support/policies.

    Best regards,
    -tony

  2. Support Staff 2 Posted by PostSharp Techn... on 09 Feb, 2018 04:29 PM

    PostSharp Technologies's Avatar

    Hello again,

    we have reviewed the issue #15872. This use case unsupported at the moment, so you'll need to keep using the aspect provider. Does this solution work for you?

    Best regards,
    -tony

  3. 3 Posted by ondrej.maler on 12 Feb, 2018 05:19 PM

    ondrej.maler's Avatar

    Hi.

    Your proposed solution works for me.

    Only thing is, that it raises warning LA0183 while building.

    Best regards,

    Ondrej Maler

  4. Support Staff 4 Posted by PostSharp Techn... on 14 Feb, 2018 04:46 PM

    PostSharp Technologies's Avatar

    Hello,

    this warning tells you that you can get unexpected behavior while applying the aspect on external virtual method. Main reason is, that it is applied only on call sites where Postsharp identifies the method as the one being called. It may be possible to call the method in a way that is not intercepted.

    Best regards,
    Daniel

  5. Support Staff 5 Posted by PostSharp Techn... on 20 Feb, 2018 04:06 PM

    PostSharp Technologies's Avatar

    Hello,

    We are going to close this request as there have not been any further updates. Please feel free to reopen the discussion if you need more help.

    Thanks,
    PostSharp Team

  6. PostSharp Technologies closed this discussion on 20 Feb, 2018 04:06 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac