2015-05-13 43 views
0

我最近更新PostSharp到v4.1.13,我已經開始收到這個錯誤,當我嘗試建立我的解決方案:Postsharp MethodInterceptionAspect屬性拋出EntrypointNotFoundException

自定義屬性「True.Kentico.Caching.KenticoCacheAttribute的構造函數拋出EntryPointNotFoundException異常:未找到入口點。

該屬性實現緩存,並已工作過,所以我想知道是什麼原因打破。我在引用方面的包含程序集的其他程序集中使用此屬性,並且在那裏也得到了該錯誤。

以下是該屬性的代碼。道歉,它包含許多構造函數。

[Serializable] 
public class KenticoCacheAttribute : MethodInterceptionAspect 
{ 
    public int CacheMinutes { get; set; } 

    /// <summary> 
    /// the string value of the cache dependency key. If it uses parameters from the method, include a {0} to format with the method parameter 
    /// </summary> 
    public string CacheDependency { get; set; } 
    public string[] CacheDependencyStrings { get; set; } 

    public KenticoCacheDependencyObtainFrom ObtainCacheDependencyFrom { get; set; } 

    /// <summary> 
    /// whether caching is enabled - default from app settings 
    /// </summary> 
    public bool CacheEnabled { get; set; } 

    /// <summary> 
    /// this is the index of the parameter that will be used to format the cachedependency, if required 
    /// </summary> 
    public int CacheDependencyParameterIndex { get; set; } 


    public string CacheDependencyObjectProperty { get; set; } 

    private string _methodName; 

    /// <summary> 
    /// initializes the cache attribute to use a static dependency 
    /// </summary> 
    /// <param name="cacheDependency"></param> 
    public KenticoCacheAttribute(string cacheDependency) 
    { 
     CacheMinutes = EngineContext.Current.Resolve<AppSettings>().Cache.TTL.GlobalSetting; 
     CacheEnabled = !EngineContext.Current.Resolve<AppSettings>().Cache.IgnoreCache; 
     CacheDependencyParameterIndex = -1; 
     CacheDependency = cacheDependency; 
     if (String.IsNullOrEmpty(cacheDependency)) 
      ObtainCacheDependencyFrom = KenticoCacheDependencyObtainFrom.NoDependency; 
     else 
     { 

      ObtainCacheDependencyFrom = KenticoCacheDependencyObtainFrom.Static; 
     } 
    } 

    /// <summary> 
    /// initializes the cache attribute to use a static dependency using multiple depndencies 
    /// </summary> 
    /// <param name="cacheDependencystrings">an array of strings</param> 
    public KenticoCacheAttribute(string[] cacheDependencystrings) 
    { 
     CacheMinutes = EngineContext.Current.Resolve<AppSettings>().Cache.TTL.GlobalSetting; 
     CacheEnabled = !EngineContext.Current.Resolve<AppSettings>().Cache.IgnoreCache; 
     CacheDependencyParameterIndex = -1; 
     CacheDependencyStrings = cacheDependencystrings; 
     ObtainCacheDependencyFrom = KenticoCacheDependencyObtainFrom.MultipleDependenciesStatic; 
    } 

    /// <summary> 
    /// initializes the cache attribute to take the dependency from the input parameter 
    /// </summary> 
    /// <param name="cacheDependency">the static string to be used for the cache dependency. you may include {CurrentSiteName} to be replaced with the current site name, and {0} to be replaced with the value passed in as one of the method parameters</param> 
    /// <param name="cacheDependencyParameterIndex">the index of the parameter in the method parameters that will be used to create the cache dependency key</param> 
    public KenticoCacheAttribute(string cacheDependency, int cacheDependencyParameterIndex) 
    { 
     CacheMinutes = EngineContext.Current.Resolve<AppSettings>().Cache.TTL.GlobalSetting; 
     CacheEnabled = !EngineContext.Current.Resolve<AppSettings>().Cache.IgnoreCache; 
     CacheDependencyParameterIndex = cacheDependencyParameterIndex; 
     CacheDependency = cacheDependency; 
     if (String.IsNullOrEmpty(cacheDependency)) 
     { 

      ObtainCacheDependencyFrom = KenticoCacheDependencyObtainFrom.MultipleDependenciesFromParameter; 
     } 
     else 
     { 

      ObtainCacheDependencyFrom = KenticoCacheDependencyObtainFrom.FromMethodParameter; 
     } 
    } 

    /// <summary> 
    /// initialize the cache attribute to obtain the cache dependency from the named property of the return object 
    /// </summary> 
    /// <param name="cacheDependency">the static string to be used for the cache dependency. you may include {CurrentSiteName} to be replaced with the current site name, and {0} to be replaced with the value in the named parameter </param> 
    /// <param name="cacheDependencyObjectPropertyName">the name of the property of the return object that will be used to replace the placeholder in the static string to build the cache dependency string</param> 
    public KenticoCacheAttribute(string cacheDependency, string cacheDependencyObjectPropertyName) 
    { 
     CacheMinutes = EngineContext.Current.Resolve<AppSettings>().Cache.TTL.GlobalSetting; 
     CacheEnabled = !EngineContext.Current.Resolve<AppSettings>().Cache.IgnoreCache; 
     CacheDependencyParameterIndex = -1; 
     CacheDependency = cacheDependency; 
     ObtainCacheDependencyFrom = KenticoCacheDependencyObtainFrom.FromReturnObject; 
     CacheDependencyObjectProperty = cacheDependencyObjectPropertyName; 
    } 

