diff --git a/Extensions/CommonExtensions.cs b/Extensions/CommonExtensions.cs
index ed5d881..cd4f331 100644
--- a/Extensions/CommonExtensions.cs
+++ b/Extensions/CommonExtensions.cs
@@ -14,6 +14,7 @@ using System.Threading.Tasks;
using System.Web;
using System.Xml;
using AutoMapper;
+using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
@@ -169,6 +170,7 @@ namespace xCommons.Extensions
//
var setting = new JsonSerializerSettings
{
+ Formatting = Newtonsoft.Json.Formatting.Indented,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
PreserveReferencesHandling = PreserveReferencesHandling.None
};
@@ -194,6 +196,7 @@ namespace xCommons.Extensions
//
var setting = new JsonSerializerSettings
{
+ Formatting = Newtonsoft.Json.Formatting.Indented,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
PreserveReferencesHandling = PreserveReferencesHandling.None,
};
@@ -670,12 +673,213 @@ namespace xCommons.Extensions
foreach (var item in source)
{
//
- cancellationToken.ThrowIfCancellationRequested();
+ if (cancellationToken.IsCancellationRequested)
+ {
+ yield break;
+ }
+
+ //
yield return item;
- await Task.Yield(); // keeps method truly async
}
}
+ ///
+ /// Converts an Stream to Model AsyncEnumerator ...
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static async IAsyncEnumerable ToModelAsyncEnumerable(
+ this Stream source,
+ [System.Runtime.CompilerServices.EnumeratorCancellation]
+ CancellationToken cancellationToken = default
+ )
+ {
+ //
+ // Validate Stream ...
+ if (source.IsNull())
+ {
+ yield break;
+ }
+
+ //
+ while (source.CanRead)
+ {
+ //
+ var model = await source
+ .ReadModelFromStream(cancellationToken);
+
+ //
+ yield return model;
+ }
+ }
+
+ ///
+ /// Converts an Stream to String AsyncEnumerator ...
+ /// using Read Line of Stream Reader ...S
+ ///
+ ///
+ ///
+ ///
+ public static async IAsyncEnumerable ToStringAsyncEnumerable(
+ this Stream source,
+ [System.Runtime.CompilerServices.EnumeratorCancellation]
+ CancellationToken cancellationToken = default
+ )
+ {
+ //
+ // Validate Stream ...
+ if (source.IsNull())
+ {
+ yield break;
+ }
+
+ //
+ while (source.CanRead)
+ {
+ //
+ var json = await source
+ .ReadJsonFromStream(cancellationToken);
+
+ //
+ yield return json;
+ }
+ }
+
+ ///
+ /// Write a Model to Stream ...
+ ///
+ /// an Stream to Write ...
+ /// a Model instance to Write ...
+ ///
+ ///
+ ///
+ public static async Task WriteModelToStream(
+ this Stream source,
+ T model,
+ CancellationToken cancellationToken = default
+ )
+ {
+ //
+ // Validate Args ...
+ if (source.IsNull() || model.IsNull())
+ {
+ return;
+ }
+
+ //
+ // Extract Json of Model ...
+ var json = model.ToJSON(true);
+
+ //
+ await source.WriteJsonToStream(
+ json: json,
+ cancellationToken: cancellationToken
+ );
+ }
+
+ ///
+ /// Write a Json String to Stream ...
+ ///
+ /// an Stream to Write ...
+ /// json string to write ...
+ ///
+ ///
+ public static async Task WriteJsonToStream(
+ this Stream source,
+ string json,
+ CancellationToken cancellationToken = default
+ )
+ {
+ //
+ // Validate Args ...
+ if (source.IsNull() || json.IsNullOrEmpty())
+ {
+ return;
+ }
+
+ //
+ using (var streamWriter = new StreamWriter(stream: source))
+ {
+ //
+ using (var jsonWriter = new JsonTextWriter(streamWriter))
+ {
+ //
+ await jsonWriter.WriteRawAsync(json, cancellationToken);
+ await jsonWriter.FlushAsync(cancellationToken);
+ }
+ }
+ }
+
+ ///
+ /// Read a Json Model From Stream ...
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static async Task ReadModelFromStream(
+ this Stream source,
+ CancellationToken cancellationToken = default
+ )
+ {
+ //
+ T result = default(T);
+
+ //
+ var json = await source.ReadJsonFromStream(cancellationToken);
+ if (!json.IsNullOrEmpty())
+ {
+ //
+ try
+ {
+ result = json.FromJSON();
+ }
+ catch
+ { }
+ }
+
+ //
+ return result;
+ }
+
+ ///
+ /// Reading Json String From Stream ...
+ ///
+ /// an Stream to Write ...
+ ///
+ ///
+ public static async Task ReadJsonFromStream(
+ this Stream source,
+ CancellationToken cancellationToken = default
+ )
+ {
+ //
+ var result = string.Empty;
+
+ //
+ // Validate Args ...
+ if (source.IsNull())
+ {
+ return result;
+ }
+
+ //
+ using (var streamReader = new StreamReader(source))
+ {
+ //
+ result = await Task.Run(() =>
+ {
+ return streamReader.ReadLine();
+ },
+ cancellationToken);
+ }
+
+ //
+ return result;
+ }
+
///
/// Update a Collection item ...
///