Skip to content

Refactor HttpRuntime usage to minimize statics usage #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace System.Web.Caching;
public class CacheDependency : IDisposable
{
private readonly List<ChangeMonitor> changeMonitors = new();
private bool hasChanged;
private bool disposedValue;
private DateTime utcLastModified;
private Action<object, EventArgs>? dependencyChangedAction;
Expand Down Expand Up @@ -56,8 +55,7 @@ public CacheDependency(

if (cachekeys is not null && cachekeys.Length != 0)
{
changeMonitors.Add(HttpRuntime.Cache.ObjectCache
.CreateCacheEntryChangeMonitor(cachekeys));
changeMonitors.Add(HttpRuntime.Cache.ObjectCache.CreateCacheEntryChangeMonitor(cachekeys));
}

if (dependency is not null)
Expand All @@ -70,9 +68,9 @@ public CacheDependency(

protected internal void FinishInit()
{
hasChanged = changeMonitors.Any(cm => cm.HasChanged && (cm.GetLastModifiedUtc() > utcStart));
HasChanged = changeMonitors.Any(cm => cm.HasChanged && (cm.GetLastModifiedUtc() > utcStart));
utcLastModified = changeMonitors.Max(cm => cm.GetLastModifiedUtc());
if (hasChanged)
if (HasChanged)
{
NotifyDependencyChanged(this, EventArgs.Empty);
}
Expand All @@ -93,7 +91,7 @@ protected void NotifyDependencyChanged(object sender, EventArgs e)
{
if (initCompleted && DateTime.UtcNow > utcStart)
{
hasChanged = true;
HasChanged = true;
utcLastModified = DateTime.UtcNow;
dependencyChangedAction?.Invoke(sender, e);
}
Expand All @@ -104,9 +102,9 @@ protected void NotifyDependencyChanged(object sender, EventArgs e)
public void SetCacheDependencyChanged(Action<object, EventArgs> dependencyChangedAction) =>
this.dependencyChangedAction = dependencyChangedAction;

public virtual string[] GetFileDependencies() => changeMonitors.OfType<FileChangeMonitor>().SelectMany(cm=>cm.FilePaths).ToArray();
public virtual string[] GetFileDependencies() => changeMonitors.OfType<FileChangeMonitor>().SelectMany(cm => cm.FilePaths).ToArray();

public bool HasChanged => hasChanged;
public bool HasChanged { get; private set; }

public DateTime UtcLastModified => changeMonitors
.OfType<FileChangeMonitor>()
Expand All @@ -116,7 +114,8 @@ public void SetCacheDependencyChanged(Action<object, EventArgs> dependencyChange

public virtual string? GetUniqueID()
{
if (!uniqueIdInitialized) {
if (!uniqueIdInitialized)
{
uniqueId = changeMonitors.Any(cm => cm.UniqueId is null) ?
null :
string.Join(":", changeMonitors.Select(cm => cm.UniqueId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
#pragma warning disable CS0809 // Obsolete member overrides non-obsolete member
#pragma warning disable CA1063 // Implement IDisposable Correctly
#pragma warning disable CA1816 // Dispose methods should call SuppressFinalize

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#pragma warning disable CS0809 // Obsolete member overrides non-obsolete member
#pragma warning disable CA1063 // Implement IDisposable Correctly
#pragma warning disable CA1816 // Dispose methods should call SuppressFinalize

namespace System.Web
{
public partial class HttpBrowserCapabilities : System.Web.Configuration.HttpCapabilitiesBase
Expand Down Expand Up @@ -547,7 +548,7 @@ public partial class CacheDependency : System.IDisposable
public CacheDependency(string[] filenames, System.DateTime start) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public CacheDependency(string[] filenames, string[] cachekeys, System.DateTime start) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public CacheDependency(string[] filenames, string[] cachekeys, System.Web.Caching.CacheDependency dependency, System.DateTime start) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public bool HasChanged { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public bool HasChanged { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public System.DateTime UtcLastModified { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
protected virtual void DependencyDispose() { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public void Dispose() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#pragma warning disable CS0809 // Obsolete member overrides non-obsolete member
#pragma warning disable CA1063 // Implement IDisposable Correctly
#pragma warning disable CA1816 // Dispose methods should call SuppressFinalize

[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpBrowserCapabilities))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpBrowserCapabilitiesBase))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpBrowserCapabilitiesWrapper))]
Expand Down
9 changes: 7 additions & 2 deletions src/Microsoft.AspNetCore.SystemWebAdapters/HttpRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public sealed class HttpRuntime
{
private static IHttpRuntime? _current;

/// <summary>
/// Gets the current <see cref="IHttpRuntime"/>. This should not be used internally besides where is strictly necessary.
/// If this is needed, it should be retrieved through dependency injection.
/// </summary>
internal static IHttpRuntime Current
{
get => _current ?? throw new InvalidOperationException("HttpRuntime is not available in the current environment");
Expand All @@ -20,7 +24,8 @@ private HttpRuntime()
}

public static string AppDomainAppVirtualPath => Current.AppDomainAppVirtualPath;
public static string AppDomainAppPath => Current.AppDomainAppPath;

public static System.Web.Caching.Cache Cache => Current.Cache;
public static string AppDomainAppPath => Current.AppDomainAppPath;

public static Caching.Cache Cache => Current.Cache;
}
29 changes: 19 additions & 10 deletions src/Microsoft.AspNetCore.SystemWebAdapters/HttpServerUtility.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Hosting;
using System.IO;
using Microsoft.AspNetCore.SystemWebAdapters;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -21,16 +22,24 @@ internal HttpServerUtility(HttpContextCore context)

public string MapPath(string? path)
{
var appPath = (string.IsNullOrEmpty(path) ? VirtualPathUtility.GetDirectory(_context.Request.Path) :
VirtualPathUtility.Combine(
VirtualPathUtility.GetDirectory(_context.Request.Path) ?? "/"
, path));
var rootPath = HttpRuntime.AppDomainAppPath;
if (string.IsNullOrEmpty(appPath)) return rootPath;
return System.IO.Path.Combine(rootPath,
var runtime = _context.RequestServices.GetRequiredService<IHttpRuntime>();

var appPath = string.IsNullOrEmpty(path)
? VirtualPathUtilityImpl.GetDirectory(_context.Request.Path)
: new VirtualPathUtilityImpl(runtime).Combine(VirtualPathUtilityImpl.GetDirectory(_context.Request.Path) ?? "/", path);

var rootPath = runtime.AppDomainAppPath;

if (string.IsNullOrEmpty(appPath))
{
return rootPath;
}

return Path.Combine(
rootPath,
appPath[1..]
.Replace('/', System.IO.Path.DirectorySeparatorChar))
.TrimEnd(System.IO.Path.DirectorySeparatorChar);
.Replace('/', Path.DirectorySeparatorChar))
.TrimEnd(Path.DirectorySeparatorChar);
}

[Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1024:Use properties where appropriate", Justification = Constants.ApiFromAspNet)]
Expand Down
Loading