//private readonly Task queueListenerTask;
 /// <summary>
 /// Creates a new WatchingResolvePathTemplateManager.
 /// </summary>
 /// <param name="layoutRoot">the folders to watch and look for templates.</param>
 /// <param name="cache">the cache to invalidate</param>
 public WatchingResolvePathTemplateManager(IEnumerable<string> layoutRoot, InvalidatingCachingProvider cache)
 {
     this.cache = cache;
     var list = new ReadOnlyCollection<string>(new List<string>(layoutRoot));
     inner = new ResolvePathTemplateManager(list);
     watchers = list.Select(path =>
     {
         var watcher = new FileSystemWatcher(Path.GetFullPath(path), "*.*");
         watcher.EnableRaisingEvents = true;
         watcher.IncludeSubdirectories = true;
         watcher.Changed += watcher_Changed;
         watcher.Created += watcher_Changed;
         watcher.Deleted += watcher_Changed;
         watcher.Renamed += watcher_Renamed;
         return watcher;
     }).ToList();
     //queueListenerTask = StartQueue();
 }
        //private readonly Task queueListenerTask;
        /// <summary>
        /// Creates a new WatchingResolvePathTemplateManager.
        /// </summary>
        /// <param name="layoutRoot">the folders to watch and look for templates.</param>
        /// <param name="cache">the cache to invalidate</param>
        public WatchingResolvePathTemplateManager(IEnumerable <string> layoutRoot, InvalidatingCachingProvider cache)
        {
            this.cache = cache;
            var list = new ReadOnlyCollection <string>(new List <string>(layoutRoot));

            inner    = new ResolvePathTemplateManager(list);
            watchers = list.Select(path =>
            {
                var watcher = new FileSystemWatcher(Path.GetFullPath(path), "*.*");
                watcher.EnableRaisingEvents   = true;
                watcher.IncludeSubdirectories = true;
                watcher.Changed += watcher_Changed;
                watcher.Created += watcher_Changed;
                watcher.Deleted += watcher_Changed;
                watcher.Renamed += watcher_Renamed;
                return(watcher);
            }).ToList();
            //queueListenerTask = StartQueue();
        }
        public void RazorEngineService_TestWatchingResolvePathTemplateManager()
        {
            var temp = Path.GetTempFileName();
            var template2File = Path.GetTempFileName();
            File.Delete(temp);
            var prev = Environment.GetEnvironmentVariable("MONO_MANAGED_WATCHER");
            try
            {
                if (IsolatedRazorEngineServiceTestFixture.IsRunningOnMono())
                {
                    Environment.SetEnvironmentVariable("MONO_MANAGED_WATCHER", "enabled");
                }
                Directory.CreateDirectory(temp);
                var cache = new InvalidatingCachingProvider();
                var mgr = new WatchingResolvePathTemplateManager(new [] { temp }, cache);
                var template = @"initial";
                var templateChanged = @"next";

                var templateFileName = Path.ChangeExtension(Path.GetRandomFileName(), ".cshtml");
                var templateName = Path.GetFileNameWithoutExtension(templateFileName);
                var templateFile = Path.Combine(temp, templateFileName);
                File.WriteAllText(templateFile, template);

                RunTestHelper(service =>
                {
                    var model = new { MyPrice = 0 };
                    string result = service.RunCompile(templateFileName, null, model);
                    Assert.AreEqual("initial", result.Trim());

                    File.WriteAllText(templateFile, templateChanged);
                    Thread.Sleep(2000); // wait for the events to kick in.

                    string result2 = service.RunCompile(templateFileName, null, model);
                    Assert.AreEqual("next", result2.Trim());
                }, config =>
                {
                    config.CachingProvider = cache;
                    config.TemplateManager = mgr;
                });
            }
            finally
            {
                Environment.SetEnvironmentVariable("MONO_MANAGED_WATCHER", prev);
                Directory.Delete(temp, true);
                if (File.Exists(template2File)) { File.Delete(template2File); }
            }
        }
        public void RazorEngineService_TestResolvePathTemplateManager()
        {
            var cache = new InvalidatingCachingProvider();
            var temp = Path.GetTempFileName();
            var template2File = Path.GetTempFileName();
            File.Delete(temp);
            try
            {
                Directory.CreateDirectory(temp);
                var mgr = new ResolvePathTemplateManager(new[] { temp });
                var template = @"free";
                var template2 = @"template2";
                File.WriteAllText(template2File, template2);

                var templateFileName = Path.ChangeExtension(Path.GetRandomFileName(), ".cshtml");
                var templateName = Path.GetFileNameWithoutExtension(templateFileName);
                var templateFile = Path.Combine(temp, templateFileName);
                File.WriteAllText(templateFile, template);

                RunTestHelper(service =>
                {
                    var model = new { MyPrice = 0 };
                    string result = service.RunCompile(templateName, null, model);
                    Assert.AreEqual("free", result.Trim());
                    string result2 = service.RunCompile(template2File, null, model);
                    Assert.AreEqual("template2", result2.Trim());
                }, config =>
                {
                    config.CachingProvider = cache;
                    config.TemplateManager = mgr;
                });
            }
            finally
            {
                Directory.Delete(temp, true);
                if (File.Exists(template2File)) { File.Delete(template2File); }
            }
        }
 public void RazorEngineService_TestInvalidatingCachingProvider()
 {
     var cache = new InvalidatingCachingProvider();
     var mgr = new DelegateTemplateManager();
     var template =
     @"@helper Display(int price) {
     if (price == 0) {
     <text>free</text>
     } else {
     <text>@price</text>
     }
     }
     @Display(Model.MyPrice)";
     var template2 =
     @"@helper Display(int price) {
     if (price == 0) {
     <text>totally free</text>
     } else {
     <text>@price</text>
     }
     }
     @Display(Model.MyPrice)";
     RunTestHelper(service =>
     {
         var model = new { MyPrice = 0 };
         string result = service.RunCompile(template, "key", null, model);
         Assert.AreEqual("free", result.Trim());
         cache.InvalidateCache(service.GetKey("key"));
         mgr.RemoveDynamic(service.GetKey("key"));
         string result2 = service.RunCompile(template2, "key", null, model);
         Assert.AreEqual("totally free", result2.Trim());
     }, config => {
         config.CachingProvider = cache;
         config.TemplateManager = mgr;
     });
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultCachingProvider"/> class.
 /// </summary>
 /// <param name="registerForCleanup">callback for files which need to be cleaned up.</param>
 public DefaultCachingProvider(Action<string> registerForCleanup)
 {
     inner = new InvalidatingCachingProvider(registerForCleanup);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultCachingProvider"/> class.
 /// </summary>
 public DefaultCachingProvider() : this(null)
 {
     inner = new InvalidatingCachingProvider();
 }
 /// <summary>
 /// Get the key used within a dictionary for a modelType.
 /// </summary>
 public static Type GetModelTypeKey(Type modelType)
 {
     return InvalidatingCachingProvider.GetModelTypeKey(modelType);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultCachingProvider"/> class.
 /// </summary>
 /// <param name="registerForCleanup">callback for files which need to be cleaned up.</param>
 public DefaultCachingProvider(Action<string> registerForCleanup)
 {
     inner = new InvalidatingCachingProvider(registerForCleanup);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultCachingProvider"/> class.
 /// </summary>
 public DefaultCachingProvider()
     : this(null)
 {
     inner = new InvalidatingCachingProvider();
 }