Skip to content

Commit da6d7f4

Browse files
Copilotcaptainsafia
andcommitted
Revert framework to use IActionContextAccessor internally with ASPDEPR004 pragma disables
Co-authored-by: captainsafia <[email protected]>
1 parent e2c25f0 commit da6d7f4

25 files changed

+160
-40
lines changed

src/Mvc/Mvc.Core/src/Infrastructure/ActionContextAccessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure;
1010
/// <summary>
1111
/// Type that provides access to an <see cref="ActionContext"/>.
1212
/// </summary>
13-
[Obsolete("ActionContextAccessor is obsolete. Use IHttpContextAccessor instead and access the endpoint information from HttpContext.GetEndpoint(). This type will be removed in a future version.")]
13+
[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")]
1414
public class ActionContextAccessor : IActionContextAccessor
1515
{
1616
internal static readonly IActionContextAccessor Null = new NullActionContextAccessor();

src/Mvc/Mvc.Core/src/Infrastructure/ControllerActionInvoker.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ internal partial class ControllerActionInvoker : ResourceInvoker, IActionInvoker
3030
internal ControllerActionInvoker(
3131
ILogger logger,
3232
DiagnosticListener diagnosticListener,
33+
#pragma warning disable ASPDEPR004 // Type or member is obsolete
34+
IActionContextAccessor actionContextAccessor,
35+
#pragma warning restore ASPDEPR004 // Type or member is obsolete
3336
IActionResultTypeMapper mapper,
3437
ControllerContext controllerContext,
3538
ControllerActionInvokerCacheEntry cacheEntry,
3639
IFilterMetadata[] filters)
37-
: base(diagnosticListener, logger, mapper, controllerContext, filters, controllerContext.ValueProviderFactories)
40+
: base(diagnosticListener, logger, actionContextAccessor, mapper, controllerContext, filters, controllerContext.ValueProviderFactories)
3841
{
3942
ArgumentNullException.ThrowIfNull(cacheEntry);
4043

src/Mvc/Mvc.Core/src/Infrastructure/ControllerActionInvokerProvider.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,29 @@ internal sealed class ControllerActionInvokerProvider : IActionInvokerProvider
2323
private readonly ILogger _logger;
2424
private readonly DiagnosticListener _diagnosticListener;
2525
private readonly IActionResultTypeMapper _mapper;
26+
#pragma warning disable ASPDEPR004 // Type or member is obsolete
27+
private readonly IActionContextAccessor _actionContextAccessor;
28+
#pragma warning restore ASPDEPR004 // Type or member is obsolete
2629

2730
public ControllerActionInvokerProvider(
2831
ControllerActionInvokerCache controllerActionInvokerCache,
2932
IOptions<MvcOptions> optionsAccessor,
3033
ILoggerFactory loggerFactory,
3134
DiagnosticListener diagnosticListener,
3235
IActionResultTypeMapper mapper)
36+
: this(controllerActionInvokerCache, optionsAccessor, loggerFactory, diagnosticListener, mapper, null)
37+
{
38+
}
39+
40+
public ControllerActionInvokerProvider(
41+
ControllerActionInvokerCache controllerActionInvokerCache,
42+
IOptions<MvcOptions> optionsAccessor,
43+
ILoggerFactory loggerFactory,
44+
DiagnosticListener diagnosticListener,
45+
IActionResultTypeMapper mapper,
46+
#pragma warning disable ASPDEPR004 // Type or member is obsolete
47+
IActionContextAccessor? actionContextAccessor)
48+
#pragma warning restore ASPDEPR004 // Type or member is obsolete
3349
{
3450
_controllerActionInvokerCache = controllerActionInvokerCache;
3551
_valueProviderFactories = optionsAccessor.Value.ValueProviderFactories.ToArray();
@@ -39,6 +55,9 @@ public ControllerActionInvokerProvider(
3955
_logger = loggerFactory.CreateLogger(typeof(ControllerActionInvoker));
4056
_diagnosticListener = diagnosticListener;
4157
_mapper = mapper;
58+
#pragma warning disable ASPDEPR004 // Type or member is obsolete
59+
_actionContextAccessor = actionContextAccessor ?? ActionContextAccessor.Null;
60+
#pragma warning restore ASPDEPR004 // Type or member is obsolete
4261
}
4362

4463
public int Order => -1000;
@@ -64,6 +83,7 @@ public void OnProvidersExecuting(ActionInvokerProviderContext context)
6483
var invoker = new ControllerActionInvoker(
6584
_logger,
6685
_diagnosticListener,
86+
_actionContextAccessor,
6787
_mapper,
6888
controllerContext,
6989
cacheEntry,

src/Mvc/Mvc.Core/src/Infrastructure/IActionContextAccessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure;
1010
/// <summary>
1111
/// Defines an interface for exposing an <see cref="ActionContext"/>.
1212
/// </summary>
13-
[Obsolete("IActionContextAccessor is obsolete. Use IHttpContextAccessor instead and access the endpoint information from HttpContext.GetEndpoint(). This type will be removed in a future version.")]
13+
[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")]
1414
public interface IActionContextAccessor
1515
{
1616
/// <summary>

src/Mvc/Mvc.Core/src/Infrastructure/ResourceInvoker.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ internal abstract partial class ResourceInvoker
1515
{
1616
protected readonly DiagnosticListener _diagnosticListener;
1717
protected readonly ILogger _logger;
18+
#pragma warning disable ASPDEPR004 // Type or member is obsolete
19+
protected readonly IActionContextAccessor _actionContextAccessor;
20+
#pragma warning restore ASPDEPR004 // Type or member is obsolete
1821
protected readonly IActionResultTypeMapper _mapper;
1922
protected readonly ActionContext _actionContext;
2023
protected readonly IFilterMetadata[] _filters;
@@ -36,13 +39,17 @@ internal abstract partial class ResourceInvoker
3639
public ResourceInvoker(
3740
DiagnosticListener diagnosticListener,
3841
ILogger logger,
42+
#pragma warning disable ASPDEPR004 // Type or member is obsolete
43+
IActionContextAccessor actionContextAccessor,
44+
#pragma warning restore ASPDEPR004 // Type or member is obsolete
3945
IActionResultTypeMapper mapper,
4046
ActionContext actionContext,
4147
IFilterMetadata[] filters,
4248
IList<IValueProviderFactory> valueProviderFactories)
4349
{
4450
_diagnosticListener = diagnosticListener ?? throw new ArgumentNullException(nameof(diagnosticListener));
4551
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
52+
_actionContextAccessor = actionContextAccessor ?? throw new ArgumentNullException(nameof(actionContextAccessor));
4653
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
4754
_actionContext = actionContext ?? throw new ArgumentNullException(nameof(actionContext));
4855

@@ -58,6 +65,7 @@ public virtual Task InvokeAsync()
5865
return Logged(this);
5966
}
6067

68+
_actionContextAccessor.ActionContext = _actionContext;
6169
var scope = _logger.ActionScope(_actionContext.ActionDescriptor);
6270

6371
Task task;
@@ -92,6 +100,7 @@ static async Task Awaited(ResourceInvoker invoker, Task task, IDisposable? scope
92100
static async Task Logged(ResourceInvoker invoker)
93101
{
94102
var actionContext = invoker._actionContext;
103+
invoker._actionContextAccessor.ActionContext = actionContext;
95104
try
96105
{
97106
var logger = invoker._logger;

src/Mvc/Mvc.Core/src/Routing/ControllerRequestDelegateFactory.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ internal sealed class ControllerRequestDelegateFactory : IRequestDelegateFactory
2424
private readonly ILogger _logger;
2525
private readonly DiagnosticListener _diagnosticListener;
2626
private readonly IActionResultTypeMapper _mapper;
27+
#pragma warning disable ASPDEPR004 // Type or member is obsolete
28+
private readonly IActionContextAccessor _actionContextAccessor;
29+
#pragma warning restore ASPDEPR004 // Type or member is obsolete
2730
private readonly bool _enableActionInvokers;
2831

2932
public ControllerRequestDelegateFactory(
@@ -32,6 +35,19 @@ public ControllerRequestDelegateFactory(
3235
ILoggerFactory loggerFactory,
3336
DiagnosticListener diagnosticListener,
3437
IActionResultTypeMapper mapper)
38+
: this(controllerActionInvokerCache, optionsAccessor, loggerFactory, diagnosticListener, mapper, null)
39+
{
40+
}
41+
42+
public ControllerRequestDelegateFactory(
43+
ControllerActionInvokerCache controllerActionInvokerCache,
44+
IOptions<MvcOptions> optionsAccessor,
45+
ILoggerFactory loggerFactory,
46+
DiagnosticListener diagnosticListener,
47+
IActionResultTypeMapper mapper,
48+
#pragma warning disable ASPDEPR004 // Type or member is obsolete
49+
IActionContextAccessor? actionContextAccessor)
50+
#pragma warning restore ASPDEPR004 // Type or member is obsolete
3551
{
3652
_controllerActionInvokerCache = controllerActionInvokerCache;
3753
_valueProviderFactories = optionsAccessor.Value.ValueProviderFactories.ToArray();
@@ -42,6 +58,9 @@ public ControllerRequestDelegateFactory(
4258
_logger = loggerFactory.CreateLogger<ControllerActionInvoker>();
4359
_diagnosticListener = diagnosticListener;
4460
_mapper = mapper;
61+
#pragma warning disable ASPDEPR004 // Type or member is obsolete
62+
_actionContextAccessor = actionContextAccessor ?? ActionContextAccessor.Null;
63+
#pragma warning restore ASPDEPR004 // Type or member is obsolete
4564
}
4665

4766
public RequestDelegate? CreateRequestDelegate(ActionDescriptor actionDescriptor, RouteValueDictionary? dataTokens)
@@ -81,6 +100,7 @@ public ControllerRequestDelegateFactory(
81100
var invoker = new ControllerActionInvoker(
82101
_logger,
83102
_diagnosticListener,
103+
_actionContextAccessor,
84104
_mapper,
85105
controllerContext,
86106
cacheEntry,

src/Mvc/Mvc.Core/test/Filters/MiddlewareFilterTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ public TestControllerActionInvoker(
398398
: base(
399399
logger,
400400
diagnosticListener,
401+
#pragma warning disable ASPDEPR004 // Type or member is obsolete
402+
ActionContextAccessor.Null,
403+
#pragma warning restore ASPDEPR004 // Type or member is obsolete
401404
mapper,
402405
CreateControllerContext(actionContext, valueProviderFactories, maxAllowedErrorsInModelState),
403406
CreateCacheEntry((ControllerActionDescriptor)actionContext.ActionDescriptor, controllerFactory),

src/Mvc/Mvc.Core/test/Infrastructure/ControllerActionInvokerTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,9 @@ public async Task Invoke_UsesDefaultValuesIfNotBound()
14321432
var invoker = new ControllerActionInvoker(
14331433
new NullLoggerFactory().CreateLogger<ControllerActionInvoker>(),
14341434
new DiagnosticListener("Microsoft.AspNetCore"),
1435+
#pragma warning disable ASPDEPR004 // Type or member is obsolete
1436+
ActionContextAccessor.Null,
1437+
#pragma warning restore ASPDEPR004 // Type or member is obsolete
14351438
new ActionResultTypeMapper(),
14361439
controllerContext,
14371440
cacheEntry,
@@ -1768,6 +1771,9 @@ private ControllerActionInvoker CreateInvoker(
17681771
var invoker = new ControllerActionInvoker(
17691772
logger,
17701773
diagnosticSource,
1774+
#pragma warning disable ASPDEPR004 // Type or member is obsolete
1775+
ActionContextAccessor.Null,
1776+
#pragma warning restore ASPDEPR004 // Type or member is obsolete
17711777
new ActionResultTypeMapper(),
17721778
controllerContext,
17731779
cacheEntry,

src/Mvc/Mvc.RazorPages/src/Infrastructure/PageActionInvoker.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public PageActionInvoker(
3535
IPageHandlerMethodSelector handlerMethodSelector,
3636
DiagnosticListener diagnosticListener,
3737
ILogger logger,
38+
#pragma warning disable ASPDEPR004 // Type or member is obsolete
39+
IActionContextAccessor actionContextAccessor,
40+
#pragma warning restore ASPDEPR004 // Type or member is obsolete
3841
IActionResultTypeMapper mapper,
3942
PageContext pageContext,
4043
IFilterMetadata[] filterMetadata,
@@ -44,6 +47,7 @@ public PageActionInvoker(
4447
: base(
4548
diagnosticListener,
4649
logger,
50+
actionContextAccessor,
4751
mapper,
4852
pageContext,
4953
filterMetadata,

src/Mvc/Mvc.RazorPages/src/Infrastructure/PageActionInvokerProvider.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ internal sealed class PageActionInvokerProvider : IActionInvokerProvider
2525
private readonly DiagnosticListener _diagnosticListener;
2626
private readonly ILogger<PageActionInvoker> _logger;
2727
private readonly IActionResultTypeMapper _mapper;
28+
#pragma warning disable ASPDEPR004 // Type or member is obsolete
29+
private readonly IActionContextAccessor _actionContextAccessor;
30+
#pragma warning restore ASPDEPR004 // Type or member is obsolete
2831

2932
public PageActionInvokerProvider(
3033
PageLoader pageLoader,
@@ -36,7 +39,10 @@ public PageActionInvokerProvider(
3639
IPageHandlerMethodSelector selector,
3740
DiagnosticListener diagnosticListener,
3841
ILoggerFactory loggerFactory,
39-
IActionResultTypeMapper mapper)
42+
IActionResultTypeMapper mapper,
43+
#pragma warning disable ASPDEPR004 // Type or member is obsolete
44+
IActionContextAccessor? actionContextAccessor = null)
45+
#pragma warning restore ASPDEPR004 // Type or member is obsolete
4046
{
4147
_pageLoader = pageLoader;
4248
_pageActionInvokerCache = pageActionInvokerCache;
@@ -48,6 +54,9 @@ public PageActionInvokerProvider(
4854
_diagnosticListener = diagnosticListener;
4955
_logger = loggerFactory.CreateLogger<PageActionInvoker>();
5056
_mapper = mapper;
57+
#pragma warning disable ASPDEPR004 // Type or member is obsolete
58+
_actionContextAccessor = actionContextAccessor ?? ActionContextAccessor.Null;
59+
#pragma warning restore ASPDEPR004 // Type or member is obsolete
5160
}
5261

5362
// For testing
@@ -87,6 +96,7 @@ public void OnProvidersExecuting(ActionInvokerProviderContext context)
8796
_selector,
8897
_diagnosticListener,
8998
_logger,
99+
_actionContextAccessor,
90100
_mapper,
91101
pageContext,
92102
filters,

0 commit comments

Comments
 (0)