diff --git a/src/Mvc/Mvc.Core/src/Infrastructure/ResourceInvoker.cs b/src/Mvc/Mvc.Core/src/Infrastructure/ResourceInvoker.cs index 6379295394..7be49bfda6 100644 --- a/src/Mvc/Mvc.Core/src/Infrastructure/ResourceInvoker.cs +++ b/src/Mvc/Mvc.Core/src/Infrastructure/ResourceInvoker.cs @@ -14,6 +14,7 @@ using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.Mvc.Infrastructure { +#nullable enable internal abstract class ResourceInvoker { protected readonly DiagnosticListener _diagnosticListener; @@ -24,18 +25,18 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure protected readonly IFilterMetadata[] _filters; protected readonly IList _valueProviderFactories; - private AuthorizationFilterContextSealed _authorizationContext; - private ResourceExecutingContextSealed _resourceExecutingContext; - private ResourceExecutedContextSealed _resourceExecutedContext; - private ExceptionContextSealed _exceptionContext; - private ResultExecutingContextSealed _resultExecutingContext; - private ResultExecutedContextSealed _resultExecutedContext; + private AuthorizationFilterContextSealed? _authorizationContext; + private ResourceExecutingContextSealed? _resourceExecutingContext; + private ResourceExecutedContextSealed? _resourceExecutedContext; + private ExceptionContextSealed? _exceptionContext; + private ResultExecutingContextSealed? _resultExecutingContext; + private ResultExecutedContextSealed? _resultExecutedContext; // Do not make this readonly, it's mutable. We don't want to make a copy. // https://blogs.msdn.microsoft.com/ericlippert/2008/05/14/mutating-readonly-structs/ protected FilterCursor _cursor; - protected IActionResult _result; - protected object _instance; + protected IActionResult? _result; + protected object? _instance; public ResourceInvoker( DiagnosticListener diagnosticListener, @@ -67,7 +68,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure _actionContextAccessor.ActionContext = _actionContext; var scope = _logger.ActionScope(_actionContext.ActionDescriptor); - Task task = null; + Task task; try { task = InvokeFilterPipelineAsync(); @@ -77,13 +78,12 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure return Awaited(this, Task.FromException(exception), scope); } - Debug.Assert(task != null); if (!task.IsCompletedSuccessfully) { return Awaited(this, task, scope); } - Exception releaseException = null; + Exception? releaseException = null; try { ReleaseResources(); @@ -93,10 +93,10 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure releaseException = exception; } - Exception scopeException = null; + Exception? scopeException = null; try { - scope.Dispose(); + scope?.Dispose(); } catch (Exception exception) { @@ -120,7 +120,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure return Task.FromException(scopeException); } - static async Task Awaited(ResourceInvoker invoker, Task task, IDisposable scope) + static async Task Awaited(ResourceInvoker invoker, Task task, IDisposable? scope) { try { @@ -135,7 +135,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure } finally { - scope.Dispose(); + scope?.Dispose(); } } @@ -204,7 +204,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure // The `state` is used for internal state handling during transitions between states. In practice this // means storing a filter instance in `state` and then retrieving it in the next state. - var state = (object)null; + var state = (object?)null; // `isCompleted` will be set to true when we've reached a terminal state. var isCompleted = false; @@ -228,7 +228,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure return Task.FromException(ex); } - static async Task Awaited(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) + static async Task Awaited(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object? state, bool isCompleted) { await lastTask; @@ -269,7 +269,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure } } - private Task Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted) + private Task Next(ref State next, ref Scope scope, ref object? state, ref bool isCompleted) { switch (next) { @@ -868,7 +868,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure } Debug.Assert(scope == Scope.Invoker); - Rethrow(_resourceExecutedContext); + Rethrow(_resourceExecutedContext!); goto case State.InvokeEnd; } @@ -929,7 +929,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure { var scope = Scope.Resource; var next = State.ResourceNext; - var state = (object)null; + var state = (object?)null; var isCompleted = false; while (!isCompleted) @@ -943,7 +943,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure } catch (Exception exception) { - _resourceExecutedContext = new ResourceExecutedContextSealed(_resourceExecutingContext, _filters) + _resourceExecutedContext = new ResourceExecutedContextSealed(_resourceExecutingContext!, _filters) { ExceptionDispatchInfo = ExceptionDispatchInfo.Capture(exception), }; @@ -952,7 +952,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure Debug.Assert(_resourceExecutedContext != null); return Task.CompletedTask; - static async Task Awaited(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) + static async Task Awaited(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object? state, bool isCompleted) { try { @@ -965,7 +965,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure } catch (Exception exception) { - invoker._resourceExecutedContext = new ResourceExecutedContextSealed(invoker._resourceExecutingContext, invoker._filters) + invoker._resourceExecutedContext = new ResourceExecutedContextSealed(invoker._resourceExecutingContext!, invoker._filters) { ExceptionDispatchInfo = ExceptionDispatchInfo.Capture(exception), }; @@ -980,7 +980,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure try { var next = State.ExceptionNext; - var state = (object)null; + var state = (object?)null; var scope = Scope.Exception; var isCompleted = false; @@ -1002,7 +1002,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure return Task.FromException(ex); } - static async Task Awaited(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) + static async Task Awaited(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object? state, bool isCompleted) { try { @@ -1029,7 +1029,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure { var next = State.ResultBegin; var scope = Scope.Invoker; - var state = (object)null; + var state = (object?)null; var isCompleted = false; while (!isCompleted) @@ -1050,7 +1050,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure return Task.FromException(ex); } - static async Task Awaited(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) + static async Task Awaited(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object? state, bool isCompleted) { await lastTask; @@ -1067,7 +1067,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure { var next = State.ResultBegin; var scope = Scope.Invoker; - var state = (object)null; + var state = (object?)null; var isCompleted = false; while (!isCompleted) @@ -1088,7 +1088,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure return Task.FromException(ex); } - static async Task Awaited(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) + static async Task Awaited(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object? state, bool isCompleted) { await lastTask; @@ -1099,7 +1099,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure } } - private Task ResultNext(ref State next, ref Scope scope, ref object state, ref bool isCompleted) + private Task ResultNext(ref State next, ref Scope scope, ref object? state, ref bool isCompleted) where TFilter : class, IResultFilter where TFilterAsync : class, IAsyncResultFilter { @@ -1122,7 +1122,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure { if (_resultExecutingContext == null) { - _resultExecutingContext = new ResultExecutingContextSealed(_actionContext, _filters, _result, _instance); + _resultExecutingContext = new ResultExecutingContextSealed(_actionContext, _filters, _result!, _instance!); } state = current.FilterAsync; @@ -1132,7 +1132,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure { if (_resultExecutingContext == null) { - _resultExecutingContext = new ResultExecutingContextSealed(_actionContext, _filters, _result, _instance); + _resultExecutingContext = new ResultExecutingContextSealed(_actionContext, _filters, _result!, _instance!); } state = current.Filter; @@ -1186,7 +1186,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure _actionContext, _filters, resultExecutingContext.Result, - _instance) + _instance!) { Canceled = true, }; @@ -1232,7 +1232,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure resultExecutingContext, _filters, resultExecutingContext.Result, - _instance) + _instance!) { Canceled = true, }; @@ -1309,13 +1309,13 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure { if (_resultExecutedContext == null) { - _resultExecutedContext = new ResultExecutedContextSealed(_actionContext, _filters, result, _instance); + _resultExecutedContext = new ResultExecutedContextSealed(_actionContext, _filters, result!, _instance!); } return Task.CompletedTask; } - Rethrow(_resultExecutedContext); + Rethrow(_resultExecutedContext!); return Task.CompletedTask; } @@ -1331,7 +1331,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure try { var next = State.ResultNext; - var state = (object)null; + var state = (object?)null; var scope = Scope.Result; var isCompleted = false; while (!isCompleted) @@ -1345,7 +1345,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure } catch (Exception exception) { - _resultExecutedContext = new ResultExecutedContextSealed(_actionContext, _filters, _result, _instance) + _resultExecutedContext = new ResultExecutedContextSealed(_actionContext, _filters, _result!, _instance!) { ExceptionDispatchInfo = ExceptionDispatchInfo.Capture(exception), }; @@ -1355,7 +1355,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure return Task.CompletedTask; - static async Task Awaited(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) + static async Task Awaited(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object? state, bool isCompleted) { try { @@ -1368,7 +1368,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure } catch (Exception exception) { - invoker._resultExecutedContext = new ResultExecutedContextSealed(invoker._actionContext, invoker._filters, invoker._result, invoker._instance) + invoker._resultExecutedContext = new ResultExecutedContextSealed(invoker._actionContext, invoker._filters, invoker._result!, invoker._instance!) { ExceptionDispatchInfo = ExceptionDispatchInfo.Capture(exception), }; @@ -1589,4 +1589,5 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure public AuthorizationFilterContextSealed(ActionContext actionContext, IList filters) : base(actionContext, filters) { } } } +#nullable restore } diff --git a/src/Mvc/Mvc.Core/src/MvcCoreLoggerExtensions.cs b/src/Mvc/Mvc.Core/src/MvcCoreLoggerExtensions.cs index 165d3ab90c..26534c1712 100644 --- a/src/Mvc/Mvc.Core/src/MvcCoreLoggerExtensions.cs +++ b/src/Mvc/Mvc.Core/src/MvcCoreLoggerExtensions.cs @@ -695,10 +695,12 @@ namespace Microsoft.AspNetCore.Mvc _selectingFirstCanWriteFormatter(logger, null); } - public static IDisposable ActionScope(this ILogger logger, ActionDescriptor action) +#nullable enable + public static IDisposable? ActionScope(this ILogger logger, ActionDescriptor action) { return logger.BeginScope(new ActionLogScope(action)); } +#nullable restore public static void ExecutingAction(this ILogger logger, ActionDescriptor action) { diff --git a/src/Mvc/Mvc.ViewFeatures/src/MvcViewFeaturesLoggerExtensions.cs b/src/Mvc/Mvc.ViewFeatures/src/MvcViewFeaturesLoggerExtensions.cs index a7e9a492fd..635c3e315d 100644 --- a/src/Mvc/Mvc.ViewFeatures/src/MvcViewFeaturesLoggerExtensions.cs +++ b/src/Mvc/Mvc.ViewFeatures/src/MvcViewFeaturesLoggerExtensions.cs @@ -122,10 +122,12 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures "Skipping the execution of current filter as its not the most effective filter implementing the policy {FilterPolicy}."); } - public static IDisposable ViewComponentScope(this ILogger logger, ViewComponentContext context) +#nullable enable + public static IDisposable? ViewComponentScope(this ILogger logger, ViewComponentContext context) { return logger.BeginScope(new ViewComponentLogScope(context.ViewComponentDescriptor)); } +#nullable restore public static void ViewComponentExecuting( this ILogger logger, diff --git a/src/Mvc/Mvc.ViewFeatures/src/ViewComponents/DefaultViewComponentInvoker.cs b/src/Mvc/Mvc.ViewFeatures/src/ViewComponents/DefaultViewComponentInvoker.cs index a7f6fa6617..3fced5a0c9 100644 --- a/src/Mvc/Mvc.ViewFeatures/src/ViewComponents/DefaultViewComponentInvoker.cs +++ b/src/Mvc/Mvc.ViewFeatures/src/ViewComponents/DefaultViewComponentInvoker.cs @@ -93,6 +93,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewComponents await result.ExecuteAsync(context); } +#nullable enable private async Task InvokeAsyncCore(ObjectMethodExecutor executor, ViewComponentContext context) { var component = _viewComponentFactory.CreateViewComponent(context); @@ -168,6 +169,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewComponents return viewComponentResult; } } +#nullable restore private static IViewComponentResult CoerceToViewComponentResult(object value) { diff --git a/src/Mvc/mvc.slnf b/src/Mvc/mvc.slnf deleted file mode 100644 index 79d4fb7df0..0000000000 --- a/src/Mvc/mvc.slnf +++ /dev/null @@ -1,73 +0,0 @@ -{ - "solution": { - "path": "Mvc.sln", - "projects": [ - "test\\WebSites\\BasicWebSite\\BasicWebSite.csproj", - "test\\WebSites\\RoutingWebSite\\Mvc.RoutingWebSite.csproj", - "test\\WebSites\\RazorWebSite\\RazorWebSite.csproj", - "test\\WebSites\\FormatterWebSite\\FormatterWebSite.csproj", - "test\\WebSites\\ApiExplorerWebSite\\ApiExplorerWebSite.csproj", - "test\\WebSites\\VersioningWebSite\\VersioningWebSite.csproj", - "test\\WebSites\\TagHelpersWebSite\\TagHelpersWebSite.csproj", - "test\\WebSites\\FilesWebSite\\FilesWebSite.csproj", - "test\\WebSites\\ApplicationModelWebSite\\ApplicationModelWebSite.csproj", - "test\\WebSites\\HtmlGenerationWebSite\\HtmlGenerationWebSite.csproj", - "test\\WebSites\\ErrorPageMiddlewareWebSite\\ErrorPageMiddlewareWebSite.csproj", - "test\\WebSites\\XmlFormattersWebSite\\XmlFormattersWebSite.csproj", - "test\\WebSites\\ControllersFromServicesWebSite\\ControllersFromServicesWebSite.csproj", - "test\\WebSites\\ControllersFromServicesClassLibrary\\ControllersFromServicesClassLibrary.csproj", - "test\\WebSites\\CorsWebSite\\CorsWebSite.csproj", - "samples\\MvcSandbox\\MvcSandbox.csproj", - "test\\WebSites\\SimpleWebSite\\SimpleWebSite.csproj", - "test\\WebSites\\SecurityWebSite\\SecurityWebSite.csproj", - "test\\WebSites\\RazorPagesWebSite\\RazorPagesWebSite.csproj", - "benchmarks\\Microsoft.AspNetCore.Mvc.Performance\\Microsoft.AspNetCore.Mvc.Performance.csproj", - "test\\WebSites\\RazorBuildWebSite\\RazorBuildWebSite.csproj", - "test\\WebSites\\RazorBuildWebSite.Views\\RazorBuildWebSite.Views.csproj", - "Mvc.Analyzers\\src\\Microsoft.AspNetCore.Mvc.Analyzers.csproj", - "Mvc.Analyzers\\test\\Mvc.Analyzers.Test.csproj", - "test\\WebSites\\RazorPagesClassLibrary\\RazorPagesClassLibrary.csproj", - "shared\\Mvc.Views.TestCommon\\Microsoft.AspNetCore.Mvc.Views.TestCommon.csproj", - "Mvc.Api.Analyzers\\test\\Mvc.Api.Analyzers.Test.csproj", - "Mvc.Api.Analyzers\\src\\Microsoft.AspNetCore.Mvc.Api.Analyzers.csproj", - "test\\WebSites\\GenericHostWebSite\\GenericHostWebSite.csproj", - "Mvc\\src\\Microsoft.AspNetCore.Mvc.csproj", - "Mvc\\test\\Microsoft.AspNetCore.Mvc.Test.csproj", - "Mvc.Abstractions\\src\\Microsoft.AspNetCore.Mvc.Abstractions.csproj", - "Mvc.Abstractions\\test\\Microsoft.AspNetCore.Mvc.Abstractions.Test.csproj", - "Mvc.ApiExplorer\\src\\Microsoft.AspNetCore.Mvc.ApiExplorer.csproj", - "Mvc.ApiExplorer\\test\\Microsoft.AspNetCore.Mvc.ApiExplorer.Test.csproj", - "Mvc.Core\\src\\Microsoft.AspNetCore.Mvc.Core.csproj", - "Mvc.Core\\test\\Microsoft.AspNetCore.Mvc.Core.Test.csproj", - "Mvc.Cors\\src\\Microsoft.AspNetCore.Mvc.Cors.csproj", - "Mvc.Cors\\test\\Microsoft.AspNetCore.Mvc.Cors.Test.csproj", - "Mvc.DataAnnotations\\src\\Microsoft.AspNetCore.Mvc.DataAnnotations.csproj", - "Mvc.DataAnnotations\\test\\Microsoft.AspNetCore.Mvc.DataAnnotations.Test.csproj", - "Mvc.Formatters.Json\\src\\Microsoft.AspNetCore.Mvc.Formatters.Json.csproj", - "Mvc.Formatters.Xml\\src\\Microsoft.AspNetCore.Mvc.Formatters.Xml.csproj", - "Mvc.Formatters.Xml\\test\\Microsoft.AspNetCore.Mvc.Formatters.Xml.Test.csproj", - "Mvc.Localization\\src\\Microsoft.AspNetCore.Mvc.Localization.csproj", - "Mvc.Localization\\test\\Microsoft.AspNetCore.Mvc.Localization.Test.csproj", - "Mvc.Razor\\src\\Microsoft.AspNetCore.Mvc.Razor.csproj", - "Mvc.Razor\\test\\Microsoft.AspNetCore.Mvc.Razor.Test.csproj", - "Mvc.RazorPages\\src\\Microsoft.AspNetCore.Mvc.RazorPages.csproj", - "Mvc.RazorPages\\test\\Microsoft.AspNetCore.Mvc.RazorPages.Test.csproj", - "Mvc.TagHelpers\\src\\Microsoft.AspNetCore.Mvc.TagHelpers.csproj", - "Mvc.TagHelpers\\test\\Microsoft.AspNetCore.Mvc.TagHelpers.Test.csproj", - "Mvc.ViewFeatures\\src\\Microsoft.AspNetCore.Mvc.ViewFeatures.csproj", - "Mvc.ViewFeatures\\test\\Microsoft.AspNetCore.Mvc.ViewFeatures.Test.csproj", - "test\\Mvc.FunctionalTests\\Microsoft.AspNetCore.Mvc.FunctionalTests.csproj", - "test\\Mvc.IntegrationTests\\Microsoft.AspNetCore.Mvc.IntegrationTests.csproj", - "shared\\Mvc.TestDiagnosticListener\\Microsoft.AspNetCore.Mvc.TestDiagnosticListener.csproj", - "Mvc.Testing\\src\\Microsoft.AspNetCore.Mvc.Testing.csproj", - "shared\\Mvc.Core.TestCommon\\Microsoft.AspNetCore.Mvc.Core.TestCommon.csproj", - "Mvc.NewtonsoftJson\\src\\Microsoft.AspNetCore.Mvc.NewtonsoftJson.csproj", - "Mvc.NewtonsoftJson\\test\\Microsoft.AspNetCore.Mvc.NewtonsoftJson.Test.csproj", - "Mvc.Razor.RuntimeCompilation\\src\\Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.csproj", - "Mvc.Razor.RuntimeCompilation\\test\\Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.Test.csproj", - "test\\WebSites\\RazorBuildWebSite.PrecompiledViews\\RazorBuildWebSite.PrecompiledViews.csproj", - "Mvc.Components.Prerendering\\src\\Microsoft.AspNetCore.Mvc.Components.Prerendering.csproj", - "Mvc.Components.Prerendering\\test\\Microsoft.AspNetCore.Mvc.Components.Prerendering.Test.csproj" - ] - } -} \ No newline at end of file diff --git a/src/Mvc/samples/MvcSandbox/Startup.cs b/src/Mvc/samples/MvcSandbox/Startup.cs index c1bc81b7bc..26848d96d7 100644 --- a/src/Mvc/samples/MvcSandbox/Startup.cs +++ b/src/Mvc/samples/MvcSandbox/Startup.cs @@ -109,7 +109,6 @@ namespace MvcSandbox factory .AddConsole() .AddDebug(); - factory.SetMinimumLevel(LogLevel.Trace); }) .UseIISIntegration() .UseKestrel()