PostSharp无法使用共享主机

时间:2013-11-07 21:22:17

标签: asp.net asp.net-mvc-4 postsharp

最近,我发现了一个惊人的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属性设置为off中的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级别?我希望这个问题有一个解决方法吗?

1 个答案:

答案 0 :(得分:1)

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

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

作为一种解决方法,您可以尝试使用其他序列化程序。

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

[PSerializable]
public class LoggingAspect : OnMethodBoundaryAspect 

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

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

[OnMethodBoundaryAspectConfiguration(SerializerType=typeof(MsilAspectSerializer))]
public class LoggingAspect : OnMethodBoundaryAspect