Reputation: 135
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;
}
Upvotes: 1
Views: 967
Reputation: 135
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
Upvotes: 2