Skip to content

Deprecate IActionContextAccessor #62647

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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,6 +10,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure;
/// <summary>
/// Type that provides access to an <see cref="ActionContext"/>.
/// </summary>
[Obsolete("ActionContextAccessor is obsolete. Use IHttpContextAccessor instead and access the endpoint information from HttpContext.GetEndpoint(). This type will be removed in a future version.", DiagnosticId = "ASPDEPR004")]
public class ActionContextAccessor : IActionContextAccessor
{
internal static readonly IActionContextAccessor Null = new NullActionContextAccessor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ internal partial class ControllerActionInvoker : ResourceInvoker, IActionInvoker
internal ControllerActionInvoker(
ILogger logger,
DiagnosticListener diagnosticListener,
#pragma warning disable ASPDEPR004 // Type or member is obsolete
IActionContextAccessor actionContextAccessor,
#pragma warning restore ASPDEPR004 // Type or member is obsolete
IActionResultTypeMapper mapper,
ControllerContext controllerContext,
ControllerActionInvokerCacheEntry cacheEntry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ internal sealed class ControllerActionInvokerProvider : IActionInvokerProvider
private readonly ILogger _logger;
private readonly DiagnosticListener _diagnosticListener;
private readonly IActionResultTypeMapper _mapper;
#pragma warning disable ASPDEPR004 // Type or member is obsolete
private readonly IActionContextAccessor _actionContextAccessor;
#pragma warning restore ASPDEPR004 // Type or member is obsolete

public ControllerActionInvokerProvider(
ControllerActionInvokerCache controllerActionInvokerCache,
Expand All @@ -41,7 +43,9 @@ public ControllerActionInvokerProvider(
ILoggerFactory loggerFactory,
DiagnosticListener diagnosticListener,
IActionResultTypeMapper mapper,
#pragma warning disable ASPDEPR004 // Type or member is obsolete
IActionContextAccessor? actionContextAccessor)
#pragma warning restore ASPDEPR004 // Type or member is obsolete
{
_controllerActionInvokerCache = controllerActionInvokerCache;
_valueProviderFactories = optionsAccessor.Value.ValueProviderFactories.ToArray();
Expand All @@ -51,7 +55,9 @@ public ControllerActionInvokerProvider(
_logger = loggerFactory.CreateLogger(typeof(ControllerActionInvoker));
_diagnosticListener = diagnosticListener;
_mapper = mapper;
#pragma warning disable ASPDEPR004 // Type or member is obsolete
_actionContextAccessor = actionContextAccessor ?? ActionContextAccessor.Null;
#pragma warning restore ASPDEPR004 // Type or member is obsolete
}

public int Order => -1000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure;
/// <summary>
/// Defines an interface for exposing an <see cref="ActionContext"/>.
/// </summary>
[Obsolete("IActionContextAccessor is obsolete. Use IHttpContextAccessor instead and access the endpoint information from HttpContext.GetEndpoint(). This type will be removed in a future version.", DiagnosticId = "ASPDEPR004")]
public interface IActionContextAccessor
{
/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions src/Mvc/Mvc.Core/src/Infrastructure/ResourceInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ internal abstract partial class ResourceInvoker
{
protected readonly DiagnosticListener _diagnosticListener;
protected readonly ILogger _logger;
#pragma warning disable ASPDEPR004 // Type or member is obsolete
protected readonly IActionContextAccessor _actionContextAccessor;
#pragma warning restore ASPDEPR004 // Type or member is obsolete
protected readonly IActionResultTypeMapper _mapper;
protected readonly ActionContext _actionContext;
protected readonly IFilterMetadata[] _filters;
Expand All @@ -37,7 +39,9 @@ internal abstract partial class ResourceInvoker
public ResourceInvoker(
DiagnosticListener diagnosticListener,
ILogger logger,
#pragma warning disable ASPDEPR004 // Type or member is obsolete
IActionContextAccessor actionContextAccessor,
#pragma warning restore ASPDEPR004 // Type or member is obsolete
IActionResultTypeMapper mapper,
ActionContext actionContext,
IFilterMetadata[] filters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ internal sealed class ControllerRequestDelegateFactory : IRequestDelegateFactory
private readonly ILogger _logger;
private readonly DiagnosticListener _diagnosticListener;
private readonly IActionResultTypeMapper _mapper;
#pragma warning disable ASPDEPR004 // Type or member is obsolete
private readonly IActionContextAccessor _actionContextAccessor;
#pragma warning restore ASPDEPR004 // Type or member is obsolete
private readonly bool _enableActionInvokers;

public ControllerRequestDelegateFactory(
Expand All @@ -43,7 +45,9 @@ public ControllerRequestDelegateFactory(
ILoggerFactory loggerFactory,
DiagnosticListener diagnosticListener,
IActionResultTypeMapper mapper,
#pragma warning disable ASPDEPR004 // Type or member is obsolete
IActionContextAccessor? actionContextAccessor)
#pragma warning restore ASPDEPR004 // Type or member is obsolete
{
_controllerActionInvokerCache = controllerActionInvokerCache;
_valueProviderFactories = optionsAccessor.Value.ValueProviderFactories.ToArray();
Expand All @@ -54,7 +58,9 @@ public ControllerRequestDelegateFactory(
_logger = loggerFactory.CreateLogger<ControllerActionInvoker>();
_diagnosticListener = diagnosticListener;
_mapper = mapper;
#pragma warning disable ASPDEPR004 // Type or member is obsolete
_actionContextAccessor = actionContextAccessor ?? ActionContextAccessor.Null;
#pragma warning restore ASPDEPR004 // Type or member is obsolete
}

public RequestDelegate? CreateRequestDelegate(ActionDescriptor actionDescriptor, RouteValueDictionary? dataTokens)
Expand Down
2 changes: 2 additions & 0 deletions src/Mvc/Mvc.Core/test/Filters/MiddlewareFilterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,9 @@ public TestControllerActionInvoker(
: base(
logger,
diagnosticListener,
#pragma warning disable ASPDEPR004 // Type or member is obsolete
ActionContextAccessor.Null,
#pragma warning restore ASPDEPR004 // Type or member is obsolete
mapper,
CreateControllerContext(actionContext, valueProviderFactories, maxAllowedErrorsInModelState),
CreateCacheEntry((ControllerActionDescriptor)actionContext.ActionDescriptor, controllerFactory),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,9 @@ public async Task Invoke_UsesDefaultValuesIfNotBound()
var invoker = new ControllerActionInvoker(
new NullLoggerFactory().CreateLogger<ControllerActionInvoker>(),
new DiagnosticListener("Microsoft.AspNetCore"),
#pragma warning disable ASPDEPR004 // Type or member is obsolete
ActionContextAccessor.Null,
#pragma warning restore ASPDEPR004 // Type or member is obsolete
new ActionResultTypeMapper(),
controllerContext,
cacheEntry,
Expand Down Expand Up @@ -1769,7 +1771,9 @@ private ControllerActionInvoker CreateInvoker(
var invoker = new ControllerActionInvoker(
logger,
diagnosticSource,
#pragma warning disable ASPDEPR004 // Type or member is obsolete
ActionContextAccessor.Null,
#pragma warning restore ASPDEPR004 // Type or member is obsolete
new ActionResultTypeMapper(),
controllerContext,
cacheEntry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public PageActionInvoker(
IPageHandlerMethodSelector handlerMethodSelector,
DiagnosticListener diagnosticListener,
ILogger logger,
#pragma warning disable ASPDEPR004 // Type or member is obsolete
IActionContextAccessor actionContextAccessor,
#pragma warning restore ASPDEPR004 // Type or member is obsolete
IActionResultTypeMapper mapper,
PageContext pageContext,
IFilterMetadata[] filterMetadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ internal sealed class PageActionInvokerProvider : IActionInvokerProvider
private readonly DiagnosticListener _diagnosticListener;
private readonly ILogger<PageActionInvoker> _logger;
private readonly IActionResultTypeMapper _mapper;
#pragma warning disable ASPDEPR004 // Type or member is obsolete
private readonly IActionContextAccessor _actionContextAccessor;
#pragma warning restore ASPDEPR004 // Type or member is obsolete

public PageActionInvokerProvider(
PageLoader pageLoader,
Expand All @@ -38,7 +40,9 @@ public PageActionInvokerProvider(
DiagnosticListener diagnosticListener,
ILoggerFactory loggerFactory,
IActionResultTypeMapper mapper,
#pragma warning disable ASPDEPR004 // Type or member is obsolete
IActionContextAccessor? actionContextAccessor = null)
#pragma warning restore ASPDEPR004 // Type or member is obsolete
{
_pageLoader = pageLoader;
_pageActionInvokerCache = pageActionInvokerCache;
Expand All @@ -50,7 +54,9 @@ public PageActionInvokerProvider(
_diagnosticListener = diagnosticListener;
_logger = loggerFactory.CreateLogger<PageActionInvoker>();
_mapper = mapper;
#pragma warning disable ASPDEPR004 // Type or member is obsolete
_actionContextAccessor = actionContextAccessor ?? ActionContextAccessor.Null;
#pragma warning restore ASPDEPR004 // Type or member is obsolete
}

// For testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ internal sealed class PageRequestDelegateFactory : IRequestDelegateFactory
private readonly DiagnosticListener _diagnosticListener;
private readonly ILogger<PageActionInvoker> _logger;
private readonly IActionResultTypeMapper _mapper;
#pragma warning disable ASPDEPR004 // Type or member is obsolete
private readonly IActionContextAccessor _actionContextAccessor;
#pragma warning restore ASPDEPR004 // Type or member is obsolete
private readonly bool _enableActionInvokers;

public PageRequestDelegateFactory(
Expand All @@ -53,7 +55,9 @@ public PageRequestDelegateFactory(
DiagnosticListener diagnosticListener,
ILoggerFactory loggerFactory,
IActionResultTypeMapper mapper,
#pragma warning disable ASPDEPR004 // Type or member is obsolete
IActionContextAccessor? actionContextAccessor)
#pragma warning restore ASPDEPR004 // Type or member is obsolete
{
_cache = cache;
_valueProviderFactories = mvcOptions.Value.ValueProviderFactories.ToArray();
Expand All @@ -65,7 +69,9 @@ public PageRequestDelegateFactory(
_diagnosticListener = diagnosticListener;
_logger = loggerFactory.CreateLogger<PageActionInvoker>();
_mapper = mapper;
#pragma warning disable ASPDEPR004 // Type or member is obsolete
_actionContextAccessor = actionContextAccessor ?? ActionContextAccessor.Null;
#pragma warning restore ASPDEPR004 // Type or member is obsolete
}

public RequestDelegate? CreateRequestDelegate(ActionDescriptor actionDescriptor, RouteValueDictionary? dataTokens)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1565,7 +1565,9 @@ object pageFactory(PageContext context, ViewContext viewContext)
selector.Object,
diagnosticListener ?? new DiagnosticListener("Microsoft.AspNetCore"),
logger ?? NullLogger.Instance,
#pragma warning disable ASPDEPR004 // Type or member is obsolete
ActionContextAccessor.Null,
#pragma warning restore ASPDEPR004 // Type or member is obsolete
new ActionResultTypeMapper(),
pageContext,
filters ?? Array.Empty<IFilterMetadata>(),
Expand Down
2 changes: 2 additions & 0 deletions src/Mvc/test/WebSites/BasicWebSite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public void ConfigureServices(IServiceCollection services)
services.AddHttpContextAccessor();
services.AddScoped<RequestIdService>();
services.AddScoped<TestResponseGenerator>();
#pragma warning disable ASPDEPR004 // Type or member is obsolete
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
#pragma warning restore ASPDEPR004 // Type or member is obsolete
}

public void Configure(IApplicationBuilder app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public void ConfigureServices(IServiceCollection services)
services.AddScoped<RequestIdService>();
services.AddTransient<ServiceActionFilter>();
services.AddScoped<TestResponseGenerator>();
#pragma warning disable ASPDEPR004 // Type or member is obsolete
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
#pragma warning restore ASPDEPR004 // Type or member is obsolete
services.TryAddSingleton(CreateWeatherForecastService);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Mvc/test/WebSites/Common/TestResponseGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class TestResponseGenerator
{
private readonly ActionContext _actionContext;

#pragma warning disable ASPDEPR004 // Type or member is obsolete
public TestResponseGenerator(IActionContextAccessor contextAccessor)
#pragma warning restore ASPDEPR004 // Type or member is obsolete
{
_actionContext = contextAccessor.ActionContext;
if (_actionContext == null)
Expand Down
2 changes: 2 additions & 0 deletions src/Mvc/test/WebSites/GenericHostWebSite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public void ConfigureServices(IServiceCollection services)
services.AddLogging();
services.AddHttpContextAccessor();
services.AddScoped<TestResponseGenerator>();
#pragma warning disable ASPDEPR004 // Type or member is obsolete
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
#pragma warning restore ASPDEPR004 // Type or member is obsolete
}

public void Configure(IApplicationBuilder app)
Expand Down
2 changes: 2 additions & 0 deletions src/Mvc/test/WebSites/RoutingWebSite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public void ConfigureServices(IServiceCollection services)
ConfigureRoutingServices(services);

services.AddScoped<TestResponseGenerator>();
#pragma warning disable ASPDEPR004 // Type or member is obsolete
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
#pragma warning restore ASPDEPR004 // Type or member is obsolete
}

public virtual void Configure(IApplicationBuilder app)
Expand Down
2 changes: 2 additions & 0 deletions src/Mvc/test/WebSites/RoutingWebSite/StartupForDynamic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public void ConfigureServices(IServiceCollection services)

services.AddTransient<Transformer>();
services.AddScoped<TestResponseGenerator>();
#pragma warning disable ASPDEPR004 // Type or member is obsolete
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
#pragma warning restore ASPDEPR004 // Type or member is obsolete

// Used by some controllers defined in this project.
services.Configure<RouteOptions>(options => options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public void ConfigureServices(IServiceCollection services)

services.AddTransient<Transformer>();
services.AddScoped<TestResponseGenerator>();
#pragma warning disable ASPDEPR004 // Type or member is obsolete
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
#pragma warning restore ASPDEPR004 // Type or member is obsolete

// Used by some controllers defined in this project.
services.Configure<RouteOptions>(options => options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public void ConfigureServices(IServiceCollection services)
services.Configure<RouteOptions>(options => options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer));
services.AddScoped<TestResponseGenerator>();
// This is used by test response generator
#pragma warning disable ASPDEPR004 // Type or member is obsolete
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
#pragma warning restore ASPDEPR004 // Type or member is obsolete
}

public virtual void Configure(IApplicationBuilder app)
Expand Down
2 changes: 2 additions & 0 deletions src/Mvc/test/WebSites/RoutingWebSite/StartupForFallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public void ConfigureServices(IServiceCollection services)
.AddNewtonsoftJson();

services.AddScoped<TestResponseGenerator>();
#pragma warning disable ASPDEPR004 // Type or member is obsolete
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
#pragma warning restore ASPDEPR004 // Type or member is obsolete

// Used by some controllers defined in this project.
services.Configure<RouteOptions>(options => options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer));
Expand Down
2 changes: 2 additions & 0 deletions src/Mvc/test/WebSites/RoutingWebSite/StartupForGroups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public void ConfigureServices(IServiceCollection services)
services.Configure<RouteOptions>(options => options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer));
services.AddScoped<TestResponseGenerator>();
// This is used by test response generator
#pragma warning disable ASPDEPR004 // Type or member is obsolete
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
#pragma warning restore ASPDEPR004 // Type or member is obsolete
}

public virtual void Configure(IApplicationBuilder app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public void ConfigureServices(IServiceCollection services)
});

services.AddScoped<TestResponseGenerator>();
#pragma warning disable ASPDEPR004 // Type or member is obsolete
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
#pragma warning restore ASPDEPR004 // Type or member is obsolete
}

public void Configure(IApplicationBuilder app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public void ConfigureServices(IServiceCollection services)

services.AddScoped<TestResponseGenerator>();
// This is used by test response generator
#pragma warning disable ASPDEPR004 // Type or member is obsolete
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
#pragma warning restore ASPDEPR004 // Type or member is obsolete
services.AddSingleton<BranchesTransformer>();
}

Expand Down
2 changes: 2 additions & 0 deletions src/Mvc/test/WebSites/VersioningWebSite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public void ConfigureServices(IServiceCollection services)
.AddNewtonsoftJson();

services.AddScoped<TestResponseGenerator>();
#pragma warning disable ASPDEPR004 // Type or member is obsolete
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
#pragma warning restore ASPDEPR004 // Type or member is obsolete
}

public virtual void Configure(IApplicationBuilder app)
Expand Down
Loading