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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With