Where is my streaming subscription going?
Asked Answered
O

2

6

I'm using the Microsoft Exchange Web Services 1.1 SDK and using the streaming connection to subscribe to new mail notification. All works fine for receiving the notifications but I receive errors every once in a while about my Exchange not being able to find my subscription.

Below is the code I'm using to initialize my subscription and the events I use.

public void Subscribe()
{
    var locateMailbox = new Mailbox
                            {
                                Address = "myemail"
                            };
    var folderId = new FolderId(WellKnownFolderName.Inbox, locateMailbox);
    var foldersToWatch = new[] {folderId};
    StreamingSubscription streamingSubscription =
        _exchangeService.SubscribeToStreamingNotifications(foldersToWatch, EventType.NewMail);
    // Timeout is set at 1 minute intentionally
    var streamingConnection = new StreamingSubscriptionConnection(_exchangeService, 1);

    streamingConnection.AddSubscription(streamingSubscription);

    streamingConnection.OnSubscriptionError += ResolveError;
    streamingConnection.OnDisconnect += Reconnect;

    streamingConnection.Open();
}

public void Reconnect(object sender, SubscriptionErrorEventArgs disconnectEventArgs)
{
    if (!((StreamingSubscriptionConnection)sender).IsOpen)
        ((StreamingSubscriptionConnection)sender).Open();
}

public void ResolveError(object sender, SubscriptionErrorEventArgs errorEventArgs)
{
    var streamingSubscriptionConnection =
        (StreamingSubscriptionConnection) sender;
    if (!streamingSubscriptionConnection.IsOpen)
        streamingSubscriptionConnection.Open();
}

ServiceLocalException - You must add at least one subscription to this connection before it can be opened.

That exception speaks for itself and I'm aware that I can simply create another subscription inside of Reconnect(). I'm hoping someone can help me understand where the subscription is going. I can't imagine a product such as Exchange 2010 would simply lose my subscription. Also, I can't pin point the error. Sometimes I can keep my subscription active for 10 minutes and other times I receive an error about my subscription not being valid after 2-3 minutes.

For what it's worth I'm using Exchange 2010 SP1.

Operon answered 6/5, 2011 at 13:8 Comment(0)
A
7

From looking at the source in Reflector, it looks like the only two ways a subscription could be removed (aside from disposing the StreamingSubscriptionConnection, is by calling Remove, which I assume you aren't doing, or by a subscription returning an error code other than ServiceError.ErrorMissedNotificationEvents. You can inspect the error by looking at errorEventArgs.Exception in your ResolveError handler. If it is an instance of ServiceResponseException, cast it to that type and get the ErrorCode property. After firing off the OnSubscriptionError event, the subscription is then removed automatically.

Getting the error code might help you track down why this is happening, but even if you can't fix it, you can determine when the subscription will be removed and asynchronously add in another subscription in that case.

Aalborg answered 8/5, 2011 at 22:14 Comment(4)
I did as you suggested and the error code I get is ErrorSubscriptionNotFound. Not sure why because if I drill down on the sender argument I can see that there's a subscription in there.Operon
The error code is the error that came from the server. When you drill down and see that the subscription is still there, you are on the client. It is the server which can't find the subscription. My guess is the server discarded the subscription after a minute or so because you only gave a timeout of one minute. Try a longer timeout and see if the problem still occurs. Why did you intentionally use a one minute timeout anyway?Aalborg
I set the timeout to one minute because I was getting the same error with a 30 minute timeout. Instead of waiting several sets of 30 minutes to find the error I wanted to see errors faster. But now that I think about it I should still be coding defensively in ensuring I keep an active subscription at all times. You never know when the server will have a hiccup. Thank you.Operon
Looks like I have to wait 6 hours before rewarding the bounty.Operon
D
2

I know this was asked a long time ago but i thought i would post how im getting around the error (cant find anything that explains WHY it happens). Also using office 2010 sp1 by the way.

You can use the Count() method from sender to verify if you have an active subscription;

private static void onDisconnect(object sender, SubscriptionErrorEventArgs args)
    {

        StreamingSubscriptionConnection renew = (StreamingSubscriptionConnection)sender;
        if(renew.CurrentSubscriptions.Count() > 0){ //if subscription exists reopen as normal
            renew.Open(); 
        }
        else
        {
            //recreate the whole connection
        }
    }
Dolf answered 2/9, 2015 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.