コード例 #1
0
        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);
                }
            }
        }
コード例 #2
0
        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);
            }
        }
コード例 #3
0
ファイル: ResourceRepository.cs プロジェクト: jo5ef/netzlib
        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);
            });
        }
コード例 #4
0
        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;
                }
            }
        }