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
- HttpErrorTraceRecord.cs
- LayoutSettings.cs
- DataGridViewRowPrePaintEventArgs.cs
- FixedSOMTextRun.cs
- AlgoModule.cs
- ToolStripRendererSwitcher.cs
- GridViewSelectEventArgs.cs
- PointIndependentAnimationStorage.cs
- MultipartIdentifier.cs
- SecurityContextKeyIdentifierClause.cs
- PriorityBinding.cs
- odbcmetadatafactory.cs
- CRYPTPROTECT_PROMPTSTRUCT.cs
- ConfigurationSectionCollection.cs
- ISAPIWorkerRequest.cs
- ReadContentAsBinaryHelper.cs
- TraceSection.cs
- RealProxy.cs
- ImageMapEventArgs.cs
- ActivityMarkupSerializationProvider.cs
- StyleSheetComponentEditor.cs
- QilSortKey.cs
- InstanceCreationEditor.cs
- documentsequencetextcontainer.cs
- XhtmlConformanceSection.cs
- HyperLinkDataBindingHandler.cs
- DataObject.cs
- MailWebEventProvider.cs
- EventListenerClientSide.cs
- PackWebRequest.cs
- DetailsViewUpdateEventArgs.cs
- CharStorage.cs
- CodeFieldReferenceExpression.cs
- unsafeIndexingFilterStream.cs
- ByteAnimationUsingKeyFrames.cs
- ObjectPersistData.cs
- HttpInputStream.cs
- SmtpFailedRecipientsException.cs
- TypeRestriction.cs
- SmtpCommands.cs
- HttpStreamFormatter.cs
- Claim.cs
- SoapFault.cs
- SessionPageStatePersister.cs
- OpCodes.cs
- PenContext.cs
- DataGridViewRowCollection.cs
- NaturalLanguageHyphenator.cs
- DropShadowBitmapEffect.cs
- HttpListenerRequest.cs
- ExponentialEase.cs
- TokenFactoryBase.cs
- handlecollector.cs
- UpDownEvent.cs
- ProtocolsConfiguration.cs
- WmlListAdapter.cs
- BridgeDataReader.cs
- Int64KeyFrameCollection.cs
- QueryAccessibilityHelpEvent.cs
- IdentifierCreationService.cs
- TransactionFlowAttribute.cs
- IdentityValidationException.cs
- ViewStateModeByIdAttribute.cs
- ExceptionValidationRule.cs
- IChannel.cs
- CroppedBitmap.cs
- MSAANativeProvider.cs
- ListViewTableRow.cs
- DataGridViewUtilities.cs
- HostedImpersonationContext.cs
- SqlCacheDependencySection.cs
- DoubleCollectionValueSerializer.cs
- Registry.cs
- TableRow.cs
- WSSecurityOneDotOneSendSecurityHeader.cs
- IntSecurity.cs
- CategoryValueConverter.cs
- DbConnectionPoolGroupProviderInfo.cs
- LocalizationParserHooks.cs
- DataKey.cs
- EndPoint.cs
- NavigationService.cs
- LogicalTreeHelper.cs
- Selection.cs
- WaitForChangedResult.cs
- RemoteWebConfigurationHostStream.cs
- NumberSubstitution.cs
- XXXOnTypeBuilderInstantiation.cs
- WorkflowViewStateService.cs
- Monitor.cs
- GZipStream.cs
- DbProviderFactories.cs
- FileSystemWatcher.cs
- SerializationTrace.cs
- SpoolingTask.cs
- PageFunction.cs
- QueryInterceptorAttribute.cs
- DataList.cs
- DeflateStreamAsyncResult.cs
- RuntimeWrappedException.cs