Exercise 4: Working with Exchange Push Notifications

Task 1 – Beginning the Exercise

In this task, you will open the project and configure it to run with your accounts.

  1. In the Solution Explorer, right-click the PushNotifications project and select Set as StartUp Project.
  2. Open the PushNotifications project.
  3. In Solution Explorer, open the App.config file.
  4. Change the PrimaryLabUserId value to your primary lab account.
  5. Start a remote desktop session as the secondary lab user.

Task 2 – Creating the Push Notification Listener

In this task, you will create and start a WCF service that listens for push notifications from the Exchange Server.

  1. Navigate to TODO: 5.4.1.
  2. Add the following code after the TODO: 5.4.1 comment to create the WCF service and pass its URI to the subscribe method.

    C#

    var listener = new PushNotificationListener(); SubscribeForPushNotifications(listener.LocalEndpoint);

  3. Navigate to TODO: 5.4.2.
  4. Add the following code after the TODO: 5.4.2 comment. This creates a dynamic Uri for the WCF service. Using a dynamically generated Uri for the push notification listener enables your application to create a unique push notification listener endpoint for every user or subscription.

    C#

    UriBuilder uriBuilder = new UriBuilder { Scheme = Uri.UriSchemeHttp, Host = properties.HostName, Path = Guid.NewGuid().ToString(), Port = 80 };
  5. Remove the comments /* .. */ from around the commented block of code.
  6. Navigate to TODO: 5.4.3.
  7. Add the following code after the TODO: 5.4.3 comment. This makes the push notification listener implement the Microsoft.Exchange.Notifications.INotificationServicePortType service contract, in order to receive and process push notifications from Exchange Server.

    C#

    _serviceHost.AddServiceEndpoint( typeof(INotificationServicePortType), new BasicHttpBinding(BasicHttpSecurityMode.None), string.Empty);

Task 3 – Subscribing for Push Notifications

In this task, you will specify the folders and events to receive push notifications for. You will also specify the URL that the notifications should be sent to.

  1. Navigate to TODO: 5.4.4.
  2. Add the following code after the TODO: 5.4.4 comment. This creates the subscription object that will be returned by the Exchange Service.

    C#

    PushSubscription pushSubscription;

  3. Navigate to TODO: 5.4.5.
  4. Add the following code after the TODO: 5.4.5 comment to create a list of folders for which you would like to get notifications.

    C#

    var folderIds = new List<FolderId>() { WellKnownFolderName.Inbox, WellKnownFolderName.Calendar };

  5. Navigate to TODO: 5.4.6.
  6. Add the following code after the TODO: 5.4.6 comment. This creates the list of events for which you would like to get notifications.

    C#

    var eventTypes = new List<EventType>(); eventTypes.Add(EventType.NewMail); eventTypes.Add(EventType.Deleted); eventTypes.Add(EventType.Moved); eventTypes.Add(EventType.Created); eventTypes.Add(EventType.Modified);

  7. Navigate to TODO: 5.4.7.
  8. Add the following code after the TODO: 5.4.7 comment. This subscribes to the push notifications on the Exchange Server and specifies the URL that the notifications should be sent to, the frequency in minutes which the Exchange Server should contact the endpoint, and the events to subscribe to.

    C#

    pushSubscription = _service.SubscribeToPushNotifications( folderIds, new Uri(listenerEndpoint), 1, null, eventTypes.ToArray());
  9. Remove the comments /* .. */ from around the commented block of code.

Task 4 – Processing Push Notifications

In this task, you will process the push notifications sent by the Exchange Server and notify the server whether or not the subscription should be expired.

  1. Navigate to TODO: 5.4.8.
  2. Add the following code after the TODO: 5.4.8 comment. This iterates through the notification events and outputs the type of event and when it occurred.

    C#

    foreach (BaseNotificationEventType notificationEvent in message.Notification.Items) { var mailEvent = notificationEvent as BaseObjectChangedEventType; Console.WriteLine(string.Format("{0} at {1}", message.Notification.ItemsElementName[ count].ToString(), mailEvent != null ? mailEvent.TimeStamp.ToString( "MM/dd/yyyy h:mm:ss tt") : "")); count++; }

  3. Navigate to TODO: 5.4.9.
  4. Add the following code after the TODO: 5.4.9 comment. This sets the subscription status to OK, so the subscription will be continued by the Exchange Server.

    C#

    response = new SendNotificationResponse( new SendNotificationResultType { SubscriptionStatus = SubscriptionStatusType.OK });

  5. Go to Debug >> Start Without Debugging or [Ctrl]+[F5] to start the application.
  6. The console will display the subscription Id.

  7. Switch to the secondary lab user’s session and open Microsoft Outlook.
  8. Send an email to the primary lab user Id.
  9. Return to the primary lab user’s session.
  10. Verify that the NewMailEvent is recorded in the console.

  11. Open Outlook and navigate to the Calendar.
  12. Create an appointment for later in the day.
  13. Verify that the CreatedEvent is recorded in the console.

  14. Close the application.