EWS Best Practices – Threading issues with the EWS managed API


An instance of the service object is not safe across threads and it’s noted as such in in the documentation.  IE they are not thread safe. While this seems like a simple point there are a lot of developers who are either ignoring this, don't understand or are not reading the documentation.

ExchangeService class
https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.exchangeservice(v=exchg.80).aspx

So, it’s best you don’t EWS Managed objects across threads otherwise you could well run into issues.  If you do EWS calls on different threads then each thread needs to have its own service object created on that thread.  Watch out for public static variables- objects created as scuh are shared accross threads.

Normally in an application which sync information using streaming notifications, push, pull, etc. you should have one thread listening and have the code spin off 1+ threads which will create their own EWS Managed API service objects in order process each request.  I’ve seen some customers try creating their service object as public static and reuse it on each thread… they eventually run into issues.   So, don’t be afraid to create a service object on each thread to do work – it the right way to code with this EWS Managed API.

Also see:

EWS Best Practices
http://blogs.msdn.com/b/webdav_101/archive/2015/05/03/ews-best-practices.aspx

Comments (0)

Skip to main content