Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 2dcc293

Browse files
committed
Design extensibility for executors
We have all of these executors but they aren't really documented/supported for extensibility today. This change introduces a pattern for action result executors so we can make them extensible.
1 parent 95c1005 commit 2dcc293

File tree

57 files changed

+244
-95
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+244
-95
lines changed

src/Microsoft.AspNetCore.Mvc.Core/ContentResult.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Mvc.Infrastructure;
67
using Microsoft.AspNetCore.Mvc.Internal;
78
using Microsoft.Extensions.DependencyInjection;
89

@@ -32,7 +33,7 @@ public override Task ExecuteResultAsync(ActionContext context)
3233
throw new ArgumentNullException(nameof(context));
3334
}
3435

35-
var executor = context.HttpContext.RequestServices.GetRequiredService<ContentResultExecutor>();
36+
var executor = context.HttpContext.RequestServices.GetRequiredService<IActionResultExecutor<ContentResult>>();
3637
return executor.ExecuteAsync(context, this);
3738
}
3839
}

src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreServiceCollectionExtensions.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -238,17 +238,17 @@ internal static void AddMvcCoreServices(IServiceCollection services)
238238
services.TryAddSingleton<IHttpResponseStreamWriterFactory, MemoryPoolHttpResponseStreamWriterFactory>();
239239
services.TryAddSingleton(ArrayPool<byte>.Shared);
240240
services.TryAddSingleton(ArrayPool<char>.Shared);
241-
services.TryAddSingleton<ObjectResultExecutor>();
242-
services.TryAddSingleton<PhysicalFileResultExecutor>();
243-
services.TryAddSingleton<VirtualFileResultExecutor>();
244-
services.TryAddSingleton<FileStreamResultExecutor>();
245-
services.TryAddSingleton<FileContentResultExecutor>();
246-
services.TryAddSingleton<RedirectResultExecutor>();
247-
services.TryAddSingleton<LocalRedirectResultExecutor>();
248-
services.TryAddSingleton<RedirectToActionResultExecutor>();
249-
services.TryAddSingleton<RedirectToRouteResultExecutor>();
250-
services.TryAddSingleton<RedirectToPageResultExecutor>();
251-
services.TryAddSingleton<ContentResultExecutor>();
241+
services.TryAddSingleton<IActionResultExecutor<ObjectResult>, ObjectResultExecutor>();
242+
services.TryAddSingleton<IActionResultExecutor<PhysicalFileResult>, PhysicalFileResultExecutor>();
243+
services.TryAddSingleton<IActionResultExecutor<VirtualFileResult>, VirtualFileResultExecutor>();
244+
services.TryAddSingleton<IActionResultExecutor<FileStreamResult>, FileStreamResultExecutor>();
245+
services.TryAddSingleton<IActionResultExecutor<FileContentResult>, FileContentResultExecutor>();
246+
services.TryAddSingleton<IActionResultExecutor<RedirectResult>, RedirectResultExecutor>();
247+
services.TryAddSingleton<IActionResultExecutor<LocalRedirectResult>, LocalRedirectResultExecutor>();
248+
services.TryAddSingleton<IActionResultExecutor<RedirectToActionResult>, RedirectToActionResultExecutor>();
249+
services.TryAddSingleton<IActionResultExecutor<RedirectToRouteResult>, RedirectToRouteResultExecutor>();
250+
services.TryAddSingleton<IActionResultExecutor<RedirectToPageResult>, RedirectToPageResultExecutor>();
251+
services.TryAddSingleton<IActionResultExecutor<ContentResult>, ContentResultExecutor>();
252252

253253
//
254254
// Route Handlers

src/Microsoft.AspNetCore.Mvc.Core/FileContentResult.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Mvc.Infrastructure;
67
using Microsoft.AspNetCore.Mvc.Internal;
78
using Microsoft.Extensions.DependencyInjection;
89
using Microsoft.Net.Http.Headers;

src/Microsoft.AspNetCore.Mvc.Core/FileStreamResult.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.IO;
66
using System.Threading.Tasks;
7+
using Microsoft.AspNetCore.Mvc.Infrastructure;
78
using Microsoft.AspNetCore.Mvc.Internal;
89
using Microsoft.Extensions.DependencyInjection;
910
using Microsoft.Net.Http.Headers;

src/Microsoft.AspNetCore.Mvc.Core/Internal/ContentResultExecutor.cs renamed to src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/ContentResultExecutor.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
using System;
55
using System.Text;
66
using System.Threading.Tasks;
7+
using Microsoft.AspNetCore.Mvc.Internal;
78
using Microsoft.Extensions.Logging;
89

