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

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

现在,我想在开发环境中模拟行为。
  • 我已订阅事件Connection.OnSubscriptionError,我的事件处理程序是OnSubscriptionError。现在,我要测试处理程序。为此,我想要某种触发事件的方法,我很难解决。
  • 我试图在应用程序运行时关闭Exchange主机服务,但这不会触发OnsubscriptionError Event
  • 我想知道触发OnsubscriptionError event时以下代码是否有效。还是在创建连接之前必须重新创建Subscription对象。

  • 这是示例代码:
    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)
        { }
     }
    

    最佳答案

    我实现了与您类似的机制,但无需调用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);
    }

    关于exchange-server - 是什么原因导致EWS API的StreamingSubscriptionConnection中引发OnSubscriptionError?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16579638/

    10-12 20:11