EWS Managed API - Error handling RRS feed

  • Question

  • Hi everybody,

    I am working on a little application that is notified via streaming notifications when mails on exchange server arrive.

    The whole notification system and mail processing works fine, but I have problems to handle errors if one occurs on the StreamingSubscriptionConnection or the exchange service itself.

    The StreamingSubscriptionConnection provides the following events:



    So far my understanding of these two events is:


    if the exception in the SubscriptionErrorEventArgs is null it is a expected and normal shutdown of the connection due to lifetime expiration. In this case a simply reopening call on the connection is enough to reestablish event notificaiton

    if the exception is not null some major problem occured e.g. loss of internet connection (I especially want to consider that case). Currently I initialize the exchange server again, create a new subscription and also a new StreamingSubscriptionConnection when this case occures.


    Somehow the subscription information got lost. Its not enough to reopen the StreamingSubscriptionConnection (I ve tried it). One has to create a new subscription and init a new StreamingSubscriptionConnection with it.

    Am I right so far?

    Are there any cases where a combination of these events are fired? Have log entries all over the application. Sometimes both events are fired in a row and can t see a correlation why


    Friday, May 9, 2014 9:00 AM

All replies

  • What version of Exchange are you using and how complex is the Org your using it in ?. In larger orgs or in the cloud affinity is import eg . So in 2010 if your going through a load balancer and your streaming request don't go back through the same CAS server you maybe get error like Subscription not found while the Sub is still valid.

    One thing to keep in mind is the EWS Managed API is a client side library so those events are generated by the Client library not the server. Because it's closed source it hard to give an exact answer but OnSubscriptionError generally means the response the client library received from the server was an error, OnDisconnect as you mentioned generally means the subscription has ended normally. Errors that may fall outside this is a TimeOut eg if the client's internet timeouts for some reason or some other connectivity error. In that case the Subscription itself is still valid and you just need to make a GetEvent request (Open the connection) to restart the current subscription however the current streaming operation is invalid and there will be a period of time where you won't have received events from the server. You need to think about where you apps is going to run and test for that eg things like Laptops going into sleep and then coming out and your app restarting before the wireless network connection is re-established etc.(hard to test but one issue I've had)


    Monday, May 12, 2014 4:19 AM
  • I m using Exchange 2013 and also initialize the ExchangeService with that parameter.

    What do you mean with 'Org'?

    My app only listens to events in a single folder in a single mailbox on the exchange server. If some conditions are fulfilled it sends a mail from this mail acc (about 3 - 5 a day). Should be not that sophisticated.

    During the last days I looked deeper into the log files and the received events.

    From time to time the OnDisconnect event is thrown but its exception is not null. It says ' The XML document ended unexpectedly.' After googleing I came across the option to trace ews requests with a ITraceListener implementing class. With that I get the xml files that are communicated between exchange server and my app. Will look deeper in the xml content when the error occures the next times.

    My overall error handling at the moment works like this:

    -App receiving OnDisconnect event

    ->Exception of the EventArgs is null: Just reopen the connection

    ->Exception not null: Dispose old exception an reinitialize it, if it still doesnt work reinit the exchange service and reestablish subscription and connection

    -App receiving OnSubscritpionError event:

    -> same as Exception not null in OnDisconnect

    Is this a generally appropriate error handling at all, or am I completely wrong?

    Monday, May 12, 2014 8:50 AM
  • >> What do you mean with 'Org'?

    The Microsoft Exchange Organization

    >>  The XML document ended unexpectedly.'

    Yeah you need to look at the traces to work out what happened here, it sounds like it got an invalid response from the server (eg something it couldn't parse). Streaming notifications make a Hanging http get so what may have happened is that it hasn't received the response correctly. Are you going through any proxies ?

    With error handling you want to actually process the error correctly and see what caused the issue or else you may find your application gets into an infinite loop. Eg say you received an error that related to throttling and your response was just to resubscribe the notification immediately when you get any exception. Your application may then end just getting stuck in a loop doing this several times a minutes and just fill your logs with errors. The same thing with access denied, eg what happens if somebody changes the password or it expires. Making generic assumptions about exceptions is not good practice.



    Tuesday, May 13, 2014 4:50 AM
  • Thanks for your time and responses.

    I changed my code a litte and apparently the changes work (no loop or crash since).

    Actually error handling works as described in my last post now. I just had some bugs in my code.

    Now my app reacts appropriate to events sent my EWS.

    I m sure there are cases I haven t considered yet because they haven t occured. But thats work for the future :)

    Thursday, May 15, 2014 7:45 AM