Rebex Graph

.NET client library for MS Graph API (Exchange Online)

Download 30-day free trial Buy from $199
More .NET libraries

Back to feature list...

Asynchronous operations

Task-based Asynchronous Pattern (.NET 4.0 or later) 

.NET 4.0 introduced the Task-based Asynchronous Pattern (TAP). Rebex Graph supports it as well - just reference the assemblies for .NET 4.0 or later from your project.

// create Graph client instance, connect, log in
// ...

// start async operation
Task<MailMessage> t = client.GetMailMessageAsync(messageId);

// set continuation method
t.ContinueWith(GetMailMessageFinished);

Sample asynchronous continuation method:

// this method is called when the async operation is completed
private void GetMailMessageFinished(Task<MailMessage> t)
{
    // show error if any
    if (t.IsFaulted)
    {
        Console.WriteLine("An error occurred: {0}", t.Exception.ToString());
        return;
    }

    // show cancel notification
    if (t.IsCanceled)
    {
        Console.WriteLine("Operation was canceled.");
        return;
    }

    // get operation result
    MailMessage mail = t.Result;

    // show result
    Console.WriteLine("Mail downloaded: {0}", mail.Subject);
}

'await' operator support (.NET 4.5 or later) 

.NET 4.5 introduced the 'await' operator, which makes writing asynchronous code more easy than ever before. Rebex Graph supports it as well, just reference the assemblies for .NET 4.0 or later from your project.

// create Graph client instance, connect, log in
// ...

try
{
    // download message asynchronously
    MailMessage mail = await client.GetMailMessageAsync(messageId);

    // show result
    Console.WriteLine("Mail downloaded: {0}", mail.Subject);
}
catch (GraphException ex)
{
    Console.WriteLine("An error occurred: {0}.", ex.Message);
}
catch (Exception ex)
{
    Console.WriteLine("An error occurred: {0}", ex.ToString());
}

IAsyncResult pattern (Begin/End methods) 

The Asynchronous Programming Model (APM) pattern, also known as IAsyncResult (or Begin/End) pattern, was introduced in .NET 1.0. When you reference Rebex assemblies for .NET 2.0 or .NET 3.5 in your project, this is the pattern you can use.

// start async operation, specify callback
IAsyncResult ar = client.BeginGetMailMessage(messageId, GetMailMessageFinished, null);

Sample asynchronous callback method:

// this method is called when the Begin method is completed
private void GetMailMessageFinished(IAsyncResult ar)
{
    try
    {
        // get operation result
        MailMessage mail = client.EndGetMailMessage(ar);

        // show result
        Console.WriteLine("Mail downloaded: {0}", mail.Subject);
    }
    catch (GraphException ex)
    {
        Console.WriteLine("An error occurred: {0}.", ex.Message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("An error occurred: {0}", ex.ToString());
    }
}

SynchronizationContext support for events 

When an asynchronous method on the GraphClient object raises an event, it's dispatched using the synchronization context captured when the operation was started.

This greatly simplifies writing GUI applications (Windows Forms and WPF) because all the events are raised on the applications GUI thread, eliminating the need to explicitly marshal the calls using the Invoke method.

If you need to disable this functionality, set GraphClient.Settings.RaiseEventsFromCurrentThread to true. This will cause all the events to be raised on the background operation's thread.

Back to feature list...