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
- ControlParser.cs
- JoinTreeNode.cs
- HelpKeywordAttribute.cs
- SchemaElement.cs
- CustomSignedXml.cs
- DbConnectionOptions.cs
- DbMetaDataCollectionNames.cs
- ProfilePropertyMetadata.cs
- PagesSection.cs
- SrgsElement.cs
- MaterialCollection.cs
- GestureRecognitionResult.cs
- ImageDrawing.cs
- VolatileEnlistmentMultiplexing.cs
- XmlArrayAttribute.cs
- TableLayoutSettings.cs
- BufferedWebEventProvider.cs
- QualificationDataAttribute.cs
- Repeater.cs
- EventlogProvider.cs
- UndirectedGraph.cs
- CryptoSession.cs
- HtmlInputRadioButton.cs
- SmiMetaDataProperty.cs
- Pens.cs
- ToolStripProgressBar.cs
- ImageButton.cs
- TextTrailingWordEllipsis.cs
- CacheSection.cs
- PrimaryKeyTypeConverter.cs
- HtmlSelect.cs
- AdapterDictionary.cs
- MailHeaderInfo.cs
- ItemMap.cs
- EventListener.cs
- BindToObject.cs
- RSAPKCS1SignatureDeformatter.cs
- CodeTypeOfExpression.cs
- GradientStop.cs
- Function.cs
- COM2ICategorizePropertiesHandler.cs
- SafeRegistryHandle.cs
- BmpBitmapEncoder.cs
- LOSFormatter.cs
- CodeCompiler.cs
- Material.cs
- GetRecipientListRequest.cs
- WaitForChangedResult.cs
- AnnotationHighlightLayer.cs
- JsonFormatReaderGenerator.cs
- TypeLibraryHelper.cs
- TagMapCollection.cs
- Enum.cs
- HttpException.cs
- BufferedGraphics.cs
- DbConnectionPoolOptions.cs
- ExclusiveTcpTransportManager.cs
- Attributes.cs
- ReaderWriterLockWrapper.cs
- Model3D.cs
- ListSourceHelper.cs
- TreeWalker.cs
- HttpDebugHandler.cs
- RemotingServices.cs
- RegionInfo.cs
- TabRenderer.cs
- SqlBooleanizer.cs
- HelpProvider.cs
- serverconfig.cs
- ContractInstanceProvider.cs
- ApplicationManager.cs
- WebPartHeaderCloseVerb.cs
- RegexParser.cs
- DesignerDataConnection.cs
- TraceSource.cs
- MetafileHeaderWmf.cs
- ReadOnlyDictionary.cs
- SignerInfo.cs
- ObjectParameter.cs
- ComponentChangingEvent.cs
- DataGridView.cs
- StylusPointPropertyId.cs
- SafeHandles.cs
- XmlNodeList.cs
- PrinterSettings.cs
- OptimisticConcurrencyException.cs
- RedistVersionInfo.cs
- ScalarRestriction.cs
- ExceptionRoutedEventArgs.cs
- XmlSchemaSequence.cs
- SQLSingleStorage.cs
- METAHEADER.cs
- ClientUriBehavior.cs
- DiscardableAttribute.cs
- MarshalByRefObject.cs
- StringTraceRecord.cs
- DiscoveryDocumentSearchPattern.cs
- ProcessStartInfo.cs
- GatewayDefinition.cs
- Int32Rect.cs