private void CompleteFetch(ExternalResource resource)
        {
            resource.Loaded.Set();

            if(resource.Uri.IsFile)
            {
                // resource is a local file, setup a file watch

                if (Settings.Default.WatchFiles)
                {
                    WatchFile(resource);
                }
            }
            else
            {
                // resource is a remote url, schedule a refetch

                if (Settings.Default.ExternalResourceRefreshInterval > 0)
                {
                    HttpRuntime.Cache.Add(Guid.NewGuid().ToString(), resource, null,
                        DateTime.Now.AddSeconds(Settings.Default.ExternalResourceRefreshInterval),
                        Cache.NoSlidingExpiration, CacheItemPriority.Default, Refetch);
                }
            }
        }
        public void Fetch(ExternalResource resource)
        {
            if (resource != null)
            {
                var wr = WebRequest.Create(resource.Uri);
                var rs = new RequestState { Request = wr, Resource = resource };
                var ar = wr.BeginGetResponse(ResponseCallback, rs);

                ThreadPool.RegisterWaitForSingleObject(ar.AsyncWaitHandle, TimeoutCallback,
                    rs, Settings.Default.ExternalResourceTimeout * 1000, true);
            }
        }
Exemple #3
0
        public void Add(Uri resourceUri)
        {
            var key = resourceUri.GetKey();

            AddInternal(key, () =>
            {
                FileInfo file;
                ExternalResource resource;

                if (resourceUri.TryMapPath(out file))
                {
                    resource = new ExternalResource { Uri = new Uri(file.FullName) };
                }
                else
                {
                    resource = new ExternalResource { Uri = resourceUri };
                }

                resources.Add(key, resource);
                fetcher.Fetch(resource);
            });
        }
        private void WatchFile(ExternalResource resource)
        {
            var file = new FileInfo(resource.Uri.LocalPath);
            lock (directories)
            {
                if (!directories.ContainsKey(file.DirectoryName))
                {
                    var fsw = new FileSystemWatcher(file.DirectoryName);
                    fsw.NotifyFilter = NotifyFilters.LastWrite;
                    fsw.Changed += OnFileChanged;
                    directories.Add(file.DirectoryName, new WatchedDirectory { Watcher = fsw });
                }

                var dir = directories[file.DirectoryName];
                if (!dir.Resources.ContainsKey(file.Name))
                {
                    dir.Resources.Add(file.Name, resource);
                    dir.Watcher.EnableRaisingEvents = true;
                }
            }
        }