TestableIO/System.IO.Abstractions

IFileSystemWatcher dependency error

ascosmin09 opened this issue · 3 comments

Describe the bug
When running the app: System.InvalidOperationException: Unable to resolve service for type 'System.IO.Abstractions.IFileSystemWatcher' while attempting to activate.

To Reproduce
Steps to reproduce the behavior:

  1. Inject IFileSystemWatcher in the constructor like:

using System.IO.Abstractions;
namespace example;
public class ExampleClass{
private readonly IFileSystemWatcher _fileSystemWatcher;
public ExampleClass(IFileSystemWatcher fileSystemWatcher){
this._fileSystemWatcher = fileSystemWatcher;
}
}

  1. Run the program.

Expected behavior
No error.

Additional context

We don't provide out-of-the-box support for dependency injection frameworks, so I guess you need to do some bootstrapping, e.g. like described in the .NET documentation.

Can you please give me an example?
Is this the right way to do it?
services.AddSingleton(sp => new FileSystemWatcherWrapper(new FileSystem()));
services.AddSingleton(sp=> new FileWrapper(new FileSystem()));
services.AddSingleton(sp=>new DirectoryWrapper(new FileSystem()));

Not an expert, but this should be enough:

services.AddSingleton<IFileSystem, FileSystem>();