2017-03-08 61 views
1

在使同步流式通知正常工作後,我發現異步BeginSubscribeToStreamingNotifications()使用EWS BeginSubscribeToStreamingNotifications

用這個擺弄了一下之後,使用了非常有限的文檔,我放棄了。我根本無法得到這個工作。

環境
這是一些相關的代碼。我正在啓動我的服務,連接並調用BeginSubscribeToStreamingNotifications()方法。

public static void InitSubscriptions() 
{ 
    var service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); 

    service.Credentials = new NetworkCredential("some", "login", "here"); 
    service.Url = new Uri("https://some.exchange/server"); 
    service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "[email protected]"); 

    // Initiating the subscription 
    var subscription = service.BeginSubscribeToStreamingNotifications(new AsyncCallback(Callback), null, new FolderId[] { WellKnownFolderName.Inbox }, EventType.NewMail); 
} 

private static void Callback(IAsyncResult result) 
{ 
    // ??? 
} 

正如你所看到的,我不知道在回調方法中該做什麼。

問題
我不知道如何實際訂閱使用此方法的通知。我使它同步工作,但如果可能的話,我想使它異步工作

下面是按預期工作的同步示例。

public static void InitSubscriptions() 
{ 
    var service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); 

    service.Credentials = new NetworkCredential("some", "login", "here"); 
    service.Url = new Uri("https://some.exchange/server"); 
    service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "[email protected]"); 

    // Initiating the subscription 
    var subscription = service.SubscribeToStreamingNotifications(new FolderId[] { WellKnownFolderName.Inbox }, EventType.NewMail); 
    var connection = new StreamingSubscriptionConnection(service, 30); 

    connection.AddSubscription(subscription); 

    // Delegate handlers 
    connection.OnNotificationEvent += new StreamingSubscriptionConnection.NotificationEventDelegate(SomeMethod); 
    connection.OnSubscriptionError += new StreamingSubscriptionConnection.SubscriptionErrorDelegate(SomeErrorMethod); 
    connection.OnDisconnect += new StreamingSubscriptionConnection.SubscriptionErrorDelegate(SomeDisconnectMethod); 
} 

所以我的問題是:我如何將同步流訂閱轉換爲異步流訂閱?

回答

0

在回調中,您需要撥打EndSubscribeToStreamingNotifications,然後設置連接和事件。

public static void InitSubscriptions() 
{ 
    var service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); 
    // ... 
    service.BeginSubscribeToStreamingNotifications(
     new AsyncCallback(Callback), 
     service, // Pass the service as state. 
     new FolderId[] { WellKnownFolderName.Inbox },  
     EventType.NewMail);  
} 

private static void Callback(IAsyncResult result) 
{ 
    // Retrieve the service from the passed state. 
    var service = result.AsyncState as ExchangeService; 
    var subscription = service.EndSubscribeToStreamingNotifications(result); 

    var connection = new StreamingSubscriptionConnection(service, 30); 
    connection.AddSubscription(subscription); 

    // Delegate handlers 
    connection.OnNotificationEvent += new StreamingSubscriptionConnection.NotificationEventDelegate(SomeMethod); 
    connection.OnSubscriptionError += new StreamingSubscriptionConnection.SubscriptionErrorDelegate(SomeErrorMethod); 
    connection.OnDisconnect += new StreamingSubscriptionConnection.SubscriptionErrorDelegate  
} 

注意我是如何將服務作爲狀態傳遞給回調方法的。

我沒有測試代碼,我從來沒有用過自己的開始/結束方法,但我希望它能讓你朝着正確的方向發展。

如果你得到它的工作,你可能想看看如何使用TaskFactory.FromAsync將開始/結束模式轉換爲基於Task的模式。