Skip to content

Adding IResult implementation for FileStreamResult and LocalRedirectResult #32956

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

Conversation

uabarahona
Copy link
Contributor

Summary

These changes add IResult implementation on ActionResults that does not have it

Details

The ActionResults that are going to be worked on this PR are:

  • FileStreamResult
  • LocalRedirectResult

Task that needs to be done for this PR are:

  • Basic implementation for IResults
  • Add test suite to test the new IResults

Addresses part of #32565

@ghost ghost added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates community-contribution Indicates that the PR has been added by a community member labels May 24, 2021
@uabarahona uabarahona changed the title Adding implementation for FileStreamResult and LocalRedirectResult Adding IResult implementation for FileStreamResult and LocalRedirectResult May 24, 2021
throw new ArgumentNullException(nameof(httpContext));
}

using (FileStream)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, it is possible to share this implementation with the one in FileStreamExecutor (using an internal static method)? We could make the WriteFileAsync an optional parameter to the function to help with the overload.

static ExecuteAsync(HttpContext context, ILogger logger, Func<..., Task>)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same q with the redirect result.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea!, I will make the change on night :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pranavkm I take me more than I thought to get back into this, I have applied your suggestions, would you take a look?

async Task IResult.ExecuteAsync(HttpContext httpContext)
{
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger<RedirectResult>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is RedirectResult the relevant type parameter for this logger?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it should be FileStreamResult, thanks! already fixed

@uabarahona uabarahona force-pushed the barahonajm/adding_IResult_Implementations branch from d49cb15 to 1bf722e Compare May 31, 2021 17:33
@uabarahona
Copy link
Contributor Author

@pranavkm @nil4 I have fixed the logger types on all my other implementations, also if they way I implemented the approach suggested by @pranavkm is correct, I will migrate the other implementations either on this ticket or in a new PR later in this week.

Thanks!

Copy link
Contributor

@pranavkm pranavkm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay in getting back

internal static async Task ExecuteAsyncInternal<TContext>(
TContext context,
FileStreamResult result,
Func<TContext, FileStreamResult, long?, bool, DateTimeOffset?, EntityTagHeaderValue?, (RangeItemHeaderValue?, long, bool)> SetHeadersAndLog,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Func<TContext, FileStreamResult, long?, bool, DateTimeOffset?, EntityTagHeaderValue?, (RangeItemHeaderValue?, long, bool)> SetHeadersAndLog,
Func<TContext, FileStreamResult, long?, bool, DateTimeOffset?, EntityTagHeaderValue?, (RangeItemHeaderValue?, long, bool)> setHeadersAndLog,

TContext context,
FileStreamResult result,
Func<TContext, FileStreamResult, long?, bool, DateTimeOffset?, EntityTagHeaderValue?, (RangeItemHeaderValue?, long, bool)> SetHeadersAndLog,
Func<TContext, FileStreamResult, RangeItemHeaderValue?, long, Task> WriteFileAsync,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Func<TContext, FileStreamResult, RangeItemHeaderValue?, long, Task> WriteFileAsync,
Func<TContext, FileStreamResult, RangeItemHeaderValue?, long, Task> writeFileAsync,

Task writeFileAsync(HttpContext httpContext, FileStreamResult result, RangeItemHeaderValue? range, long rangeLength)
=> FileStreamResultExecutor.WriteFileAsyncInternal(httpContext, this, range, rangeLength, logger!);

(RangeItemHeaderValue? range, long rangeLength, bool serveBody) setHeadersAndLog(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these be

Suggested change
(RangeItemHeaderValue? range, long rangeLength, bool serveBody) setHeadersAndLog(
static (RangeItemHeaderValue? range, long rangeLength, bool serveBody) setHeadersAndLog(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(RangeItemHeaderValue? range, long rangeLength, bool serveBody) setHeadersAndLog(
(RangeItemHeaderValue? range, long rangeLength, bool serveBody) SetHeadersAndLog(

var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger<FileStreamResult>();

Task writeFileAsync(HttpContext httpContext, FileStreamResult result, RangeItemHeaderValue? range, long rangeLength)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Task writeFileAsync(HttpContext httpContext, FileStreamResult result, RangeItemHeaderValue? range, long rangeLength)
Task WriteFileAsync(HttpContext httpContext, FileStreamResult result, RangeItemHeaderValue? range, long rangeLength)

Comment on lines +112 to +113
setHeadersAndLog,
writeFileAsync,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
setHeadersAndLog,
writeFileAsync,
SetHeadersAndLog,
WriteFileAsync,

Comment on lines +62 to +63
ILogger logger
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ILogger logger
)
ILogger logger)

@@ -45,36 +44,52 @@ public LocalRedirectResultExecutor(ILoggerFactory loggerFactory, IUrlHelperFacto
/// <inheritdoc />
public virtual Task ExecuteAsync(ActionContext context, LocalRedirectResult result)
{
if (context == null)
var urlHelper = result.UrlHelper ?? _urlHelperFactory.GetUrlHelper(context);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var urlHelper = result.UrlHelper ?? _urlHelperFactory.GetUrlHelper(context);
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var urlHelper = result.UrlHelper ?? _urlHelperFactory.GetUrlHelper(context);

return ExecuteAsyncInternal(
context.HttpContext,
result,
urlHelper.IsLocalUrl,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel a little iffy about this. This is now going to allocate a delegate per invocation. Could we have it fall-back in the implementation instead e.g.

internal static Task ExecuteAsyncInternal(
            HttpContext httpContext,
            LocalRedirectResult result,
            IUrlHelper? urlHelper)
{
  ...
   var isLocalUrl = urlHelper?.IsLocalUrl(url) ?? UrlHelperBase.CheckIsLocalUrl(url);
}

@pranavkm
Copy link
Contributor

pranavkm commented Jul 1, 2021

Thanks for your effort @barahonajm. We ended up deciding that it was best to avoid referencing MVC types from minimal actions especially for a common scenario such as the result types. We ended up duplicating the results and as such we no longer want to take this PR. Once again, thanks for your all your time!

@pranavkm pranavkm closed this Jul 1, 2021
@uabarahona
Copy link
Contributor Author

@pranavkm that sound like a right decision, thanks for your guidance during this time. See you in another PR :)

@ghost
Copy link

ghost commented Jul 2, 2021

Hi @barahonajm. It looks like you just commented on a closed PR. The team will most probably miss it. If you'd like to bring something important up to their attention, consider filing a new issue and add enough details to build context.

@uabarahona uabarahona deleted the barahonajm/adding_IResult_Implementations branch July 2, 2021 01:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants