Quantcast
Channel: Exchange Server Development forum
Viewing all articles
Browse latest Browse all 7132

Exchange Web Service with multible Streaming Subscription Connection

$
0
0

Hello,
i tried to build a Service who is listening to Events in Exchangefolders for different Users.
I created for each User an Instance of my MailBox Class with his own Connection and Subscription.
If i only create one Instance the Notification works fine and i can get the Item by Item.Bind with the ItemId.
But if i create more Instance of MailBox the Events are coming in but when i try to Bind the Item the Service hangs up. What have i forget?

My Class "MailBox"

using System;
using System.Linq;
using Microsoft.Exchange.WebServices.Data;

namespace EWS
{
    public class EWSMailBox
    {
        public ExchangeService Service
        {
            get;
            private set;
        }

        private StreamingSubscriptionConnection fConnection;

        private bool fKeepConnected = false;

        public EWSMailBox(string aUser, string aPassword)
        {
            this.Service = ExchangeService.ConnectToService(aUser, aPassword);

            this.fConnection = new StreamingSubscriptionConnection(this.Service.Service, 30);

            StreamingSubscription streamingsubscription = this.Service.SubscribeToStreamingNotifications(
                new FolderId[] { WellKnownFolderName.Calendar, WellKnownFolderName.Tasks },
                EventType.Created,
                EventType.Modified,
                EventType.Deleted);

            this.fConnection.AddSubscription(streamingsubscription);

            this.fConnection.OnNotificationEvent +=
                new StreamingSubscriptionConnection.NotificationEventDelegate(OnEvent);

            this.fConnection.OnDisconnect +=
                new StreamingSubscriptionConnection.SubscriptionErrorDelegate(Connection_OnDisconnect);
        }

        public void Subscripe()
        {
            this.fKeepConnected = true;

            this.fConnection.Open();
        }

        public void UnSubscripe()
        {
            this.fKeepConnected = false;

            this.fConnection.Close();
        }

        private void Connection_OnDisconnect(object sender, SubscriptionErrorEventArgs args)
        {
            if (this.fKeepConnected)
                this.fConnection.Open();
        }

        private void OnEvent(object sender, NotificationEventArgs args)
        {
            foreach (ItemEvent itemEvent in args.Events.OfType<ItemEvent>())
            {
                try
                {
                    // the Service stops working at this part if i create more than one Instance of MailBox
                    Item aItem =  Item.Bind(this.Service, itemEvent.ItemId);

                    switch (itemEvent.EventType)
                    {
                        case EventType.Created:
                            break;
                        case EventType.Modified:
                            break;
                        case EventType.Deleted:
                            break;
                    }
                }
                catch (Exception exp)
                { }
                finally
                { }
            }
        }
    }
}

Regards


Viewing all articles
Browse latest Browse all 7132

Trending Articles