Table of Contents

IFileSystem and IFileProvider

Two adapter packages let code written against .NET's file abstractions use files on an SSH server, unchanged, over SFTP. Nothing is mounted: only your application sees the files.

Package Type Interface
SshSharp.IO.Abstractions SftpFileSystem System.IO.Abstractions' IFileSystem: File, Directory, Path, FileInfo, streams. Read and write.
SshSharp.FileProviders SftpFileProvider Microsoft.Extensions.FileProviders.IFileProvider: ASP.NET Core static files, Razor views, configuration. Read-only.

When to use

  • IFileSystem: you already have (or want) code that takes an IFileSystem, so it can run against the local disk, an in-memory file system in tests, or a server.
  • IFileProvider: a framework component reads files through IFileProvider and you want it to read them from a server.
  • For transfers and full control, use SftpClient directly: the adapters add convenience, not speed.

Code against IFileSystem

dotnet add package SshSharp.IO.Abstractions
/// <summary>Code written against IFileSystem: it does not know, or care, where the files are.</summary>
public sealed class ReportWriter(IFileSystem fileSystem)
{
    public async Task WriteAsync(string directory, string name, string text, CancellationToken cancellationToken)
    {
        fileSystem.Directory.CreateDirectory(directory);
        var path = fileSystem.Path.Combine(directory, name);
        await fileSystem.File.WriteAllTextAsync(path, text, cancellationToken);
    }

    public IEnumerable<string> List(string directory) =>
        fileSystem.Directory.EnumerateFiles(directory, "*.txt", SearchOption.AllDirectories);
}
await using var sftp = await client.OpenSftpAsync(cancellationToken);
IFileSystem remote = new SftpFileSystem(sftp, new SftpFileSystemOptions { OperationTimeout = TimeSpan.FromSeconds(30) });

var writer = new ReportWriter(remote);                    // the same class works with new FileSystem() for local files
await writer.WriteAsync("reports/2026", "summary.txt", "All green.", cancellationToken);
foreach (var file in writer.List("reports"))
{
    Console.WriteLine(file);                               // POSIX paths: reports/2026/summary.txt
}

Streams from the adapter are ordinary streams, so local and remote combine:

// A local file into a remote one, stream to stream.
await using var sftp = await client.OpenSftpAsync(cancellationToken);
IFileSystem remote = new SftpFileSystem(sftp);

await using var source = File.OpenRead("build/app.zip");
await using var target = remote.File.Create("/srv/drop/app.zip");
await source.CopyToAsync(target, cancellationToken);

Serve files with IFileProvider

dotnet add package SshSharp.FileProviders
// Static files served from /srv/www on the server, over the shared connection of the "content" endpoint
// (builder.Services.AddSshClient("content")). The provider opens an SFTP session on the first request, and a new one after the
// connection drops (the shared connection is opened again too). It owns those sessions: dispose it when the application stops.
var connections = app.Services.GetRequiredService<ISshConnectionProvider>();
var provider = new SftpFileProvider(ct => connections.OpenSftpAsync("content", ct), "/srv/www",
    new SftpFileProviderOptions { CacheDuration = TimeSpan.FromSeconds(30) });

app.UseStaticFiles(new StaticFileOptions { FileProvider = provider, RequestPath = "/content" });
app.Lifetime.ApplicationStopped.Register(provider.Dispose);

The provider above gets its sessions from a factory, so it keeps serving after the connection drops (see Follow a connection that drops). It can also use one session you give it, for as long as that session lives:

// One SFTP session for the application's lifetime, on a connection of its own. Once that connection drops, every lookup fails:
// prefer a session factory (above) for anything long-lived.
var ssh = await SshClient.ConnectAsync(app.Configuration["Ssh:Alias"] ?? "web-content");
var sftp = await ssh.OpenSftpAsync();
var provider = new SftpFileProvider(sftp, "/srv/www");

app.UseStaticFiles(new StaticFileOptions { FileProvider = provider, RequestPath = "/content" });
app.Lifetime.ApplicationStopped.Register(() =>
{
    sftp.DisposeAsync().AsTask().GetAwaiter().GetResult();   // the provider does not own a session it was given
    ssh.DisposeAsync().AsTask().GetAwaiter().GetResult();
});

It also feeds configuration:

// Read a JSON configuration file that lives on the server (read once: SFTP has no change notifications).
var provider = new SftpFileProvider(sftp, "/etc/myapp");
configuration.AddJsonFile(provider, "appsettings.json", optional: false, reloadOnChange: false);

Follow a connection that drops

