Я немного запутался со следующим кодом:Наблюдаемые Обработка исключений с Directory.EnumerateFiles
Вот что я думаю, что должно происходить
directorySearch создаются как наблюдаемые, но не выполняется поскольку он не был перечислен.
Методы расширения уловов должны улавливать все возможные ошибки файловой системы, у меня есть дополнительный catch catch для отладки.
Что происходит:
Исключения бросают! :(
var counter = 0; var directorySearch = Directory.EnumerateFiles("//.", "*.*", SearchOption.AllDirectories).ToObservable(); directorySearch .Catch<string, DirectoryNotFoundException>(tx => Observable.Return(string.Empty)) .Catch<string, UnauthorizedAccessException>(tx => Observable.Return(string.Empty)) .Catch<string, PathTooLongException>(tx => Observable.Return(string.Empty)) .Where(next => !string.IsNullOrWhiteSpace(next)) .Select(Path.GetFileName) .Do(_ => Debug.WriteLine("Logging, update some counter or something..")) .Do(_ => counter++) .Subscribe(_ => { Debug.WriteLine("File: " + _); }); counter.Should().Be(18, "because there are that many files in the directory");
Похоже, что ToObservable вызывает перечисление произойдет.
System.IO.DirectoryNotFoundException : Could not find a part of the path 'D:\Projects\DirectorySearching\SearchingSamples\SearchingSamples\bin\Debug\fake'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileSystemEnumerableIterator`1.CommonInit()
at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
at System.IO.Directory.EnumerateFiles(String path, String searchPattern, SearchOption searchOption)
at SearchingSamples.searching_samples_using_linq.reactive_showing_data_stream_processing() in searching_samples_using_linq.cs: line 47
Вы можете вставить трассировку стека? – Neel
Что такое «поддельный» и где он используется? – Neel
Просто предложение - не делайте 'counter ++' - есть перегрузка по выбору, которая даст вам индексный номер значения, создаваемого наблюдаемым, поэтому вам не придется полагаться на побочные эффекты. – Enigmativity