-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Add FromFile to BinaryData #107231
base: main
Are you sure you want to change the base?
Add FromFile to BinaryData #107231
Conversation
string path = Path.GetTempFileName(); | ||
File.WriteAllBytes(path, buffer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should temp files be cleaned up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed!
Also fixed for the existing test that I took as reference.
private static async Task<BinaryData> FromFileAsync(string path, bool async, | ||
string? mediaType = default, CancellationToken cancellationToken = default) | ||
{ | ||
using FileStream fileStream = File.OpenRead(path); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to use File.ReadAllBytes
? Since it has that tries to minimize allocations of the byte array if the file-size is determinable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question!
-
The file-size optimization is also supported in the
FromStream
flow. -
The
ReadAllBytesAsync
method is not available for the net462 and netstandard2.0 TFMs. Can we then fallback to the synchronous implementation for these TFMs? -
The ReadAllBytes{Async} flow may be more efficient to avoid the intermediate copy buffer of the CopyTo{Async} flow.
Here would be the corresponding code:
private static
#if NET6_0_OR_GREATER
async
#endif
Task<BinaryData> FromFileAsync(string path, bool async, string? mediaType = default, CancellationToken cancellationToken = default)
{
#if NET6_0_OR_GREATER
byte[] bytes;
if (async)
{
bytes = await File.ReadAllBytesAsync(path, cancellationToken).ConfigureAwait(false);
}
else
{
bytes = File.ReadAllBytes(path);
}
return new BinaryData(bytes, mediaType);
#else
byte[] bytes = File.ReadAllBytes(path);
return Task.FromResult(new BinaryData(bytes, mediaType));
#endif
}
Better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see. Thanks for looking into it. Personally I don't think the potential benefit outweighs the added complexity, I would keep it as it is then.
private static async Task<BinaryData> FromFileAsync(string path, bool async, | ||
string? mediaType = default, CancellationToken cancellationToken = default) | ||
{ | ||
using FileStream fileStream = File.OpenRead(path); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The FileStream should be opened for asynchronous use when async: true. The way this is written, it'll always use synchronous I/O, which means async operations on it will end up being sync calls queued to the pool.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced with the FileStream
constructor enabling async calls.
Closes #106203