使用EWS BeginSubscribeToStreamingNotifications

时间:2017-03-08 11:49:12

标签: exchangewebservices ews-managed-api

在获得同步流通知后,我发现了异步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, "some@email.address");

    // 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, "some@email.address");

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

所以我的问题是:如何将同步流式订阅转换为异步流式订阅?

1 个答案:

答案 0 :(得分:1)

在回调中,您需要调用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    
}

请注意我如何将服务作为状态传递给回调方法。

我没有测试代码,我自己从未使用过Begin / End方法,但我希望它会让你朝着正确的方向前进。

如果你开始工作,你可能想看看如何使用TaskFactory.FromAsync将开始/结束模式转换为基于Task的模式。