9-
namespace Microsoft.AspNetCore.Mvc.Internal
10+
namespace Microsoft.AspNetCore.Mvc.Infrastructure
1011
{
11-
public class ContentResultExecutor
12+
public class ContentResultExecutor : IActionResultExecutor<ContentResult>
1213
{
1314
private const string DefaultContentType = "text/plain; charset=utf-8";
1415
private readonly ILogger<ContentResultExecutor> _logger;
@@ -20,6 +21,7 @@ public ContentResultExecutor(ILogger<ContentResultExecutor> logger, IHttpRespons
2021
_httpResponseStreamWriterFactory = httpResponseStreamWriterFactory;
2122
}
2223

24+
/// <inheritdoc />
2325
public virtual async Task ExecuteAsync(ActionContext context, ContentResult result)
2426
{
2527
if (context == null)

src/Microsoft.AspNetCore.Mvc.Core/Internal/FileContentResultExecutor.cs renamed to src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/FileContentResultExecutor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
using Microsoft.Extensions.Logging;
88
using Microsoft.Net.Http.Headers;
99

10-
namespace Microsoft.AspNetCore.Mvc.Internal
10+
namespace Microsoft.AspNetCore.Mvc.Infrastructure
1111
{
12-
public class FileContentResultExecutor : FileResultExecutorBase
12+
public class FileContentResultExecutor : FileResultExecutorBase, IActionResultExecutor<FileContentResult>
1313
{
1414
public FileContentResultExecutor(ILoggerFactory loggerFactory)
1515
: base(CreateLogger<FileContentResultExecutor>(loggerFactory))
1616
{
1717
}
1818

19+
/// <inheritdoc />
1920
public virtual Task ExecuteAsync(ActionContext context, FileContentResult result)
2021
{
2122
if (context == null)

src/Microsoft.AspNetCore.Mvc.Core/Internal/FileResultExecutorBase.cs renamed to src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/FileResultExecutorBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
using Microsoft.AspNetCore.Http.Extensions;
1111
using Microsoft.AspNetCore.Http.Headers;
1212
using Microsoft.AspNetCore.Internal;
13+
using Microsoft.AspNetCore.Mvc.Internal;
1314
using Microsoft.Extensions.Logging;
1415
using Microsoft.Net.Http.Headers;
1516

16-
namespace Microsoft.AspNetCore.Mvc.Internal
17+
namespace Microsoft.AspNetCore.Mvc.Infrastructure
1718
{
1819
public class FileResultExecutorBase
1920
{

src/Microsoft.AspNetCore.Mvc.Core/Internal/FileStreamResultExecutor.cs renamed to src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/FileStreamResultExecutor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
using Microsoft.Extensions.Logging;
77
using Microsoft.Net.Http.Headers;
88

9-
namespace Microsoft.AspNetCore.Mvc.Internal
9+
namespace Microsoft.AspNetCore.Mvc.Infrastructure
1010
{
11-
public class FileStreamResultExecutor : FileResultExecutorBase
11+
public class FileStreamResultExecutor : FileResultExecutorBase, IActionResultExecutor<FileStreamResult>
1212
{
1313
public FileStreamResultExecutor(ILoggerFactory loggerFactory)
1414
: base(CreateLogger<FileStreamResultExecutor>(loggerFactory))
1515
{
1616
}
1717

18+
/// <inheritdoc />
1819
public virtual Task ExecuteAsync(ActionContext context, FileStreamResult result)
1920
{
2021
if (context == null)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Threading.Tasks;
5+
6+
namespace Microsoft.AspNetCore.Mvc.Infrastructure
7+
{
8+
public interface IActionResultExecutor<TResult> where TResult : IActionResult
9+
{
10+
Task ExecuteAsync(ActionContext context, TResult result);
11+
}
12+
}

src/Microsoft.AspNetCore.Mvc.Core/Internal/LocalRedirectResultExecutor.cs renamed to src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/LocalRedirectResultExecutor.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Threading.Tasks;
56
using Microsoft.AspNetCore.Http;
67
using Microsoft.AspNetCore.Mvc.Core;
8+
using Microsoft.AspNetCore.Mvc.Internal;
79
using Microsoft.AspNetCore.Mvc.Routing;
810
using Microsoft.Extensions.Logging;
911
using Microsoft.Net.Http.Headers;
1012

11-
namespace Microsoft.AspNetCore.Mvc.Internal
13+
namespace Microsoft.AspNetCore.Mvc.Infrastructure
1214
{
13-
public class LocalRedirectResultExecutor
15+
public class LocalRedirectResultExecutor : IActionResultExecutor<LocalRedirectResult>
1416
{
1517
private readonly ILogger _logger;
1618
private readonly IUrlHelperFactory _urlHelperFactory;
@@ -31,7 +33,8 @@ public LocalRedirectResultExecutor(ILoggerFactory loggerFactory, IUrlHelperFacto
3133
_urlHelperFactory = urlHelperFactory;
3234
}
3335

34-
public virtual void Execute(ActionContext context, LocalRedirectResult result)
36+
/// <inheritdoc />
37+
public virtual Task ExecuteAsync(ActionContext context, LocalRedirectResult result)
3538
{
3639
if (context == null)
3740
{
@@ -64,6 +67,8 @@ public virtual void Execute(ActionContext context, LocalRedirectResult result)
6467
{
6568
context.HttpContext.Response.Redirect(destinationUrl, result.Permanent);
6669
}
70+
71+
return Task.CompletedTask;
6772
}
6873
}
6974
}

0 commit comments

Comments
 (0)