Using.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerable
  11. {
  12. public static IAsyncEnumerable<TSource> Using<TSource, TResource>(Func<TResource> resourceFactory, Func<TResource, IAsyncEnumerable<TSource>> enumerableFactory) where TResource : IDisposable
  13. {
  14. if (resourceFactory == null)
  15. throw new ArgumentNullException(nameof(resourceFactory));
  16. if (enumerableFactory == null)
  17. throw new ArgumentNullException(nameof(enumerableFactory));
  18. return CreateEnumerable(
  19. () =>
  20. {
  21. var resource = resourceFactory();
  22. var e = default(IAsyncEnumerator<TSource>);
  23. try
  24. {
  25. e = enumerableFactory(resource)
  26. .GetEnumerator();
  27. }
  28. catch (Exception)
  29. {
  30. resource.Dispose();
  31. throw;
  32. }
  33. var cts = new CancellationTokenDisposable();
  34. var d = Disposable.Create(cts, resource, e);
  35. var current = default(TSource);
  36. return CreateEnumerator(
  37. async ct =>
  38. {
  39. bool res;
  40. try
  41. {
  42. res = await e.MoveNext(cts.Token)
  43. .ConfigureAwait(false);
  44. }
  45. catch (Exception)
  46. {
  47. d.Dispose();
  48. throw;
  49. }
  50. if (res)
  51. {
  52. current = e.Current;
  53. return true;
  54. }
  55. d.Dispose();
  56. return false;
  57. },
  58. () => current,
  59. d.Dispose,
  60. null
  61. );
  62. });
  63. }
  64. }
  65. }