1

I was trying to add a Permission based Attribute to be used by an existing Windows application built with WPF that I should work on. the idea was to intercept the calls to the Canexecute methods of certain commands and return false -which would disable the buttons- so I made a simple example on a solution where I have added the Nuget Package of Postsharp and override the OnInvoke Method as follows:

[Serializable]
public class PermissionBasedAttribute : MethodInterceptionAspect
{
    public string PermissionName { get; private set; }

    public PermissionBasedAttribute (string permissionName)
    {
        PermissionName = permissionName;
    }

    /// <summary>
    /// Upon invocation of the calling method, the PermissionName is checked.
    /// If no exception is thrown, the calling method proceeds as normal.
    /// If an exception is thrown, the virtual method MethodOnException is called.
    /// </summary>
    /// <seealso cref="MethodOnException(SecurityPermissionException)"/>
    public override void OnInvoke(MethodInterceptionArgs args)
    {
        try
        {
             /*
             * some logic to check the eligibility of the user, in case of
                not authorised an exception should be thrown.
             */
            args.Proceed();
        }
        catch (SecurityPermissionException ex)
        {
            args.ReturnValue = false;
            // MethodOnException(ex);
        }
    }

    /// <summary>
    /// Method can be overridden in a derived attribute class.
    /// Allows the user to handle the exception in whichever way they see fit.
    /// </summary>
    /// <param name="ex">The exception of type SecurityPermissionException</param>
    public virtual void MethodOnException(SecurityPermissionException ex)
    {
        throw ex;
    }
}

anyway , every thing works fine within the small example with different approaches in the simple example like using devexpress or not. but when added to the existing application it does not work at all, to be more precise: the OnInvoke method is never hit, while the Constructor of the Attribute is already being called.

I am not sure what is wrong, but an additional information is that I found out that the existing solution was utilizing Postsharp already for logging aspects. so I have used the exact same version as the one used by the Project which is 4.2.26 by choosing this version from the Nuget Package Manager.

Another thing I have tried is that i have implemented the CompileTimeValidate method and purposefully added a code that should raise an exception upon build. in the Small -Working- example it has raised an exception upon build. while in the Existing application where it has not worked yet. upon build it does not raise any exception !!!.

Update: I am Using at as follow:

[PermissionBasedAttribute("Permission1") ]
public bool CanShowView()
{
return true;
}
user1874288
  • 135
  • 2
  • 11
  • Could you please also describe how you apply your aspect in the application's source code? I suspect that PostSharp cannot find any valid targets for the aspect. For example, if your class doesn't implement CanExecute method, then that method cannot be intercepted. Maybe a better approach for you will be to introduce and override the CanExecute method instead: http://doc.postsharp.net/code-injections – AlexD Apr 27 '18 at 09:26
  • I have updated the question to include an example of how it is used. the can execute is part of the ICommand Interface. It is working already on the simple example. while on the existing application no. Additionally I have checked by the CompileTimeValidation to raise an exception, still it is not even hit. – user1874288 Apr 27 '18 at 10:07
  • Ok, and are you sure that PostSharp is executed on this project? I.e. other aspects such as logging actually do work? You can also check in project properties whether PostSharp is not disabled, and whether there is PostSharp.targets import in the csproj file. PostSharp should also output info in the build log on successful execution. – AlexD Apr 27 '18 at 15:36
  • yes, the logging aspects are working. actually it seems you are sort of right, because when i put this attribute-PermissionBasedAttribute - on a method in the model it was hit normally. but still i could not know why it is being ignored in the can execute case, given it has worked on the same circumstances in the small application. – user1874288 Apr 27 '18 at 18:27
  • @AlexD: Do you have any suggestion on why is the target missed in this case ? I mean i have add the Compile Time Validation to return true always and still the Can Execute is not hit at all? – user1874288 Apr 28 '18 at 13:49
  • Based on the available info I suspect that the issue is related to aspect multicasting. This is a process used by PostSharp to propagate aspects applied on parent elements to child elements (e.g. from class to method). We would need to investigate a full sample that reproduces the issue to find the exact answer. You can also check out the documentation page on multicasting and experiment with various properties: http://doc.postsharp.net/attribute-multicasting – AlexD May 01 '18 at 10:05
  • @AlexD: Thanks So Much for your efforts. It turns out that Post Sharp was disabled in this project !!! I Installed the Post sharp Extension, then went to Post Sharp Section i found that it was disabled, once enabled it worked out fine. – user1874288 May 11 '18 at 01:04

1 Answers1

2

It turns out that Post Sharp was disabled in this project !!! I Installed the Post sharp Extension, then went to Post Sharp Section i found that it was disabled, once enabled it worked out fine

user1874288
  • 135
  • 2
  • 11
  • Same here. In my case I had PostSharp enabled in a referenced base project and thought that this setting would propagate to all referencing projects. Obviously, this is not the case. – Andi Mar 09 '22 at 14:04