OnMethodBoundaryAspect on an iterator

karl.richardson's Avatar

karl.richardson

13 Jul, 2017 08:06 AM

I have an OnMethodBoundary aspect on an iterator method. This iterator method is iterated through twice. However, the second time the OnEntry method on the aspect is called the arguments are all null. This is causing the method validation that the aspect is used for to fail. This has only been happening since we updated to 5.0.28 from 4.3.20.

I guess a workaround is to store the collection after the first usage, but we shouldn't need to do that. Is there any new setting we should be using to fix this issue? I know some changes have been made to the OnMethodBoundary area.

  1. Support Staff 1 Posted by PostSharp Techn... on 14 Jul, 2017 11:42 AM

    PostSharp Technologies's Avatar

    Hello,

    The OnMethodBoundary changes in PostSharp 5.0 are mostly targeted to the async methods, however iterator methods may have been affected as well. The big breaking change is that OnMethodBoundary is now applied to state-machine by default (when ApplyToStateMachine is not specified).

    Do I understand correctly that the argument values are lost when you invoke the same iterator method for the second time? We were not able to reproduce a similar issue yet. Could you please provide us with a sample reproduction code?

    -alex

  2. 2 Posted by karl.richardson on 14 Jul, 2017 01:24 PM

    karl.richardson's Avatar

    See example below.

      public class NotNullAspectTest
       {
          public void OnEntry_Iterator_ReturnsNoError()
          {
             // **Arrange**
             IEnumerable<string> result;
             string testText = "text";
    
             result = TestIteratorMethodWithAttribute(testText);
             foreach (var item in result)
             {
                var test3 = 2;
             }
             foreach (var item in result)
             {
                var test4 = 2;
             }
          }
    
          // Test methods***
          [TestAspect]
          private IEnumerable<string> TestIteratorMethodWithAttribute(string TestText)
          {
             yield return "Test1";
             yield return "Test2";
          }
       }
       [PSerializable]
       public sealed class TestAspect : OnMethodBoundaryAspect
       {
          public override void OnEntry(MethodExecutionArgs args)
          {
             var parameterSet = args.Method.GetParameters();
             var test = args.Arguments;
             base.OnEntry(args);
          }
       }
    

    TestAspect.OnEntry will be hit twice. The second time args.Arguments will contain one argument, but it will be null instead of "text". It should be "text" both times, this was correct in previous versions. Without the aspect, both calls to TestIteratorMethodWithAttribute receive the correct argument value.

  3. Support Staff 3 Posted by PostSharp Techn... on 14 Jul, 2017 03:40 PM

    PostSharp Technologies's Avatar

    Hello,

    Thank you for the sample code, we now have identified the source of the issue. The bug (#15262) is filed in our internal bug-tracker and we'll get back to you once it's fixed.

    It looks like the bug has manifested itself after the upgrade because we've changed the default behavior of OnMethodBoundary with state machines. Now the aspect is applied to state machine by default. You can revert back to the previous behavior and disable the state-machine advising in your aspect by setting the protected SemanticallyAdvisedMethodKinds property to None.

            [PSerializable]
            public sealed class TestAspect : OnMethodBoundaryAspect
            {
                public TestAspect()
                {
                    this.SemanticallyAdvisedMethodKinds = SemanticallyAdvisedMethodKinds.None;
                }
               // ...
            }
    

    -alex

  4. Support Staff 4 Posted by PostSharp Techn... on 21 Jul, 2017 12:56 PM

    PostSharp Technologies's Avatar

    Hello,

    We're closing the ticket for now as the bug has been internally filed as issue #15262. 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

    PostSharp Team

  5. PostSharp Technologies closed this discussion on 21 Jul, 2017 12:56 PM.

  6. PostSharp Technologies re-opened this discussion on 04 Aug, 2017 01:59 PM

  7. Support Staff 5 Posted by PostSharp Techn... on 04 Aug, 2017 01:59 PM

    PostSharp Technologies's Avatar

    Hello,

    The bug #15262 has been fixed in the current release of PostSharp 4.3.36 and 5.0.31.
    Should you need further help with this issue, don't hesitate to re-open this discussion.

    -alex

  8. PostSharp Technologies closed this discussion on 04 Aug, 2017 01:59 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