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.
I have subscribed to event Connection.OnSubscriptionError
and my event handler is OnSubscriptionError
. Now I want to test the handler. For that purpose I want some way to trigger that event with which i ham having trouble with.
I have tried shutting down the exchange host service while the application is running , but that doesn't fire the OnsubscriptionError Event
.
I wanted to know whether the following code works when OnsubscriptionError event
is fired. Or do i have to recreate the Subscription object before creating the connection.
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)
{ }
}
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
occure 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);
}
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.
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