EWS API 多个流媒体订阅混淆
EWS API multiple streaming subscriptions confusion
我已经“设法”通过 OnNotificationEvent 委托订阅了多个日历的更改事件,但我不知道如何确定哪个帐户发送了该事件。 OnNotificationEvent => sender returns 最后一个模拟 ID,如果我想读取 args->item.unqueid 的内容,我需要知道谁(哪个 smtp 帐户)读取它。
static void Main(string[] args)
{
var service = EWService();
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "user1@mydomain.com");
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, "user2@mydomain.com");
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);
}
}
}
通常,最好的方法是在创建订阅时跟踪 SubscriptionId,该 SubscriptionId 将返回到您创建它所针对的邮箱。 Exchange only returns SubscriptionId 是 Streaming 请求。另一种解决方法是使用已更改的项目(或文件夹)的 ParentFolderId,然后获取 StoreId,然后您可以从中解码 LegacyExchangeDn 并将其解析为邮箱的 SMTP 地址。但这需要多个 Web 请求,因此您最好在创建订阅时就这样做。
我已经“设法”通过 OnNotificationEvent 委托订阅了多个日历的更改事件,但我不知道如何确定哪个帐户发送了该事件。 OnNotificationEvent => sender returns 最后一个模拟 ID,如果我想读取 args->item.unqueid 的内容,我需要知道谁(哪个 smtp 帐户)读取它。
static void Main(string[] args)
{
var service = EWService();
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "user1@mydomain.com");
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, "user2@mydomain.com");
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);
}
}
}
通常,最好的方法是在创建订阅时跟踪 SubscriptionId,该 SubscriptionId 将返回到您创建它所针对的邮箱。 Exchange only returns SubscriptionId 是 Streaming 请求。另一种解决方法是使用已更改的项目(或文件夹)的 ParentFolderId,然后获取 StoreId,然后您可以从中解码 LegacyExchangeDn 并将其解析为邮箱的 SMTP 地址。但这需要多个 Web 请求,因此您最好在创建订阅时就这样做。