Question

Recently I have discovered amazing AOP framework PostSharp which seemed to work straight out of the box. Everything was fine and I also implemented a custom logging aspect using log4net until I decided to upload my application on a shared host.

It does not work on my shared host. On the page where custom aspect is used I get the following error:

Sorry, an error occurred while processing your request.

I want to find out if there is something that I need to tweak to get PostSharp working on shared hosting?

Also, how can I get a useful exception message thrown on the page where it fails as the message that I am getting does not help at all?

My aspect is as below:

[Serializable]
    public class LoggingAspect : OnMethodBoundaryAspect 
    {
        //Here is the once-per-class call to initialize the log object
        //[NonSerialized]
        //private static readonly ILog log = LogManager.GetLogger("Logger");

        private string methodName;
        private string className;
        private Type declaringType;

        /// <summary> 
        /// Method executed at build time. Initializes the aspect instance. After the execution 
        /// of <see cref="CompileTimeInitialize"/>, the aspect is serialized as a managed  
        /// resource inside the transformed assembly, and deserialized at runtime. 
        /// </summary> 
        /// <param name="method">Method to which the current aspect instance  
        /// has been applied.</param> 
        /// <param name="aspectInfo">Unused.</param> 
        public override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo)
        {
            this.methodName = method.Name;
            this.className = method.DeclaringType.FullName;
            this.declaringType = method.DeclaringType;
        } 

        /// <summary> 
        /// Method invoked before the execution of the method to which the current 
        /// aspect is applied. 
        /// </summary> 
        /// <param name="args">Information about the method being executed.</param> 
        public override void OnEntry(MethodExecutionArgs args)
        {
            //log.Debug(this.className + "." + this.methodName + ": Enter");
        }

        /// <summary> 
        /// Method invoked after successfull execution of the method to which the current 
        /// aspect is applied. 
        /// </summary> 
        /// <param name="args">Information about the method being executed.</param> 
        public override void OnSuccess(MethodExecutionArgs args)
        {
            //log.Debug(this.className + "." + this.methodName + ": Success");
        }

        /// <summary> 
        /// Method invoked after failure of the method to which the current 
        /// aspect is applied. 
        /// </summary> 
        /// <param name="args">Information about the method being executed.</param> 
        public override void OnException(MethodExecutionArgs args)
        {
            //log.Error(this.className + "." + this.methodName + ": Exception " + args.Exception.Message);     
        } 
    }

As you can see that I have commented out all the lines using log4net just to prove that the problem is not due to log4net. Well if I include log4net I run in other problems which I have saved for solving after I can get PostSharp working. So, even with just the empty methods PostSharp is not working. Can someone please point out what is missing here?

I would like to add that both PostSharp and log4net work great on localhost in debug mode. I don't get any errors and the problem occurs only on shared hosting.

Update

I have tried the same code again with customErrors mode attribute set to off in web.config. I now get the following exception:

Exception Details: System.Security.SecurityException: Request failed.

Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file. 

Exception Details: System.Security.SecurityException: Request failed.

Stack Trace: 

[SecurityException: Request failed.]
   PostSharp.Aspects.Serialization.BinaryAspectSerializer..cctor() +0

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18055

Does this mean I need to contact the shared hosting provider to change the Trust level of my application? I hope there is a workaround for this issue?

Était-ce utile?

La solution

Your error message shows that the SecurityException originates from BinaryAspectSerializer. This is the default class that is used to serialize aspects, which you mark with [Serializable] attribute.

The best solution here is to contact your hosting provider regarding this problem, as was mentioned already.

As a workaround you may try to use another serializer for your aspect.

If you apply [PSerializable] attribute to your aspect instead, then the PortableFormatter is used and it doesn't require full trust.

[PSerializable]
public class LoggingAspect : OnMethodBoundaryAspect 

You can also avoid serialization altogether, but that means not using CompileTimeInitialize method and doing all the aspect initialization during application run-time, possibly losing some performance.

In this case you should use MsilAspectSerializer and initialize your aspect in its RuntimeInitialize method. This is documented here: Aspect Serialization

[OnMethodBoundaryAspectConfiguration(SerializerType=typeof(MsilAspectSerializer))]
public class LoggingAspect : OnMethodBoundaryAspect 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top