Both adapters take either one SftpClient or a session factory, Func<CancellationToken, Task<SftpClient>>. With one SftpClient, the adapter uses it for its lifetime: when its connection drops, every later call fails until you build a new adapter, which UseStaticFiles and a container's singletons have no way to do. With a factory, the adapter follows the connection:

  • It opens a session on first use and shares it between all its callers.
  • When the session or the connection under it has ended, it disposes that session and opens a new one. That is decided on the session's state (SftpClient.IsConnected), not on the error: a drop, a connection a keep-alive declared dead (which fails with Timeout), a failed key exchange all count. One request's failure does not, and the session stays for the other callers: an SFTP error (a missing file, a denied request), a reply that timed out, a listing or reply too large to take. A session found ended before a call starts (the connection dropped while the adapter was idle) is replaced before the call uses it.
  • Callers that find no session share one call to the factory, so a drop is followed by one reconnect, not one per request.
  • A call that only reads (a lookup, a listing, a whole-file read, opening a file to read) and meets the dropped connection is tried once more on the new session. A call that writes is not, because the server may have done it before the connection dropped: it fails, and the next call uses the new session.
sequenceDiagram
    participant App as Callers
    participant A as SftpFileProvider / SftpFileSystem
    participant F as Session factory
    participant S as SFTP server
    App->>A: GetFileInfo / ReadAllText
    A->>F: first use: open a session
    F-->>A: session 1
    A->>S: request on session 1
    Note over A,S: the connection drops
    App->>A: next call
    A->>S: request on session 1
    S--xA: connection closed
    A->>A: dispose session 1
    A->>F: open a new session (one, for every waiting caller)
    F-->>A: session 2
    A->>S: a read is tried again on session 2
    A-->>App: the answer

With dependency injection, ISshConnectionProvider.OpenSftpAsync(name) is the factory: it opens a session on the endpoint's shared connection, which the provider itself opens again after a drop (the static files example above uses it). Without it, open the session on a connection you keep alive, such as the wrapper from Keep-alive and reconnecting:

// One connection, opened again when it drops (the wrapper from "Keep-alive and reconnecting").
await using var connection = new Resilience.ReconnectingClient(ct => SshClient.ConnectAsync("files.example.com", cancellationToken: ct));

// The file system opens a session on first use, and a new one after the session or its connection fails. It owns the sessions
// it opens and disposes them with itself; the connection stays the wrapper's.
await using var remote = new SftpFileSystem(async ct => await (await connection.GetAsync(ct)).OpenSftpAsync(ct));

var writer = new ReportWriter(remote);
await writer.WriteAsync("reports/2026", "summary.txt", "All green.", cancellationToken);
  • The adapter owns what the factory returns. It disposes each session it replaces, and the last one when it is disposed, so dispose the adapter (Dispose or DisposeAsync) when the application stops, or register it with the container, which does. The connection under the sessions stays yours (or the provider's).
  • The factory is not called concurrently by one adapter, and is called again after it fails: the next call tries again, so the factory (or the connection behind it) decides how often to connect. ISshConnectionProvider waits longer after each failed attempt.
  • Opening counts against the timeout. Each open is bounded by the adapter's OperationTimeout; a call that waits for an open fails when its own timeout passes.
  • Open streams stay on their session. A stream opened before a drop fails; open the file again.

Notes and pitfalls

  • Paths are POSIX. SftpFileSystem.Path uses / on every client OS, and \ is an ordinary character. Relative paths start at the login directory.
  • Synchronous calls block. IFileProvider is synchronous by contract, and so are many IFileSystem methods: each waits for an SFTP round trip and holds a thread meanwhile. Prefer the ...Async methods of IFileSystem under load, and keep the provider's cache on (SftpFileProviderOptions.CacheDuration, 5 seconds by default).
  • Timeouts. SftpFileSystemOptions.OperationTimeout (60 s) and SftpFileProviderOptions.OperationTimeout (30 s) bound each request; for whole-file reads and writes the file system's timeout bounds time without progress, so large files are not cut off.
  • Errors. SftpFileSystem throws the System.IO exceptions code expects (FileNotFoundException, UnauthorizedAccessException, IOException). SftpFileProvider reports a missing or unreadable file as Exists == false.
  • No change notifications. SFTP has none: FileSystemWatcher is not supported, and the provider's Watch never fires, so reloadOnChange has no effect.
  • Ownership. An adapter built from one SftpClient does not own it: dispose it (and the SshClient) after the adapter is no longer used, and disposing the adapter leaves it open. An adapter built from a session factory owns the sessions it opens, and disposes them when it is disposed.
  • Errors after a drop. With one SftpClient, a dropped connection is an SshException from the provider and an IOException from the file system, on every call. With a session factory, a failed open is reported the same way, with the factory's exception inside, and the next call tries again.
  • Hard links (.NET 11). File.CreateHardLink and FileInfo.CreateAsHardLink work where the server offers hardlink@openssh.com (OpenSSH does), and throw NotSupportedException where it does not. SshSharp.IO.Abstractions needs TestableIO.System.IO.Abstractions 23 or later.
  • Not supported by SftpFileSystem: DriveInfo, FileVersionInfo, RandomAccess, SafeFileHandle overloads and File.OpenNullHandle, encryption, Replace; SFTP has no creation times. The SFTP guide has the full table.

See also