    /// <summary> 
    /// initialize the cache attribute to obtain the cache dependency from the named property of the return object 
    /// </summary> 
    /// <param name="cacheDependency">the static string to be used for the cache dependency. you may include {CurrentSiteName} to be replaced with the current site name, and {0} to be replaced with the value in the named parameter </param> 
    /// <param name="cacheDependencyParameterIndex"></param> 
    /// <param name="cacheDependencyObjectPropertyName">the name of the property of the return object that will be used to replace the placeholder in the static string to build the cache dependency string</param> 
    public KenticoCacheAttribute(string cacheDependency, int cacheDependencyParameterIndex, string cacheDependencyObjectPropertyName) 
    { 
     CacheMinutes = EngineContext.Current.Resolve<AppSettings>().Cache.TTL.GlobalSetting; 
     CacheEnabled = !EngineContext.Current.Resolve<AppSettings>().Cache.IgnoreCache; 
     CacheDependencyParameterIndex = cacheDependencyParameterIndex; 
     CacheDependency = cacheDependency; 
     ObtainCacheDependencyFrom = KenticoCacheDependencyObtainFrom.FromMethodParameterObjectProperty; 
     CacheDependencyObjectProperty = cacheDependencyObjectPropertyName; 
    } 

    public override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo) 
    { 
     _methodName = method.Name; 
    } 

    public override void OnInvoke(MethodInterceptionArgs args) 
    { 
     var methodInfo = args.Method as MethodInfo; 
     if (methodInfo != null && (methodInfo.ReturnType != typeof(void) && CacheEnabled)) 
     { 
      var cacheKey = BuildCacheKey(args.Arguments); 

      var cacheSettings = new CacheSettings(CacheMinutes, cacheKey); 
      var data = CacheHelper.Cache(cs => GetData(cs, args), cacheSettings); 
      args.ReturnValue = data; 
     } 
     else 
      base.OnInvoke(args); 
    } 

    private object GetData(CacheSettings cs, MethodInterceptionArgs args) 
    { 
     var data = args.Invoke(args.Arguments); 

     // Checks whether data was loaded and whether the data should be cached (based on the CacheSettings) 
     if ((data != null) && cs.Cached) 
     { 
      // Sets a cache dependency for the data 
      // The data is removed from the cache if the objects represented by the dummy key are modified (all user objects in this case) 
      var dependencyResolver = CacheDependencyFactory.GetDependecyFormatter(ObtainCacheDependencyFrom); 

      var dependencyString = dependencyResolver.Format(new CacheDependencyFormatParameters() 
      { 
       CacheDependencyBase = CacheDependency, 
       CacheDependencybaseString = CacheDependencyStrings, 
       InputParameterIndex = CacheDependencyParameterIndex, 
       ReturnParameterName = CacheDependencyObjectProperty, 
       InputParameterData = args, 
       ReturnParameterData = data 
      }); 

      cs.CacheDependency = CacheHelper.GetCacheDependency(dependencyString); 
     } 



     return data; 
    } 

    private string BuildCacheKey(Arguments arguments) 
    { 
     var sb = new StringBuilder(); 
     sb.Append(_methodName); 
     foreach (var argument in arguments.ToArray()) 
     { 
      sb.Append(argument == null ? "_" : argument.ToString()); 
     } 
     sb.Append(String.Format("{0}_{1}", SiteContext.CurrentSiteName, 
      SiteContext.CurrentSite.DefaultVisitorCulture)); 
     return sb.ToString(); 
    } 
} 

任何幫助將不勝感激!

+0

提供代碼在這裏 – Kushal

+0

EngineContext.Current如何初始化? Aspect構造函數作爲PostSharp進程的一部分執行 - 這意味着構建時間。該方面被序列化,然後在運行時反序列化。如果EngineContext是從包含KenticoCacheAttribute的相同程序集初始化的,那麼它可以在構建時使用。請在哪個版本中您的方面工作正常? –

+0

經過審查,我不確定它打破了哪裏。 EngineContext正在web應用程序的app_start中初始化,KenticoCache屬性在發生這種情況之前正試圖從enginecontext中解析出某些內容。 – gabador

回答

0

因此,原來的問題是與代碼在構造這些行:

CacheMinutes = EngineContext.Current.Resolve<AppSettings>().Cache.TTL.GlobalSetting; 
CacheEnabled = !EngineContext.Current.Resolve<AppSettings>().Cache.IgnoreCache; 

當PostSharp運行時,dependencyInjection框架尚未初始化,所以執行失敗。這很奇怪,它之前工作,但嘿。我搬到這個代碼到OnInvoke方法,當DI Enginecontext已經初始化應該只運行:

public override void OnInvoke(MethodInterceptionArgs args) 
    { 

     CacheMinutes = EngineContext.Current.Resolve<AppSettings>().Cache.TTL.GlobalSetting; 
     CacheEnabled = !EngineContext.Current.Resolve<AppSettings>().Cache.IgnoreCache; 
     var methodInfo = args.Method as MethodInfo; 
...... 
相關問題