1
0

Using.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 Create(() =>
  19. {
  20. var resource = resourceFactory();
  21. var e = default(IAsyncEnumerator<TSource>);
  22. try
  23. {
  24. e = enumerableFactory(resource)
  25. .GetEnumerator();
  26. }
  27. catch (Exception)
  28. {
  29. resource.Dispose();
  30. throw;
  31. }
  32. var cts = new CancellationTokenDisposable();
  33. var d = Disposable.Create(cts, resource, e);
  34. var current = default(TSource);
  35. return Create(
  36. async ct =>
  37. {
  38. bool res;
  39. try
  40. {
  41. res = await e.MoveNext(cts.Token)
  42. .ConfigureAwait(false);
  43. }
  44. catch (Exception)
  45. {
  46. d.Dispose();
  47. throw;
  48. }
  49. if (res)
  50. {
  51. current = e.Current;
  52. return true;
  53. }
  54. d.Dispose();
  55. return false;
  56. },
  57. () => current,
  58. d.Dispose,
  59. null
  60. );
  61. });
  62. }
  63. }
  64. }