0
votes

I'm using the following code to get calendar appointments from my Office 365 account with EWS api's set (Microsoft.Exchange.WebServices 2.2.0):

ExchangeService service = new ExchangeService();
service.Credentials = new NetworkCredential(mail, password);
service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
Folder DefaultCalendar = Folder.Bind(service, WellKnownFolderName.Calendar);
CalendarView calendarView = new CalendarView(fromDate, toDate);
FindItemsResults<Appointment> resultAppointments = Folder.FindAppointments(calendarView);

often the application crash with the following exceptions:

Microsoft.Exchange.WebServices.Data.ServiceRequestException: The request failed. The operation has timed out ---> System.Net.WebException: The operation has timed out at System.Net.HttpWebRequest.GetResponse() at Microsoft.Exchange.WebServices.Data.EwsHttpWebRequest.Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest.GetResponse() at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request) --- End of inner exception stack trace --- at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request) at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute() at Microsoft.Exchange.WebServices.Data.ExchangeService.BindToFolder(FolderId folderId, PropertySet propertySet) at Microsoft.Exchange.WebServices.Data.ExchangeService.BindToFolder[TFolder](FolderId folderId, PropertySet propertySet) at Microsoft.Exchange.WebServices.Data.CalendarFolder.Bind(ExchangeService service, FolderId id)

or:

Microsoft.Exchange.WebServices.Data.ServiceRequestException: The request failed. The remote server returned an error: (503) Server Unavailable. ---> System.Net.WebException: The remote server returned an error: (503) Server Unavailable. at System.Net.HttpWebRequest.GetResponse() at Microsoft.Exchange.WebServices.Data.EwsHttpWebRequest.Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest.GetResponse() at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request) --- End of inner exception stack trace --- at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request) at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute() at Microsoft.Exchange.WebServices.Data.ExchangeService.BindToFolder(FolderId folderId, PropertySet propertySet) at Microsoft.Exchange.WebServices.Data.ExchangeService.BindToFolder[TFolder](FolderId folderId, PropertySet propertySet) at Microsoft.Exchange.WebServices.Data.CalendarFolder.Bind(ExchangeService service, FolderId id)

or:

Microsoft.Exchange.WebServices.Data.ServiceRequestException: The request failed. The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. ---> System.Net.WebException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. at System.Net.HttpWebRequest.GetResponse() at Microsoft.Exchange.WebServices.Data.EwsHttpWebRequest.Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest.GetResponse() at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request) --- End of inner exception stack trace --- at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request) at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest1.Execute() at Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems[TItem](IEnumerable1 parentFolderIds, SearchFilter searchFilter, String queryString, ViewBase view, Grouping groupBy, ServiceErrorHandling errorHandlingMode) at Microsoft.Exchange.WebServices.Data.Folder.InternalFindItems[TItem](SearchFilter searchFilter, ViewBase view, Grouping groupBy) at Microsoft.Exchange.WebServices.Data.CalendarFolder.FindAppointments(CalendarView view)

or:

The server cannot service this request right now. Try again later.

It seems that sometimes Office 365 services are unreachable or on throttling; I tried to try-catch my code, but the application still crash; I think because a new Thread is opened. How can I handle this issue?

1

1 Answers

2
votes

The first place to start is to get the latest version of the EWS Managed API from Github https://github.com/OfficeDev/ews-managed-api . The version you using hasn't been update since 2015 which is when Microsoft stopped releasing compiled versions of that library. However the code has been updated with numerous bug fixes on GitHub

With the code your using I would suggest you always set the X-AnchorMailBox which can cause 503 and timeout issues see https://blogs.msdn.microsoft.com/webdav_101/2015/05/11/best-practices-ews-authentication-and-access-issues/ (even if your not using Impersonation)

Depending on how much work your application is doing you may get throttled its important in that case to process the exception and retry. Also with Office365 mailboxes get moved around a fair bit on the back-end so it can be common to see period where the Mailbox is inaccessible during these moves

I tried to try-catch my code, but the application still crash; I think because a new Thread is opened. How can I handle this issue?

No it doesn't span a new thread you can check the source yourself on GitHub so you must have another issue with your exception handling.