EWS API 多个流媒体订阅混乱

问题描述 投票:0回答:2

我已“管理”通过 OnNotificationEvent 委托订阅多个日历的更改事件,但我不知道如何确定哪个帐户发送了该事件。 OnNotificationEvent => sender 返回最后一个模拟 id,如果我想读取 args->item.unqueid 的内容,我需要知道谁(哪个 smtp 帐户)来读取它。

static void Main(string[] args)
        {
            var service = EWService();

            service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "[email protected]");

            var StreamingSubscription = service.SubscribeToStreamingNotifications(
                new FolderId[] { WellKnownFolderName.Calendar },                
                EventType.FreeBusyChanged);
            
            StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(service, 30);
            connection.AddSubscription(StreamingSubscription);
            connection.OnNotificationEvent += OnNotificationEvent;
            connection.OnDisconnect += OnDisconnect;
            connection.OnSubscriptionError += OnSubscriptionError;            
            connection.Open();


            service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "[email protected]");

            var StreamingSubscription2 = service.SubscribeToStreamingNotifications(
                new FolderId[] { WellKnownFolderName.Calendar },  
                EventType.FreeBusyChanged);

            StreamingSubscriptionConnection connection2 = new StreamingSubscriptionConnection(service, 30);
            connection2.AddSubscription(StreamingSubscription2);
            connection2.OnNotificationEvent += OnNotificationEvent;
            connection2.OnDisconnect += OnDisconnect;
            connection2.OnSubscriptionError += OnSubscriptionError;
            connection2.Open();

            Console.WriteLine("Waiting...");

            while (isRunning ==true)
            {

            }

            StreamingSubscription.Unsubscribe();
            connection.Close();

            StreamingSubscription2.Unsubscribe();
            connection2.Close();

        }

private static void OnNotificationEvent(object sender, NotificationEventArgs args)
{
    foreach (var item in ((StreamingSubscriptionConnection)sender).CurrentSubscriptions)
    {
        var id = item.Service.ImpersonatedUserId.Id;
        Console.WriteLine($"Event for: {id} ");
    }           

    foreach (var item in args.Events)
    {
        Console.WriteLine(item.EventType);
        if (item.EventType == EventType.FreeBusyChanged)
        {
            Console.WriteLine(((ItemEvent)item).ItemId);
        }
    }
}
c# api exchangewebservices impersonation
2个回答
0
投票

通常,最好的方法是在创建订阅时跟踪订阅 ID,该订阅 ID 将返回到您创建订阅所针对的邮箱。 Exchange 仅返回 Streaming 请求的 SubscriptionId。另一种解决方法是使用已更改的项目(或文件夹)的 ParentFolderId,然后获取 StoreId,然后您可以从中解码 LegacyExchangeDn 并将其解析为邮箱的 SMTP 地址。但这需要多个网络请求,因此您最好在创建订阅时执行此操作。


0
投票

这是一个通过模拟为多个用户使用订阅的示例,它是用java编写的,但也许对某人有用

String roomMail = "[email protected]";
String userMail = "[email protected]";
ExchangeService service = createEWSService(serverUrl, emailLogin, password, proxyHost, proxyPort, proxyLogin, proxyPassword, proxyDomain);
service.getHttpHeaders().put("X-AnchorMailbox", roomMail);
service.getHttpHeaders().put("X-PreferServerAffinity", "true");
service.setImpersonatedUserId(new ImpersonatedUserId(ConnectingIdType.SmtpAddress, roomMail));
StreamingSubscription streamingSubscriptionRoom = service.subscribeToStreamingNotifications(Collections.singleton(new FolderId(WellKnownFolderName.Calendar, new Mailbox(roomMail))),
        EventType.Created,
        EventType.Deleted,
        EventType.Modified);
service.getHttpHeaders().put("X-AnchorMailbox", userMail);
service.setImpersonatedUserId(new ImpersonatedUserId(ConnectingIdType.SmtpAddress, userMail));
StreamingSubscription streamingSubscriptionUser = service.subscribeToStreamingNotifications(Collections.singleton(new FolderId(WellKnownFolderName.Calendar, new Mailbox(userMail))),
        EventType.Created,
        EventType.Deleted,
        EventType.Modified);
service.getHttpHeaders().put("X-AnchorMailbox", roomMail);
service.setImpersonatedUserId(new ImpersonatedUserId(ConnectingIdType.SmtpAddress, roomMail));
StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(service, 30);
connection.addSubscription(streamingSubscriptionRoom);
connection.addSubscription(streamingSubscriptionUser);
connection.addOnNotificationEvent((sender, args) -> {
    L.info("Events" + args);
});
connection.addOnDisconnect((sender, args) -> L.info("Subscript error" + args));
connection.open();
© www.soinside.com 2019 - 2024. All rights reserved.