Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / DynamicData / DynamicData / Util / FileChangeNotifier.cs / 1305376 / FileChangeNotifier.cs
using System.Collections; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections.Specialized; using System.Diagnostics; using System.Web.Resources; using System.Globalization; using System.Web.Caching; using System.Web.Hosting; namespace System.Web.DynamicData { delegate void FileChangedCallback(string path); class FileChangeNotifier { private static VirtualPathProvider _vpp; internal static VirtualPathProvider VirtualPathProvider { private get { if (_vpp == null) { _vpp = HostingEnvironment.VirtualPathProvider; } return _vpp; } // For unit test purpose set { _vpp = value; } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "System.Web.DynamicData.FileChangeNotifier", Justification="The object deals with file change notifications and we don't need to hold on to it")] internal static void Register(string virtualPath, FileChangedCallback onFileChanged) { new FileChangeNotifier(virtualPath, onFileChanged); } private FileChangedCallback _onFileChanged; private FileChangeNotifier(string virtualPath, FileChangedCallback onFileChanged) { _onFileChanged = onFileChanged; RegisterForNextNotification(virtualPath); } private void RegisterForNextNotification(string virtualPath) { // Get a CacheDependency from the BuildProvider, so that we know anytime something changes var virtualPathDependencies = new List(); virtualPathDependencies.Add(virtualPath); CacheDependency cacheDependency = VirtualPathProvider.GetCacheDependency( virtualPath, virtualPathDependencies, DateTime.UtcNow); // Rely on the ASP.NET cache for file change notifications, since FileSystemWatcher // doesn't work in medium trust HttpRuntime.Cache.Insert(virtualPath /*key*/, virtualPath /*value*/, cacheDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, new CacheItemRemovedCallback(OnCacheItemRemoved)); } private void OnCacheItemRemoved(string key, object value, CacheItemRemovedReason reason) { // We only care about dependency changes if (reason != CacheItemRemovedReason.DependencyChanged) return; _onFileChanged(key); // We need to register again to get the next notification RegisterForNextNotification(key); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. using System.Collections; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections.Specialized; using System.Diagnostics; using System.Web.Resources; using System.Globalization; using System.Web.Caching; using System.Web.Hosting; namespace System.Web.DynamicData { delegate void FileChangedCallback(string path); class FileChangeNotifier { private static VirtualPathProvider _vpp; internal static VirtualPathProvider VirtualPathProvider { private get { if (_vpp == null) { _vpp = HostingEnvironment.VirtualPathProvider; } return _vpp; } // For unit test purpose set { _vpp = value; } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "System.Web.DynamicData.FileChangeNotifier", Justification="The object deals with file change notifications and we don't need to hold on to it")] internal static void Register(string virtualPath, FileChangedCallback onFileChanged) { new FileChangeNotifier(virtualPath, onFileChanged); } private FileChangedCallback _onFileChanged; private FileChangeNotifier(string virtualPath, FileChangedCallback onFileChanged) { _onFileChanged = onFileChanged; RegisterForNextNotification(virtualPath); } private void RegisterForNextNotification(string virtualPath) { // Get a CacheDependency from the BuildProvider, so that we know anytime something changes var virtualPathDependencies = new List (); virtualPathDependencies.Add(virtualPath); CacheDependency cacheDependency = VirtualPathProvider.GetCacheDependency( virtualPath, virtualPathDependencies, DateTime.UtcNow); // Rely on the ASP.NET cache for file change notifications, since FileSystemWatcher // doesn't work in medium trust HttpRuntime.Cache.Insert(virtualPath /*key*/, virtualPath /*value*/, cacheDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, new CacheItemRemovedCallback(OnCacheItemRemoved)); } private void OnCacheItemRemoved(string key, object value, CacheItemRemovedReason reason) { // We only care about dependency changes if (reason != CacheItemRemovedReason.DependencyChanged) return; _onFileChanged(key); // We need to register again to get the next notification RegisterForNextNotification(key); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- LocalBuilder.cs
- SafeFindHandle.cs
- HostingEnvironmentWrapper.cs
- ViewGenerator.cs
- WindowsRichEditRange.cs
- DataMisalignedException.cs
- FileLogRecordEnumerator.cs
- StylusPointPropertyInfoDefaults.cs
- NativeMethods.cs
- EmptyQuery.cs
- JsonClassDataContract.cs
- SystemInfo.cs
- ThrowHelper.cs
- HandlerWithFactory.cs
- SqlNodeAnnotation.cs
- WsdlParser.cs
- QualificationDataAttribute.cs
- ValidationError.cs
- AdRotator.cs
- EndEvent.cs
- NotifyIcon.cs
- RegistryPermission.cs
- CursorInteropHelper.cs
- SoapIgnoreAttribute.cs
- XamlParser.cs
- TableParagraph.cs
- Transform3DGroup.cs
- XmlDataImplementation.cs
- SettingsBindableAttribute.cs
- FontInfo.cs
- RuntimeResourceSet.cs
- XamlParser.cs
- SafeFindHandle.cs
- SetterBaseCollection.cs
- ValueExpressions.cs
- ReplyChannel.cs
- SystemIPInterfaceStatistics.cs
- ZoneButton.cs
- KeyValuePairs.cs
- UrlPath.cs
- RuntimeConfigLKG.cs
- VScrollProperties.cs
- FieldNameLookup.cs
- ReadWriteSpinLock.cs
- XmlWhitespace.cs
- GeometryCollection.cs
- DataControlImageButton.cs
- COM2ExtendedUITypeEditor.cs
- TextPatternIdentifiers.cs
- ArrayList.cs
- DataGridViewImageCell.cs
- filewebrequest.cs
- SqlConnectionPoolGroupProviderInfo.cs
- ProvidersHelper.cs
- ReaderWriterLockWrapper.cs
- RepeaterItem.cs
- WizardPanel.cs
- EncoderBestFitFallback.cs
- DataGridAddNewRow.cs
- ViewKeyConstraint.cs
- OperationInvokerTrace.cs
- WindowsGraphicsWrapper.cs
- arabicshape.cs
- PackagePart.cs
- BufferedReadStream.cs
- httpserverutility.cs
- QueryPageSettingsEventArgs.cs
- CreateUserWizard.cs
- SectionXmlInfo.cs
- StreamMarshaler.cs
- ErrorWebPart.cs
- CqlParser.cs
- CompatibleIComparer.cs
- Deserializer.cs
- MetafileHeaderWmf.cs
- WizardStepBase.cs
- base64Transforms.cs
- OracleRowUpdatingEventArgs.cs
- ReadOnlyHierarchicalDataSource.cs
- ZipIOModeEnforcingStream.cs
- ValueConversionAttribute.cs
- WebBrowserEvent.cs
- ThreadPool.cs
- ComponentResourceManager.cs
- StylusShape.cs
- TableLayoutRowStyleCollection.cs
- ActivationArguments.cs
- SchemaDeclBase.cs
- SqlResolver.cs
- TrackingDataItem.cs
- SpAudioStreamWrapper.cs
- HtmlEncodedRawTextWriter.cs
- XmlDocument.cs
- SystemUnicastIPAddressInformation.cs
- DBDataPermissionAttribute.cs
- DataSourceHelper.cs
- Enumerable.cs
- QilDataSource.cs
- DataGrid.cs
- BooleanKeyFrameCollection.cs