Exchange server 是什么导致在EWS API中的StreamingSubscriptionConnection中触发OnSubscriptionError?

Exchange server 是什么导致在EWS API中的StreamingSubscriptionConnection中触发OnSubscriptionError?,exchange-server,exchangewebservices,Exchange Server,Exchangewebservices,我在生产中遇到了这个错误。不知道到底是什么原因造成的 OnSubscriptionError : Microsoft.Exchange.WebServices.Data.ServiceResponseException: The specified subscription was not found. 现在我想在我的开发环境中模拟行为 我已经订阅了事件连接。OnSubscriptionError并且我的事件处理程序是OnSubscriptionError。现在我想

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

OnSubscriptionError : 
    Microsoft.Exchange.WebServices.Data.ServiceResponseException: 
       The specified subscription was not found.
现在我想在我的开发环境中模拟行为

  • 我已经订阅了事件
    连接。OnSubscriptionError
    并且我的事件处理程序是
    OnSubscriptionError
    。现在我想测试处理程序。为此,我想用某种方法来触发我遇到麻烦的事件

  • 我已尝试在应用程序运行时关闭exchange主机服务,但这不会触发
    OnsubscriptionError事件

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

以下是示例代码:

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

当一个连接触发
OnSubscriptionError
时,该连接会以某种方式丢失它拥有的所有订阅。因此,您无法再次打开连接,因为它没有任何订阅。
我亲自重新创建订阅。我曾尝试将订阅放入一个列表中,然后直接重新添加,但没有成功

我实现了与您类似的机制,但没有调用
Connection.Open()您需要重新启动整个订阅。。。因为正如我上面的@BraveHeart所说,一旦发生订阅错误,您就无法重新打开连接

这是一个可以使用的代码段

SubscriptionError上的私有void(对象发送方、SubscriptionErrorEventArgs args args) { 连接=(StreamingSubscriptionConnection)发送方; //在这里登录 如果(args.Subscription!=null) { 尝试 { connection.RemoveSubscription(args.Subscription); } 捕获(异常removeSubscriptionException) { //在这里登录 } } if(connection!=null&&connection.IsOpen) { 尝试 { connection.Close(); } 捕获(异常订阅关闭异常) { //在这里登录 } } 订阅=\u exchangeService.SubscribeToStreamingNotifications( 新文件夹ID[]{WellKnownFolderName.Inbox},EventType.NewMail); CreateStreamingSubscription(_exchangeService,Subscription); }