1

最近我发现了惊人的 AOP 框架PostSharp,它似乎可以直接开箱即用。一切都很好,我还使用 log4net 实现了自定义日志记录方面,直到我决定将我的应用程序上传到共享主机上。

它不适用于我的共享主机。在使用自定义方面的页面上,我收到以下错误:

Sorry, an error occurred while processing your request.

我想知道是否需要调整一些东西才能让 PostSharp 在共享主机上工作?

另外,我怎样才能在失败的页面上抛出有用的异常消息,因为我收到的消息根本没有帮助?

我的方面如下:

[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);     
        } 
    }

如您所见,我已经使用 log4net 注释掉了所有行,只是为了证明问题不是由于 log4net 引起的。好吧,如果我包含 log4net,我会在我可以让 PostSharp 工作后解决的其他问题中运行。因此,即使只有空方法 PostSharp 也无法正常工作。有人可以指出这里缺少什么吗?

我想补充一点,PostSharp 和 log4net 在调试模式下在 localhost 上工作得很好。我没有收到任何错误,并且问题仅发生在共享主机上。

更新

我再次尝试了相同的代码,并将customErrors mode属性设置为offin web.config。我现在得到以下异常:

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

这是否意味着我需要联系共享主机提供商来更改Trust我的应用程序级别?我希望有解决这个问题的方法?

4

1 回答 1

1

您的错误消息显示SecurityException来自BinaryAspectSerializer. 这是用于序列化方面的默认类,您使用[Serializable]属性对其进行标记。

如前所述,这里最好的解决方案是就这个问题联系您的托管服务提供商。

作为一种解决方法,您可以尝试为您的方面使用另一个序列化程序。

如果您[PSerializable]改为将属性应用于您的方面,则PortableFormatter使用 并且不需要完全信任。

[PSerializable]
public class LoggingAspect : OnMethodBoundaryAspect 

您也可以完全避免序列化,但这意味着CompileTimeInitialize在应用程序运行时不使用方法并进行所有方面的初始化,可能会损失一些性能。

在这种情况下,您应该在其方法中使用MsilAspectSerializer并初始化您的方面。RuntimeInitialize这在此处记录:Aspect Serialization

[OnMethodBoundaryAspectConfiguration(SerializerType=typeof(MsilAspectSerializer))]
public class LoggingAspect : OnMethodBoundaryAspect 
于 2013-11-08T17:36:28.133 回答