Search code examples
postsharp

POSTSHARP: disable postsharp on debug, for Onentry and Onexit functions,


POSTSHARP: How to disable postsharp for Onentry and Onexit functions, basically need to enable/disable these functions for debug mode in web.config


Solution

  • You can disable PostSharp completely for Debug build configuration on the PostSharp page of the project properties. Look for "Disable PostSharp for this configuration" setting at the top of the page.

    If you want to disable only some of the aspects, then you can do so using C# directives around the applied attributes:

    #if !DEBUG
        [MyAspect]
    #endif
        public class MyClass
        // ...
    

    You cannot control the build process with settings in web.config, however. You can use it to control execution at application run-time, and skip some of the aspects this way (but they still will be weaved into your code and invoked at run-time):

    [Serializable]
    public class MyAspect : OnMethodBoundaryAspect
    {
        public override void OnEntry(MethodExecutionArgs args)
        {
            if ( ConfigurationManager.AppSettings["DisableMyAspect"] == "true" )
                return;
            // ...
        }
    }