C# Outlook error: The object invoked has disconnected from its clients

Add-in Express™ Support Service
That's what is more important than anything else

C# Outlook error: The object invoked has disconnected from its clients
 
Jes?s Alexander Guzman Silva




Posts: 1
Joined: 2015-03-23
Hi! I'm creating a plugin for outlook appoiments and meetings, so, when I try to set the events ItemAdd, ItemChange and BeforeDeleted in the appoiments items, I get the current error:
The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))
.

In some PCs this error not happen, but in anothers this is persistent and I dont know why is the problem.

My code is this:


private List<Outlook.MAPIFolder> calendarFolders = new List<Outlook.MAPIFolder>();
private List<Outlook.Items> calendarItems = new List<Outlook.Items>();
private List<Outlook.AppointmentItem> appItems = null;

private async void AddinModule_AddinInitialize(object sender, EventArgs e)
{        
        try
        {
            var app = this.OutlookApp;
            Outlook.NameSpace nameSpace = app.GetNamespace("MAPI");
            foreach (Outlook.Store store in nameSpace.Stores)
            {
                if (store.IsCachedExchange)
                {
                    calendarFolders.Add(store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar));
                }
            }

            await updateAppItemList();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error CODE 0x01", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
}


        private Task updateAppItemList()
        {
            appItems = new List<Outlook.AppointmentItem>();
            foreach (Outlook.MAPIFolder folder in calendarFolders)
            {
                Outlook.Items items = folder.Items;
                var parent = items.Parent as Outlook.MAPIFolder;
                if (!calendarItems.Any(x => (x.Parent as Outlook.MAPIFolder).StoreID == folder.StoreID))
                {
                    items.ItemAdd += this.ItemAdd;
                    items.ItemChange += this.ItemChange;

                    calendarItems.Add(items);
                }

                foreach (var item in items)
                {
                    Outlook.AppointmentItem appItem = item as Outlook.AppointmentItem;
                    if (appItem != null)
                    {
                        appItem.BeforeDelete += this.ItemRemove;
                        appItems.Add(appItem);
                    }
                }
            }
            return Task.FromResult(0);
        }
Posted 23 Mar, 2015 12:18:38 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Jes?s Alexander,

1. Don't use async and await; there's nothing of asynchronous nature in your code. Also note that the Outlook object model (actually, all object models in Office) are not thread-safe.

2. I strongly recommend that you check section Releasing COM Objects in the PDF file in the folder {Add-in Express}\Docs on your development PC. It describes why you need to release COM objects in your code, what objects to release, why to use for loops instead of foreach loops, etc.

3. I think you may misunderstand the BeforeDelete event. The event won't fire when you delete an item that isn't open; check the Remarks section at https://msdn.microsoft.com/EN-US/library/office/ff869458.aspx.


Andrei Smolin
Add-in Express Team Leader
Posted 24 Mar, 2015 02:21:02 Top