Skip to content

IFileInfo async methods #37590

Description

@synercoder

Background and Motivation

I am currently working on a hobby (web) project, and for testing I have a bunch of images I server stashed in a folder. The images are served using:

app.UseStaticFiles(new StaticFileOptions()
{
    RequestPath = "/images",
    FileProvider = new PhysicalFileProvider(@"C:\path\to\my\images\folder")
});

This works fine of course, but I would like to have the images served from the cloud if I ever put this project on the web.

To this end, I wanted to see if I could replace the PhysicalFileProvider with a custom creation of my own, something in the line of: AzureBlobFileProvider. So I dived into IFileProvider and saw that it only had synchronous methods.

Proposed API

namespace Microsoft.Extensions.FileProviders
{
    public interface IFileProvider
    {
-       IDirectoryContents GetDirectoryContents(string subpath);
+       Task<IDirectoryContents> GetDirectoryContentsAsync(string subpath);
+       Task<IDirectoryContents> GetDirectoryContentsAsync(string subpath, System.Threading.CancellationToken cancellationToken);
-       IFileInfo GetFileInfo(string subpath);
+       Task<IFileInfo> GetFileInfoAsync(string subpath);
+       Task<IFileInfo> GetFileInfoAsync(string subpath, System.Threading.CancellationToken cancellationToken);
    }

    public interface IFileInfo
    {
-       Stream CreateReadStream();
+       Task<Stream> CreateReadStreamAsync();
+       Task<Stream> CreateReadStreamAsync(System.Threading.CancellationToken cancellationToken);
    }
}

And perhaps?

namespace Microsoft.Extensions.FileProviders
{
-    public interface IDirectoryContents : IEnumerable<IFileInfo>, IEnumerable
+    public interface IDirectoryContents : IAsyncEnumerable<IFileInfo>
}

Usage Examples

The code in the UseStaticFiles extension should be needed to adapted to use the new API surface.

Alternative design

For my idea

As stated in the background and motivation, I would like to have the option to access other "filesystems", some filesystems like Azure Blob Storage resides on the web. Therefor it is best practice to use async methods. Unfortunately this is in the currently API-surface not possible.

The Azure library however offers some sync methods, but I would like to avoid those if possible.

For the API-proposal

Instead of removing the currently sync methods, the async methods could be an addition to the current interfaces. This would make this a non-breaking change, except for IDirectoryContents since this would mean adding another interface & thus changing the signature.

Risks

The risks with this API-proposal is like I mentioned before that this would entail a breaking change. How much of a breaking change depends of course on whether the sync methods will be replaced or if they stay and async will be added.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.IO

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions