Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / Web / Configuration / MTConfigUtil.cs / 1484510 / MTConfigUtil.cs
using System; using System.Collections.Concurrent; using System.Configuration; using System.Web; using System.Web.Compilation; using System.Web.Configuration; using System.Web.Hosting; using Microsoft.Build.Utilities; // Helper class to use 2.0 root config as necessary. Each helper method will // go through RuntimeConfig (the original code path), except in the // case of building and targeting 2.0/3.5. internal class MTConfigUtil { private static readonly ConcurrentDictionary, ConfigurationSection> s_sections = new ConcurrentDictionary , ConfigurationSection>(); private static readonly ConcurrentDictionary s_configurations = new ConcurrentDictionary (); private static string s_machineConfigPath; private static VirtualPath s_appVirtualPath; // We only need to use the root config of 2.0 if we are building (and // not during runtime) and targeting 2.0 or 3.5. static private bool? s_useMTConfig; static private bool UseMTConfig { get { if (s_useMTConfig == null) { s_useMTConfig = BuildManagerHost.InClientBuildManager && (MultiTargetingUtil.IsTargetFramework20 || MultiTargetingUtil.IsTargetFramework35); } return s_useMTConfig.Value; } } // Counterpart for RuntimeConfig.GetAppConfig().Profile; static internal ProfileSection GetProfileAppConfig() { if (!UseMTConfig) { return RuntimeConfig.GetAppConfig().Profile; } return GetAppConfig (); } // Counterpart for RuntimeConfig.GetAppConfig().Pages; static internal PagesSection GetPagesAppConfig() { if (!UseMTConfig) { return RuntimeConfig.GetAppConfig().Pages; } return GetAppConfig (); } // Counterpart for RuntimeConfig.GetConfig().Pages; static internal PagesSection GetPagesConfig() { if (!UseMTConfig) { return RuntimeConfig.GetConfig().Pages; } return GetConfig (); } // Counterpart for RuntimeConfig.GetConfig(string).Pages static internal PagesSection GetPagesConfig(string vpath) { if (!UseMTConfig) { return RuntimeConfig.GetConfig(vpath).Pages; } return GetConfig (vpath); } // Counterpart for RuntimeConfig.GetConfig(VirtualPath).Pages static internal PagesSection GetPagesConfig(VirtualPath vpath) { if (!UseMTConfig) { return RuntimeConfig.GetConfig(vpath).Pages; } return GetConfig (vpath); } // Counterpart for RuntimeConfig.GetConfig(HttpContext).Pages static internal PagesSection GetPagesConfig(HttpContext context) { if (!UseMTConfig) { return RuntimeConfig.GetConfig(context).Pages; } return GetConfig (context); } // Counterpart for RuntimeConfig.GetConfig().Compilation static internal CompilationSection GetCompilationConfig() { if (!UseMTConfig) { return RuntimeConfig.GetConfig().Compilation; } return GetConfig (); } // Counterpart for RuntimeConfig.GetAppConfig().Compilation static internal CompilationSection GetCompilationAppConfig() { if (!UseMTConfig) { return RuntimeConfig.GetAppConfig().Compilation; } return GetAppConfig (); } // Counterpart for RuntimeConfig.GetConfig(string).Compilation static internal CompilationSection GetCompilationConfig(string vpath) { if (!UseMTConfig) { return RuntimeConfig.GetConfig(vpath).Compilation; } return GetConfig (vpath); } // Counterpart for RuntimeConfig.GetConfig(VirtualPath).Compilation static internal CompilationSection GetCompilationConfig(VirtualPath vpath) { if (!UseMTConfig) { return RuntimeConfig.GetConfig(vpath).Compilation; } return GetConfig (vpath); } // Counterpart for RuntimeConfig.GetConfig(HttpContext).Compilation static internal CompilationSection GetCompilationConfig(HttpContext context) { if (!UseMTConfig) { return RuntimeConfig.GetConfig(context).Compilation; } return GetConfig (context); } // Counterpart to RuntimeConfig.GetConfig() static private S GetConfig () where S : ConfigurationSection { HttpContext context = HttpContext.Current; if (context != null) { return GetConfig(context); } else { return GetAppConfig(); } } // Counterpart to RuntimeConfig.GetAppConfig() static private S GetAppConfig() where S : ConfigurationSection { return GetConfig((VirtualPath) null); } // Counterpart to RuntimeConfig.GetConfig(HttpContext) static private S GetConfig(HttpContext context) where S : ConfigurationSection { return GetConfig(context.ConfigurationPath); } // Counterpart to RuntimeConfig.GetConfig(string) static private S GetConfig(string vpath) where S : ConfigurationSection{ return GetConfig(VirtualPath.CreateNonRelativeAllowNull(vpath)); } // Counterpart to RuntimeConfig.GetConfig(VirtualPath) static private S GetConfig(VirtualPath vpath) where S : ConfigurationSection { Tuplekey = new Tuple (typeof(S), vpath); ConfigurationSection result; if (!s_sections.TryGetValue(key, out result)) { result = GetConfigHelper (vpath); s_sections.TryAdd(key, result); } return result as S; } // Actual method performing to work to retrieve the required ConfigurationSection. static private S GetConfigHelper(VirtualPath vpath) where S : ConfigurationSection{ string physicalPath = null; if (vpath == null || !vpath.IsWithinAppRoot) { // If virtual path is null or outside the application root, we use the application level config. vpath = HostingEnvironment.ApplicationVirtualPathObject; physicalPath = HostingEnvironment.ApplicationPhysicalPath; } else { // If it is not a directory, use the directory as the vpath if (!vpath.DirectoryExists()) { vpath = vpath.Parent; } physicalPath = HostingEnvironment.MapPath(vpath); } Configuration config = GetConfiguration(vpath, physicalPath); // Retrieve the specified section if (typeof(S) == typeof(CompilationSection)) { return config.GetSection("system.web/compilation") as S; } else if (typeof(S) == typeof(PagesSection)) { return config.GetSection("system.web/pages") as S; } else if (typeof(S) == typeof(ProfileSection)) { return config.GetSection("system.web/profile") as S; } throw new InvalidOperationException(SR.GetString(SR.Config_section_not_supported, typeof(S).FullName)); } static private string MachineConfigPath { get { if (s_machineConfigPath == null) { s_machineConfigPath = ToolLocationHelper.GetPathToDotNetFrameworkFile(@"config\machine.config", TargetDotNetFrameworkVersion.Version20); if (string.IsNullOrEmpty(s_machineConfigPath)) { string message = SR.GetString(SR.Downlevel_requires_35); throw new InvalidOperationException(message); } } return s_machineConfigPath; } } static private Configuration GetConfiguration(VirtualPath vpath, string physicalPath) { Configuration result; if (!s_configurations.TryGetValue(vpath, out result)) { result = GetConfigurationHelper(vpath, physicalPath); s_configurations.TryAdd(vpath, result); } return result; } static private Configuration GetConfigurationHelper(VirtualPath vpath, string physicalPath) { // Set up the configuration file map string machineConfigPath = MachineConfigPath; WebConfigurationFileMap fileMap = new WebConfigurationFileMap(machineConfigPath); // Set up file maps for the current directory and parent directories up to application root VirtualPath currentVPath = vpath; while (currentVPath != null && currentVPath.IsWithinAppRoot) { string vpathString = currentVPath.VirtualPathStringNoTrailingSlash; if (physicalPath == null) { physicalPath = HostingEnvironment.MapPath(currentVPath); } fileMap.VirtualDirectories.Add(vpathString, new VirtualDirectoryMapping(physicalPath, IsAppRoot(currentVPath))); currentVPath = currentVPath.Parent; physicalPath = null; } Configuration config = WebConfigurationManager.OpenMappedWebConfiguration(fileMap, vpath.VirtualPathStringNoTrailingSlash, HostingEnvironment.SiteName); return config; } static private bool IsAppRoot(VirtualPath path) { if (s_appVirtualPath == null) { s_appVirtualPath = VirtualPath.Create(HttpRuntime.AppDomainAppVirtualPathObject.VirtualPathStringNoTrailingSlash); } var vpath = VirtualPath.Create(path.VirtualPathStringNoTrailingSlash); return s_appVirtualPath.Equals(vpath); } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. using System; using System.Collections.Concurrent; using System.Configuration; using System.Web; using System.Web.Compilation; using System.Web.Configuration; using System.Web.Hosting; using Microsoft.Build.Utilities; // Helper class to use 2.0 root config as necessary. Each helper method will // go through RuntimeConfig (the original code path), except in the // case of building and targeting 2.0/3.5. internal class MTConfigUtil { private static readonly ConcurrentDictionary, ConfigurationSection> s_sections = new ConcurrentDictionary , ConfigurationSection>(); private static readonly ConcurrentDictionary s_configurations = new ConcurrentDictionary (); private static string s_machineConfigPath; private static VirtualPath s_appVirtualPath; // We only need to use the root config of 2.0 if we are building (and // not during runtime) and targeting 2.0 or 3.5. static private bool? s_useMTConfig; static private bool UseMTConfig { get { if (s_useMTConfig == null) { s_useMTConfig = BuildManagerHost.InClientBuildManager && (MultiTargetingUtil.IsTargetFramework20 || MultiTargetingUtil.IsTargetFramework35); } return s_useMTConfig.Value; } } // Counterpart for RuntimeConfig.GetAppConfig().Profile; static internal ProfileSection GetProfileAppConfig() { if (!UseMTConfig) { return RuntimeConfig.GetAppConfig().Profile; } return GetAppConfig (); } // Counterpart for RuntimeConfig.GetAppConfig().Pages; static internal PagesSection GetPagesAppConfig() { if (!UseMTConfig) { return RuntimeConfig.GetAppConfig().Pages; } return GetAppConfig (); } // Counterpart for RuntimeConfig.GetConfig().Pages; static internal PagesSection GetPagesConfig() { if (!UseMTConfig) { return RuntimeConfig.GetConfig().Pages; } return GetConfig (); } // Counterpart for RuntimeConfig.GetConfig(string).Pages static internal PagesSection GetPagesConfig(string vpath) { if (!UseMTConfig) { return RuntimeConfig.GetConfig(vpath).Pages; } return GetConfig (vpath); } // Counterpart for RuntimeConfig.GetConfig(VirtualPath).Pages static internal PagesSection GetPagesConfig(VirtualPath vpath) { if (!UseMTConfig) { return RuntimeConfig.GetConfig(vpath).Pages; } return GetConfig (vpath); } // Counterpart for RuntimeConfig.GetConfig(HttpContext).Pages static internal PagesSection GetPagesConfig(HttpContext context) { if (!UseMTConfig) { return RuntimeConfig.GetConfig(context).Pages; } return GetConfig (context); } // Counterpart for RuntimeConfig.GetConfig().Compilation static internal CompilationSection GetCompilationConfig() { if (!UseMTConfig) { return RuntimeConfig.GetConfig().Compilation; } return GetConfig (); } // Counterpart for RuntimeConfig.GetAppConfig().Compilation static internal CompilationSection GetCompilationAppConfig() { if (!UseMTConfig) { return RuntimeConfig.GetAppConfig().Compilation; } return GetAppConfig (); } // Counterpart for RuntimeConfig.GetConfig(string).Compilation static internal CompilationSection GetCompilationConfig(string vpath) { if (!UseMTConfig) { return RuntimeConfig.GetConfig(vpath).Compilation; } return GetConfig (vpath); } // Counterpart for RuntimeConfig.GetConfig(VirtualPath).Compilation static internal CompilationSection GetCompilationConfig(VirtualPath vpath) { if (!UseMTConfig) { return RuntimeConfig.GetConfig(vpath).Compilation; } return GetConfig (vpath); } // Counterpart for RuntimeConfig.GetConfig(HttpContext).Compilation static internal CompilationSection GetCompilationConfig(HttpContext context) { if (!UseMTConfig) { return RuntimeConfig.GetConfig(context).Compilation; } return GetConfig (context); } // Counterpart to RuntimeConfig.GetConfig() static private S GetConfig () where S : ConfigurationSection { HttpContext context = HttpContext.Current; if (context != null) { return GetConfig(context); } else { return GetAppConfig(); } } // Counterpart to RuntimeConfig.GetAppConfig() static private S GetAppConfig() where S : ConfigurationSection { return GetConfig((VirtualPath) null); } // Counterpart to RuntimeConfig.GetConfig(HttpContext) static private S GetConfig(HttpContext context) where S : ConfigurationSection { return GetConfig(context.ConfigurationPath); } // Counterpart to RuntimeConfig.GetConfig(string) static private S GetConfig(string vpath) where S : ConfigurationSection{ return GetConfig(VirtualPath.CreateNonRelativeAllowNull(vpath)); } // Counterpart to RuntimeConfig.GetConfig(VirtualPath) static private S GetConfig(VirtualPath vpath) where S : ConfigurationSection { Tuplekey = new Tuple (typeof(S), vpath); ConfigurationSection result; if (!s_sections.TryGetValue(key, out result)) { result = GetConfigHelper (vpath); s_sections.TryAdd(key, result); } return result as S; } // Actual method performing to work to retrieve the required ConfigurationSection. static private S GetConfigHelper(VirtualPath vpath) where S : ConfigurationSection{ string physicalPath = null; if (vpath == null || !vpath.IsWithinAppRoot) { // If virtual path is null or outside the application root, we use the application level config. vpath = HostingEnvironment.ApplicationVirtualPathObject; physicalPath = HostingEnvironment.ApplicationPhysicalPath; } else { // If it is not a directory, use the directory as the vpath if (!vpath.DirectoryExists()) { vpath = vpath.Parent; } physicalPath = HostingEnvironment.MapPath(vpath); } Configuration config = GetConfiguration(vpath, physicalPath); // Retrieve the specified section if (typeof(S) == typeof(CompilationSection)) { return config.GetSection("system.web/compilation") as S; } else if (typeof(S) == typeof(PagesSection)) { return config.GetSection("system.web/pages") as S; } else if (typeof(S) == typeof(ProfileSection)) { return config.GetSection("system.web/profile") as S; } throw new InvalidOperationException(SR.GetString(SR.Config_section_not_supported, typeof(S).FullName)); } static private string MachineConfigPath { get { if (s_machineConfigPath == null) { s_machineConfigPath = ToolLocationHelper.GetPathToDotNetFrameworkFile(@"config\machine.config", TargetDotNetFrameworkVersion.Version20); if (string.IsNullOrEmpty(s_machineConfigPath)) { string message = SR.GetString(SR.Downlevel_requires_35); throw new InvalidOperationException(message); } } return s_machineConfigPath; } } static private Configuration GetConfiguration(VirtualPath vpath, string physicalPath) { Configuration result; if (!s_configurations.TryGetValue(vpath, out result)) { result = GetConfigurationHelper(vpath, physicalPath); s_configurations.TryAdd(vpath, result); } return result; } static private Configuration GetConfigurationHelper(VirtualPath vpath, string physicalPath) { // Set up the configuration file map string machineConfigPath = MachineConfigPath; WebConfigurationFileMap fileMap = new WebConfigurationFileMap(machineConfigPath); // Set up file maps for the current directory and parent directories up to application root VirtualPath currentVPath = vpath; while (currentVPath != null && currentVPath.IsWithinAppRoot) { string vpathString = currentVPath.VirtualPathStringNoTrailingSlash; if (physicalPath == null) { physicalPath = HostingEnvironment.MapPath(currentVPath); } fileMap.VirtualDirectories.Add(vpathString, new VirtualDirectoryMapping(physicalPath, IsAppRoot(currentVPath))); currentVPath = currentVPath.Parent; physicalPath = null; } Configuration config = WebConfigurationManager.OpenMappedWebConfiguration(fileMap, vpath.VirtualPathStringNoTrailingSlash, HostingEnvironment.SiteName); return config; } static private bool IsAppRoot(VirtualPath path) { if (s_appVirtualPath == null) { s_appVirtualPath = VirtualPath.Create(HttpRuntime.AppDomainAppVirtualPathObject.VirtualPathStringNoTrailingSlash); } var vpath = VirtualPath.Create(path.VirtualPathStringNoTrailingSlash); return s_appVirtualPath.Equals(vpath); } } // 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
- ZipIOExtraFieldZip64Element.cs
- CleanUpVirtualizedItemEventArgs.cs
- ToolStripDropDownClosingEventArgs.cs
- brushes.cs
- TypeSystemProvider.cs
- NativeMethods.cs
- PublisherMembershipCondition.cs
- ConfigurationValidatorBase.cs
- SamlAuthorityBinding.cs
- FixUp.cs
- MetadataWorkspace.cs
- Table.cs
- TrustDriver.cs
- Expander.cs
- TableAdapterManagerHelper.cs
- CodeConditionStatement.cs
- DefaultMemberAttribute.cs
- CommandBinding.cs
- LookupBindingPropertiesAttribute.cs
- XslTransform.cs
- Hyperlink.cs
- SqlDataSourceQueryConverter.cs
- EventMap.cs
- RsaSecurityTokenAuthenticator.cs
- CookieHandler.cs
- RepeatBehaviorConverter.cs
- UITypeEditor.cs
- InternalConfigSettingsFactory.cs
- SourceFileInfo.cs
- _HeaderInfoTable.cs
- CompensatableTransactionScopeActivityDesigner.cs
- safelink.cs
- _CacheStreams.cs
- Wildcard.cs
- XmlSchemas.cs
- PersistenceProviderElement.cs
- TextTreeInsertElementUndoUnit.cs
- TrackingQuery.cs
- CompositeDataBoundControl.cs
- NativeConfigurationLoader.cs
- TransformerTypeCollection.cs
- CqlWriter.cs
- TableCellAutomationPeer.cs
- DataControlPagerLinkButton.cs
- Config.cs
- DateTimeOffsetConverter.cs
- SafeRightsManagementSessionHandle.cs
- OperatingSystem.cs
- Line.cs
- FontFamilyValueSerializer.cs
- EncryptedType.cs
- ToolboxItemAttribute.cs
- Monitor.cs
- CaseStatement.cs
- MemoryStream.cs
- PropertyConverter.cs
- WeakEventManager.cs
- ItemCheckEvent.cs
- BrowserCapabilitiesFactory.cs
- CompoundFileStorageReference.cs
- EnumUnknown.cs
- OutputCacheProfile.cs
- ProgramNode.cs
- SqlBuilder.cs
- TrackingParameters.cs
- SrgsElement.cs
- HtmlTernaryTree.cs
- QueryStringConverter.cs
- ValueTypeFixupInfo.cs
- ValidationHelper.cs
- EndpointDispatcherTable.cs
- StringAnimationUsingKeyFrames.cs
- Converter.cs
- RoleManagerSection.cs
- Wrapper.cs
- GPRECTF.cs
- PackagingUtilities.cs
- TableMethodGenerator.cs
- Padding.cs
- ChangeNode.cs
- FixedSOMElement.cs
- HashHelper.cs
- DbConnectionPoolGroup.cs
- CodeExporter.cs
- ToggleProviderWrapper.cs
- BidirectionalDictionary.cs
- Helper.cs
- TransactionProxy.cs
- Stylesheet.cs
- BitArray.cs
- ListViewVirtualItemsSelectionRangeChangedEvent.cs
- DataGridViewCellCollection.cs
- DockPattern.cs
- ValueUnavailableException.cs
- SHA256.cs
- FunctionDescription.cs
- PageThemeBuildProvider.cs
- BoolLiteral.cs
- ProfileInfo.cs
- HttpServerVarsCollection.cs