使用ResourceViewLocationProvider时如何执行运行时视图更新

问题描述 投票:0回答:1

Im runing a nancyfx with owin on centos 6.5 with mono 5.10.0.140, I change the default ViewLocationProvider to ResourceViewLocationProvider for the default ViewLocationProvider causes memory leak of somekind after running for days, and the ResourceViewLocationProvider dont也有同样的问题。我想热门更新视图,就像我们可以用默认的ViewLocationProvider做的那样,但是当谷歌搜索时它似乎是不可能的。

我确实找到了部分解决方案,通过实现自定义ViewLocator和IView Cache,我确实实现了热更新。但除了那些丑陋的静态课外,它没有感觉到

//Here is what I did in the custom IViewLocator
//...class definition fallback viewlocator and other staffs
private static ConcurrentDictionary<string, ViewLocationResult> _cachedViewLocationResults;
//..other code      
    public ViewLocationResult LocateView(string viewName, NancyContext context)
    {
//...lock and others
        if (_cachedViewLocationResults != null && _cachedViewLocationResults.ContainsKey(viewName))
        {
             return _cachedViewLocationResults[viewName];
        }
//...lock and others
       return fallbackViewLocator.LocateView(viewName, context);
   }
//...other class
//here is how I update Views
public static void UpdateCachedView(IDictionary<string, ViewLocationResult> replacements)
{    
    lock (CacheLock)
    {
        if(_cachedViewLocationResults == null)_cachedViewLocationResults = new ConcurrentDictionary<string, ViewLocationResult>();
        foreach (var replace in replacements)
        {
           _cachedViewLocationResults.AddOrUpdate(replace.Key, x=>replacements[x], (x,y)=>y);
        }
   }
}

//结束IViewLocator

//here is what I did in the custom IViewCache
//another static for ViewCache to tell if the view has been updated
public static List<ViewLocationResult> Exceptions { get; private set; }
//...some other code
//here is how I ignore the old cache
public TCompiledView GetOrAdd<TCompiledView>(ViewLocationResult viewLocationResult, Func<ViewLocationResult, TCompiledView> valueFactory)
{
    if (Exceptions.Any(x=>x.Name == viewLocationResult.Name && x.Location == viewLocationResult.Location && x.Extension == viewLocationResult.Extension))
    {
        object old;
        this.cache.TryRemove(viewLocationResult, out old);
        Exceptions.Remove(viewLocationResult);
    }            
    return (TCompiledView)this.cache.GetOrAdd(viewLocationResult, x => valueFactory(x));
}

通过这些实现和引导程序上的一些设置以及用于某些mysql更新的路由器,我可以按照我想要的方式更新View,但问题是:1。现在我必须手动映射所有位置,名称, ViewLocationResult的扩展使用并且有太多它们(243 ...),我想使用某种内置函数来识别变化,比如ViewLocationResult的IsStale函数,但是我确实没有使用它。知道更换它们的更好方法。

有人可以给我一个提示,提前谢谢。

c# razor static-methods nancy
1个回答
0
投票

好吧,我终于弄清楚如何自己做这件事,以防万一其他人想要使用与我相同的方法,以下是如何在内存中更新你的视图:

  1. 建立一个界面
t know which and how...
2. those static class are ugly and I think it could be problematic but I didn
  1. 创建一个新的ViewLocationResultProvider
    public interface INewViewLocationResultProvider
    {
        bool UseCachedView { get; set; }
        ViewLocationResult GetNewerVersion(string viewName, NancyContext context);
        void UpdateCachedView(IDictionary<string, ViewLocationResult> replacements);
    }
  1. 在Bootstrapper中,使用tinyIoc或等效注册新的ViewLocationResultProvider
public class ConcurrentNewViewLocationResultProvider : INewViewLocationResultProvider
    {
        private Dictionary<string, ViewLocationResult> _cachedViewLocationResults;
        private readonly object _cacheLock = new object();
        public bool UseCachedView { get; set; }

        public ConcurrentNewViewLocationResultProvider()
        {
            lock (_cacheLock)
            {
                if(_cachedViewLocationResults == null)_cachedViewLocationResults = new Dictionary<string, ViewLocationResult>();
            }
        }

        public ViewLocationResult GetNewerVersion(string viewName, NancyContext context)
        {
            if (UseCachedView)
            {
                if (Monitor.TryEnter(_cacheLock, TimeSpan.FromMilliseconds(20)))
                {
                    try
                    {
                        if (_cachedViewLocationResults != null && _cachedViewLocationResults.ContainsKey(viewName))
                        {
                            return _cachedViewLocationResults[viewName];
                        }
                    }
                    finally
                    {
                        Monitor.Exit(_cacheLock);
                    }
                }
            }

            return null;
        }

        public void UpdateCachedView(IDictionary<string, ViewLocationResult> replacements)
        {
            lock (_cacheLock)
            {
                if(_cachedViewLocationResults == null)_cachedViewLocationResults = new Dictionary<string, ViewLocationResult>();
                foreach (var replace in replacements)
                {
                    if (_cachedViewLocationResults.ContainsKey(replace.Key))
                    {
                        _cachedViewLocationResults[replace.Key] = replace.Value;
                    }
                    else
                    {
                        _cachedViewLocationResults.Add(replace.Key,replace.Value);
                    }                   
                }
            }
        }
    }
  1. 从ViewLocationResult创建派生类
container.Register<INewViewLocationResultProvider, ConcurrentNewViewLocationResultProvider>().AsSingleton();
  1. 还有一个新的IViewLocator:
    public class OneTimeUsedViewLocationResult : ViewLocationResult
    {
        private bool _used = false;
        public OneTimeUsedViewLocationResult(string location, string name, string extension, Func<TextReader> contents)
            : base(location, name, extension, contents)
        {
        }

        public override bool IsStale()
        {
            if (_used) return false;
            _used = true;
            return true;
        }
    }
  1. 告诉nancy关于新的ViewLocator
public class CachedViewLocator : IViewLocator
    {
        private readonly INewViewLocationResultProvider _newVersion;
        private readonly DefaultViewLocator _fallbackViewLocator;
        public CachedViewLocator(IViewLocationProvider viewLocationProvider, IEnumerable<IViewEngine> viewEngines, INewViewLocationResultProvider newVersion)
        {
            _fallbackViewLocator = new DefaultViewLocator(viewLocationProvider, viewEngines);
            _newVersion = newVersion;
        }

        public ViewLocationResult LocateView(string viewName, NancyContext context)
        {
            if (_newVersion.UseCachedView)
            {
                var result = _newVersion.GetNewerVersion(viewName, context);
                if (result != null) return result;
            }
            return _fallbackViewLocator.LocateView(viewName, context);
        }

        public IEnumerable<ViewLocationResult> GetAllCurrentlyDiscoveredViews()
        {
            return _fallbackViewLocator.GetAllCurrentlyDiscoveredViews();
        }

    }
}
  1. 然后你可以通过这样的API更新它:
        protected override NancyInternalConfiguration InternalConfiguration
        {
            get
            {
                return NancyInternalConfiguration.WithOverrides
                (
                    nic =>
                    {
                        nic.ViewLocationProvider = typeof(ResourceViewLocationProvider);//use this or your equivalent
                        nic.ViewLocator = typeof(CachedViewLocator);
                    }
                );
            }            
        }

注意:上面的代码并没有解决我在问题中手动映射ViewLocationResult事件的所有Location,Name,Extension,但是因为我最终为我的大学构建了一个视图编辑器来上传他们的视图,所以我不需要解决它了。

© www.soinside.com 2019 - 2024. All rights reserved.