title: EWS exchange sometimes not firing NewMail event using StreamingSubscription
body: <p>I have application that notifies about new mail, created using EWS managed api (v2.2). I am using StreamingSubscription for listening mailbox events. I've noticed that sometimes NewMail event not fired (I see unhandled mail in mailbox later), no connection/subscription exception being raised, and after some period of time new mail receiving as nothing happened... what could cause such behavior? There's nothing suspicious in exchange logs, event just not being fired... Any Ideas and suggestion would be appreciated.</p>
  
  <p>Thanks.</p>
  
  <p>Source:</p>
  
  <pre><code>public class ExchangeClient : IDisposable
  {
      private ExchangeService _exchange;
      private SubscriptionBase _subscription;
      private StreamingSubscriptionConnection _connection;
  
      private bool _disposed;
      private bool _disposing;
  
      public event EventHandler&lt;ExchangeEventArgs&gt; ExchangeEvent;
  
      public ExchangeClient(string userName, string password, string domain, ExchangeVersion version)
      {
          _exchange = new ExchangeService(version);
          _exchange.Credentials = new WebCredentials(userName, password);
  
          _exchange.AutodiscoverUrl(userName + "@" + domain);
  
          var ids = new FolderId[2] { new FolderId(WellKnownFolderName.Root), new FolderId(WellKnownFolderName.Inbox) };
          var events = new List&lt;EventType&gt;();
          events.Add(EventType.NewMail);
  
          _subscription = _exchange.SubscribeToStreamingNotifications(ids, events.ToArray());
          _connection = new StreamingSubscriptionConnection(_exchange, 30);
          _connection.AddSubscription((StreamingSubscription)_subscription);
          _connection.OnNotificationEvent += OnNotificationEvent;
          _connection.OnSubscriptionError += OnSubscriptionError;
          _connection.OnDisconnect += OnDisconnect;
          _connection.Open();
      }
  
      private void OnDisconnect(object sender, SubscriptionErrorEventArgs args)
      {
          if (!_disposing &amp;&amp; _connection != null)
          {
              _connection.Open();
          }
      }
  
      private void OnSubscriptionError(object sender, SubscriptionErrorEventArgs args)
      {
          throw args.Exception;
      }
  
      private void OnNotificationEvent(object sender, NotificationEventArgs args)
      {
          if (_subscription != null)
          {
              if (args.Subscription.Id == _subscription.Id)
              {
                  foreach (var notificationEvent in args.Events)
                  {
                      switch (notificationEvent.EventType)
                      {
                          case EventType.Status:
                              break;
  
                          case EventType.NewMail:
                              NotificationReceived(new ExchangeEventArgs(
                              notificationEvent.EventType,
                              ((ItemEvent)notificationEvent).ItemId, ((ItemEvent)notificationEvent).ParentFolderId));
                              break;
  
                          default:
                              break;
                      }
                  }
              }
          }
      }
  
      public void Disconnect()
      {
          if (_connection.IsOpen)
              _connection.Close();
      }
  
      private void NotificationReceived(ExchangeEventArgs e)
      {
          if (ExchangeEvent != null)
          {
              ExchangeEvent(this, e);
          }
      }
  
      public void Dispose()
      {
          _disposing = true;
          Dispose(_disposing);
          GC.SuppressFinalize(this);
      }
  
      protected virtual void Dispose(bool disposing)
      {
          if (disposing &amp;&amp; !_disposed)
          {
              if (_connection != null)
              {
                  if (_connection.IsOpen)
                      _connection.Close();
                  _connection = null;
              }
              _exchange = null;
              _disposed = true;
          }
      }
  }
  </code></pre>