摘要
核心代码块
<span style="color: #0000ff;">namespace<span style="color: #000000;"> Exchange101
{
<span style="color: #008000;">//<span style="color: #008000;"> This sample is for demonstration purposes only. Before you run this sample,make sure that the code meets the coding requirements of your organization.
<span style="color: #0000ff;">class<span style="color: #000000;"> Notifications
{
<span style="color: #0000ff;">static ExchangeService service = Service.ConnectToService(UserDataFromConsole.GetUserData(),<span style="color: #0000ff;">new<span style="color: #000000;"> TraceListener());
<span style="color: #0000ff;">private <span style="color: #0000ff;">static<span style="color: #000000;"> AutoResetEvent Signal;
Console.WriteLine(<span style="color: #800000;">"<span style="color: #800000;">Do you want to reconnect to the subscription? Y/N<span style="color: #800000;">"<span style="color: #000000;">);
<span style="color: #0000ff;">while (<span style="color: #0000ff;">true<span style="color: #000000;">)
{
cki = Console.ReadKey(<span style="color: #0000ff;">true<span style="color: #000000;">);
{
<span style="color: #0000ff;">if (cki.Key ==<span style="color: #000000;"> ConsoleKey.Y)
{
connection.Open();
Console.WriteLine(<span style="color: #800000;">"<span style="color: #800000;">Connection open.<span style="color: #800000;">"<span style="color: #000000;">);
Console.WriteLine(<span style="color: #800000;">"<span style="color: #800000;">\r\n<span style="color: #800000;">"<span style="color: #000000;">);
<span style="color: #0000ff;">break<span style="color: #000000;">;
}
<span style="color: #0000ff;">else <span style="color: #0000ff;">if (cki.Key ==<span style="color: #000000;"> ConsoleKey.N)
{
Signal.Set();
<span style="color: #0000ff;">bool isOpen =<span style="color: #000000;"> connection.IsOpen;
{
<span style="color: #008000;">//<span style="color: #008000;"> This sample is for demonstration purposes only. Before you run this sample,make sure that the code meets the coding requirements of your organization.
<span style="color: #0000ff;">class<span style="color: #000000;"> Notifications
{
<span style="color: #0000ff;">static ExchangeService service = Service.ConnectToService(UserDataFromConsole.GetUserData(),<span style="color: #0000ff;">new<span style="color: #000000;"> TraceListener());
<span style="color: #0000ff;">private <span style="color: #0000ff;">static<span style="color: #000000;"> AutoResetEvent Signal;
</span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
{
SetStreamingNotifications(service);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Wait for the application to exit</span>
Signal = <span style="color: #0000ff;">new</span> AutoResetEvent(<span style="color: #0000ff;">false</span><span style="color: #000000;">);
Signal.WaitOne();
}
</span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> SetStreamingNotifications(ExchangeService service)
{
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Subscribe to streaming notifications on the In<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a> folder,and listen
</span><span style="color: #008000;">//</span><span style="color: #008000;"> for "NewMail","Created",and "Deleted" events. </span>
StreamingSubscription streamingsubscription =<span style="color: #000000;"> service.SubscribeToStreamingNotifications(
</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> FolderId[] { WellKnownFolderName.In<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a> },EventType.NewMail,EventType.Created,EventType.Deleted);
StreamingSubscriptionConnection connection </span>= <span style="color: #0000ff;">new</span> StreamingSubscriptionConnection(service,<span style="color: #800080;">1</span><span style="color: #000000;">);
connection.AddSubscription(streamingsubscription);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Delegate event handlers. </span>
connection.OnNotificationEvent +=
<span style="color: #0000ff;">new</span><span style="color: #000000;"> StreamingSubscriptionConnection.NotificationEventDelegate(OnEvent);
connection.OnSubscriptionError </span>+=
<span style="color: #0000ff;">new</span><span style="color: #000000;"> StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnError);
connection.OnDisconnect </span>+=
<span style="color: #0000ff;">new</span><span style="color: #000000;"> StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnDisconnect);
connection.Open();
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">--------- StreamSubscription event -------</span><span style="color: #800000;">"</span><span style="color: #000000;">);
}
</span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> OnDisconnect(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender,SubscriptionErrorEventArgs args)
{
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Cast the sender as a StreamingSubscriptionConnection object. </span>
StreamingSubscriptionConnection connection =<span style="color: #000000;"> (StreamingSubscriptionConnection)sender;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Ask the user if they want to reconnect or close the subscription. </span>
<span style="color: #000000;"> ConsoleKeyInfo cki;
Console.WriteLine(
Console.WriteLine(<span style="color: #800000;">"<span style="color: #800000;">Do you want to reconnect to the subscription? Y/N<span style="color: #800000;">"<span style="color: #000000;">);
<span style="color: #0000ff;">while (<span style="color: #0000ff;">true<span style="color: #000000;">)
{
cki = Console.ReadKey(<span style="color: #0000ff;">true<span style="color: #000000;">);
{
<span style="color: #0000ff;">if (cki.Key ==<span style="color: #000000;"> ConsoleKey.Y)
{
connection.Open();
Console.WriteLine(<span style="color: #800000;">"<span style="color: #800000;">Connection open.<span style="color: #800000;">"<span style="color: #000000;">);
Console.WriteLine(<span style="color: #800000;">"<span style="color: #800000;">\r\n<span style="color: #800000;">"<span style="color: #000000;">);
<span style="color: #0000ff;">break<span style="color: #000000;">;
}
<span style="color: #0000ff;">else <span style="color: #0000ff;">if (cki.Key ==<span style="color: #000000;"> ConsoleKey.N)
{
Signal.Set();
<span style="color: #0000ff;">bool isOpen =<span style="color: #000000;"> connection.IsOpen;
</span><span style="color: #0000ff;">if</span> (isOpen == <span style="color: #0000ff;">true</span><span style="color: #000000;">)
{
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Close the connection</span>
<span style="color: #000000;"> connection.Close();
}
<span style="color: #0000ff;">else<span style="color: #000000;">
{
<span style="color: #0000ff;">break<span style="color: #000000;">;
}
}
}
}
}
</span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> OnEvent(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender,NotificationEventArgs args)
{
StreamingSubscription subscription </span>=<span style="color: #000000;"> args.Subscription;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Loop through all item-related events. </span>
<span style="color: #0000ff;">foreach</span> (NotificationEvent notification <span style="color: #0000ff;">in</span><span style="color: #000000;"> args.Events)
{
</span><span style="color: #0000ff;">switch</span><span style="color: #000000;"> (notification.EventType)
{
</span><span style="color: #0000ff;">case</span><span style="color: #000000;"> EventType.NewMail:
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">\n-------------Mail created:-------------</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">case</span><span style="color: #000000;"> EventType.Created:
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">\n-------------Item or folder created:-------------</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">case</span><span style="color: #000000;"> EventType.Deleted:
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">\n-------------Item or folder deleted:-------------</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
}
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Display the notification identifier. </span>
<span style="color: #0000ff;">if</span> (notification <span style="color: #0000ff;">is</span><span style="color: #000000;"> ItemEvent)
{
</span><span style="color: #008000;">//</span><span style="color: #008000;"> The NotificationEvent for an email message is an ItemEvent. </span>
ItemEvent itemEvent =<span style="color: #000000;"> (ItemEvent)notification;
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">\nItemId: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> itemEvent.ItemId.UniqueId);
}
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">
{
</span><span style="color: #008000;">//</span><span style="color: #008000;"> The NotificationEvent for a folder is a FolderEvent. </span>
FolderEvent folderEvent =<span style="color: #000000;"> (FolderEvent)notification;
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">\nFolderId: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> folderEvent.FolderId.UniqueId);
}
}
}
</span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> OnError(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender,SubscriptionErrorEventArgs args)
{
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Handle error conditions. </span>
Exception e =<span style="color: #000000;"> args.Exception;
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">\n-------------Error ---</span><span style="color: #800000;">"</span> + e.Message + <span style="color: #800000;">"</span><span style="color: #800000;">-------------</span><span style="color: #800000;">"</span><span style="color: #000000;">);
}
}
}