VcfDeserializeAsync(FuncCancellationToken, TaskStream, AnsiFilter, CancellationToken) Method

Deserializes a Stream of VCF data and selects the correct Encoding automatically in an asynchronous operation.

Definition

Namespace: FolkerKinzel.VCards
Assembly: FolkerKinzel.VCards (in FolkerKinzel.VCards.dll) Version: 8.0.1+a91cc3f0fd39aeb548e16006a60ca9dd10a304a2
C#
public static Task<IReadOnlyList<VCard>> DeserializeAsync(
	Func<CancellationToken, Task<Stream>> factory,
	AnsiFilter filter,
	CancellationToken token = default
)

Parameters

factory  FuncCancellationToken, TaskStream
A function that takes a CancellationToken as argument and returns a Stream of VCF data as an asynchronous operation.
filter  AnsiFilter
An AnsiFilter instance.
token  CancellationToken  (Optional)
A cancellation token that can be used by other objects or threads to receive notice of cancellation.

Return Value

TaskIReadOnlyListVCard
The task object representing the asynchronous operation.

Remarks

AnsiFilter only recognizes one Encoding per Stream. This means that if a Stream contains VCF data with different Encodings, decoding errors may occur.

Any Streams that are used within the method will be closed when the method completes.

Example

C#
using FolkerKinzel.VCards;

namespace Examples;

public static class WebExample
{
    private const string URI =
        "https://raw.githubusercontent.com/FolkerKinzel/VCards/master/src/Examples/AnsiFilterExamples/German.vcf";

    private static readonly HttpClient _client = new();

    public static async Task AsyncExample()
    {
        using var cts = new CancellationTokenSource();

        IReadOnlyList<VCard> vc = await Vcf.DeserializeAsync(t => _client.GetStreamAsync(URI, t),
                                                     new AnsiFilter(),
                                                     cts.Token);
        Console.WriteLine(vc[0]);
    }

    public static void SynchronousExample()
    {
        using HttpResponseMessage response =
            _client.Send(new HttpRequestMessage(HttpMethod.Get, URI));

        IReadOnlyList<VCard> vc = Vcf.Deserialize(() => response.Content.ReadAsStream(),
                                          new AnsiFilter());
        Console.WriteLine(vc[0]);
    }
}

/*
Console Output:

Version: 2.1

DisplayNames: Sören Täve Nüßlebaum

NameViews:
    FamilyNames:     Nüßlebaum
    GivenNames:      Sören
    AdditionalNames: Täve

[Label: Mößlitz]
[CharSet: Windows-1252]
Addresses: Locality: Mößlitz
 */

Exceptions

ArgumentNullExceptionfactory or filter is null.
ArgumentExceptionThe Stream that factory returns asynchronously doesn't support reading.
ObjectDisposedExceptionfactory returns a closed stream.
IOException The method could not read from the Stream.

See Also