Sameer
Sameer

Reputation: 3183

What causes the OnSubscriptionError to be fired in StreamingSubscriptionConnection in EWS API?

I am getting this error in production. Don't know what exactly causes this.

OnSubscriptionError : 
    Microsoft.Exchange.WebServices.Data.ServiceResponseException: 
       The specified subscription was not found.

Now i want to simulate the behavior in my dev environment.

Here is the sample code:

Subscription =_exchangeService.SubscribeToStreamingNotifications(
                new FolderId[] { WellKnownFolderName.Inbox },
                EventType.NewMail);
CreateStreamingSubscription(_exchangeService, Subscription);

private void CreateStreamingSubscription(ExchangeService service,
                                         StreamingSubscription subscription)
{

    // public StreamingSubscriptionConnection(ExchangeService service, int lifetime) lifetime is in minutes and 30 mins is the maximum time till which
    // the connection can be open.
    Connection = new StreamingSubscriptionConnection(service, 30);
    Connection.AddSubscription(subscription);
    Connection.OnNotificationEvent += OnNotificationEvent;
    Connection.OnSubscriptionError += OnSubscriptionError;
    Connection.OnDisconnect += OnDisconnect;
    Connection.Open();
 }  
 private void OnSubscriptionError(object sender, SubscriptionErrorEventArgs args)
 {
    if (args.Exception is ServiceResponseException)
    {
        var exception = args.Exception as ServiceResponseException; 
    }
    else if (args.Exception !=null)
    {
        Logwriter.LogMsg(_clientInfo.LogId, LogLevel.FATAL,
                         "OnSubscriptionError() : " + args.Exception.Message +
                         " Stack Trace : " + args.Exception.StackTrace + 
                         " Inner Exception : " + args.Exception.InnerException);
    }
    try
    {
        Connection = (StreamingSubscriptionConnection)sender;
        if(!Connection.IsOpen)
            Connection.Open();
    }
    catch (ServiceResponseException exception)
    { }
    catch (Exception ex)
    { }
 }

Upvotes: 6

Views: 3199

Answers (2)

Raz Shuty
Raz Shuty

Reputation: 51

I implemented a similar mechinisem as you did, but instead of calling Connection.Open(); you need to restart your entire subscription... because as @BraveHeart above me said, once OnSubscriptionError occur you can't reopen the connection.

This is a code snippet that you can use

private void OnSubscriptionError(object sender, SubscriptionErrorEventArgs args)
{
    connection = (StreamingSubscriptionConnection)sender;
    //Log here
    if (args.Subscription != null)
    {
        try
        {
            connection.RemoveSubscription(args.Subscription);
        }
        catch (Exception removeSubscriptionException)
        {
            //Log Here
        }
     }
     if (connection != null && connection.IsOpen)
     {
         try
         {
             connection.Close();
         }
         catch (Exception subscriptionCloseException)
         {
             //Log Here
         }
      }
      Subscription =_exchangeService.SubscribeToStreamingNotifications(
             new FolderId[] { WellKnownFolderName.Inbox },EventType.NewMail);
      CreateStreamingSubscription(_exchangeService, Subscription);
}

Upvotes: 5

Ahmad ElMadi
Ahmad ElMadi

Reputation: 2617

When OnSubscriptionError fired by a connection, this connection somehow loses all the subscriptions it has. So you cannot open the connection again, since it does not have any subscription . I personally recreate the subscriptions again. I have tried to put the subscriptions in a list and add them again directly but they did not work.

Upvotes: 4

Related Questions