locked
EWS Managed API - thread safe? RRS feed

  • Question

  • Any comment on that?

    I'm running two threads (separate service object in each one, no cross-thread communication), but I get an error when loading extended properties on the first folder:

    Collection was modified; enumeration operation may not execute.

    at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)\r\n   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()\r\n   at System.Collections.Generic.List`1.Enumerator.MoveNext()\r\n   at Microsoft.Exchange.WebServices.Data.PropertySet.WriteAdditionalPropertiesToXml(EwsServiceXmlWriter writer, IEnumerable`1 propertyDefinitions)\r\n   at Microsoft.Exchange.WebServices.Data.PropertySet.WriteToXml(EwsServiceXmlWriter writer, ServiceObjectType serviceObjectType)\r\n   at Microsoft.Exchange.WebServices.Data.GetRequest`2.WriteElementsToXml(EwsServiceXmlWriter writer)\r\n   at Microsoft.Exchange.WebServices.Data.GetItemRequestBase`1.WriteElementsToXml(EwsServiceXmlWriter writer)\r\n   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.WriteBodyToXml(EwsServiceXmlWriter writer)\r\n   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.WriteToXml(EwsServiceXmlWriter writer)\r\n   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.Emit()\r\n   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.InternalExecute()\r\n   at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()\r\n   at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalBindToItems(IEnumerable`1 itemIds, PropertySet propertySet, ServiceErrorHandling errorHandling)\r\n   at Microsoft.Exchange.WebServices.Data.ExchangeService.BindToItem(ItemId itemId, PropertySet propertySet)\r\n   at Microsoft.Exchange.WebServices.Data.ExchangeService.BindToItem[TItem](ItemId itemId, PropertySet propertySet)\r\n   at Microsoft.Exchange.WebServices.Data.Item.Bind(ExchangeService service, ItemId id, PropertySet propertySet)\r\n   at Vision.Service.EWSSM.EWS.EWSPropertySet.BindWithRetryOnFailure(Item item) in C:\\Users\\nicw\\code\\git\\comarchive.git\\Services\\EWS Store Manager\\EWS\\EWSPropertySet.cs:line 86" string

    If I rerun the bind, it works fine the second time around. Sometimes it works first time, too :)

    Are there any official comments on EWS MAPI's threadsafeness (oh, thats a bad FLA - MAPI (Managed API) vrs MAPI (Messageing API)....)

    Ta



    Wednesday, March 17, 2010 11:06 AM

Answers

  • No, the API is not thread-safe. Using multiple ExchangeService instances buys you the ability to make multiple calls to EWS on multiple threads, but it doesn't buy you the ability to use an object on one thread while modifying it on the other.

    In this particular case, it looks like one thread was modifying the property set that the other thread was using to call EWS. You have to make sure this doesn't happen.
    David Claux | Program Manager - Exchange Web Services
    • Proposed as answer by David Claux - MSFT Wednesday, March 17, 2010 4:03 PM
    • Marked as answer by NicWise Wednesday, March 17, 2010 4:59 PM
    Wednesday, March 17, 2010 4:02 PM

All replies

  • No, the API is not thread-safe. Using multiple ExchangeService instances buys you the ability to make multiple calls to EWS on multiple threads, but it doesn't buy you the ability to use an object on one thread while modifying it on the other.

    In this particular case, it looks like one thread was modifying the property set that the other thread was using to call EWS. You have to make sure this doesn't happen.
    David Claux | Program Manager - Exchange Web Services
    • Proposed as answer by David Claux - MSFT Wednesday, March 17, 2010 4:03 PM
    • Marked as answer by NicWise Wednesday, March 17, 2010 4:59 PM
    Wednesday, March 17, 2010 4:02 PM
  • I guess I had the wrong term.

    In this case, I spin up a new thread (2 of them eventually), create a new service object, and use it. The service object is never passed or use outside it's creating thread.

    But you DID answer my question :) Thanks!

    Wednesday, March 17, 2010 4:59 PM