5

我在生产中遇到此错误。不知道究竟是什么原因造成的。

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

现在我想模拟我的开发环境中的行为。

  • 我订阅了事件Connection.OnSubscriptionError,我的事件处理程序是OnSubscriptionError. 现在我想测试处理程序。为此,我想要某种方式来触发我遇到问题的那个事件。

  • 我曾尝试在应用程序运行时关闭交换主机服务,但这不会触发OnsubscriptionError Event.

  • 我想知道以下代码在OnsubscriptionError event被触发时是否有效。或者我是否必须在创建连接之前重新创建订阅对象。

这是示例代码:

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)
    { }
 }
4

2 回答 2

4

我实现了与您类似的机制,但您无需打电话,而是Connection.Open();需要重新启动整个订阅...因为正如我上面的@BraveHeart 所说,一旦发生,OnSubscriptionError您将无法重新打开连接...

这是您可以使用的代码片段

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);
}

于 2015-02-27T09:58:08.983 回答
3

OnSubscriptionError被一个连接触发时,这个连接会以某种方式丢失它拥有的所有订阅。所以你不能再次打开连接,因为它没有任何订阅。我个人再次重新创建订阅。我试图将订阅放在一个列表中并直接再次添加它们,但它们不起作用。

于 2013-10-31T12:14:31.173 回答