Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Async TPL methods #57

Open
turowicz opened this issue Aug 20, 2015 · 7 comments
Open

Async TPL methods #57

turowicz opened this issue Aug 20, 2015 · 7 comments

Comments

@turowicz
Copy link

Is there any way to use this API in the truly async-await fashion where the threads aren't being blocked? If not I think that is a great feature request. At least listing folders, opening folders and binding items. Especially for email messages.

@turowicz
Copy link
Author

I did something like this as a proof of concept:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Exchange.WebServices.Data;

namespace Exchange
{
    public static class ExchangeServiceExtensions
    {
        public static Task<List<Folder>> LoadFoldersAsync(this ExchangeService service)
        {
            var tcs = new TaskCompletionSource<List<Folder>>();

            try
            {
                var properties = new PropertySet(BasePropertySet.IdOnly, FolderSchema.DisplayName,
                                 new ExtendedPropertyDefinition(0x10f4, MapiPropertyType.Boolean));
                service.BeginSyncFolderHierarchy(ar =>
                {
                    try
                    {
                        var result = service.EndSyncFolderHierarchy(ar);
                        tcs.TrySetResult(result.Where(x => x.ChangeType == ChangeType.Create).Select(x => x.Folder).ToList());
                    }
                    catch (OperationCanceledException)
                    {
                        tcs.TrySetCanceled();
                    }
                    catch (Exception ex)
                    {
                        tcs.TrySetException(ex);
                    }
                }, null, new FolderId(WellKnownFolderName.MsgFolderRoot), properties, null);
            }
            catch
            {
                tcs.TrySetResult(Enumerable.Empty<Folder>().ToList());
                throw;
            }

            return tcs.Task;
        }

        public static Task<List<EmailMessage>> LoadEmailMessagesAsync(this ExchangeService service, FolderId folderId, Int32 limit = 500)
        {
            var tcs = new TaskCompletionSource<List<EmailMessage>>();

            try
            {
                var properties = new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.From, 
                                                                         EmailMessageSchema.Subject, 
                                                                         EmailMessageSchema.HasAttachments, 
                                                                         EmailMessageSchema.DateTimeReceived,
                                 new ExtendedPropertyDefinition(0x10f4, MapiPropertyType.Boolean));
                service.BeginSyncFolderItems(ar =>
                {
                    try
                    {
                        var result = service.EndSyncFolderItems(ar);
                        tcs.TrySetResult(result.Where(x => x.Item is EmailMessage).Select(x => x.Item as EmailMessage).ToList());
                    }
                    catch (OperationCanceledException)
                    {
                        tcs.TrySetCanceled();
                    }
                    catch (Exception ex)
                    {
                        tcs.TrySetException(ex);
                    }
                }, null, folderId, properties, null, limit, SyncFolderItemsScope.NormalItems, null);


            }
            catch
            {
                tcs.TrySetResult(Enumerable.Empty<EmailMessage>().ToList());
                throw;
            }

            return tcs.Task;
        }
    }
}

@turowicz
Copy link
Author

It works pretty well, do you guys think this could be a way to go about introducing async-await to the library?

@turowicz
Copy link
Author

Any idea how to get a single Item's (EmailMessage) Body+Attachments by just the ID asynchronously? Item.Bind doesn't cut it.

@kasperdaff
Copy link

+1 👍

1 similar comment
@naeemkhedarun
Copy link

+1 👍

@turowicz
Copy link
Author

Created a PR

#61

@MpDzik
Copy link
Contributor

MpDzik commented Nov 18, 2015

+1

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants