/// <summary> /// Obligator Run for the Azure worker role /// </summary> public override void Run() { Trace.WriteLine("APMWorkerRole run", "Information"); // Retrieve a reference to the messages queue this.msgQueue = new PushMessageQueue(); Type enumType = typeof(MsgHelperLib.Messages.PushMessageType); // Create our connection objects to two services. // Create MPNSConnection to send the message immediately. Retry 3 times before logging error. this.mpnsConnection = new MpnsConnection(WP7BatchingPolicy.Immediately, 3); // Get the devicetype MPNS supports, only WP7 this.mpnsDevType = this.mpnsConnection.SupportedDeviceType; // Get the type of messages it handles, toast, raw, tile and common this.mpnsSupportedMessages = this.mpnsConnection.HandlesMessageTypes(Enum.GetValues(enumType)); // Create APNSConnection for Apple Push notification. Use Sandbox using certs and use 3 retries before logging error this.apnsConnection = new ApnsConnection(true, ApnsP12File, ApnsP12FilePassword, 3); // Get the type of device it support this.apnsDevType = this.apnsConnection.SupportedDeviceType; // Find the types of messages it supports. iPhone and Common types this.apnsSupportedMessages = this.apnsConnection.HandlesMessageTypes(Enum.GetValues(enumType)); // Create APNSConnection for Apple Push notification. Use Sandbox using certs and use 3 retries before logging error this.c2dmConnection = new C2dmConnection(3); // Get the type of device it support this.c2dmDevType = this.c2dmConnection.SupportedDeviceType; // Find the types of messages it supports. iPhone and Common types this.c2dmSupportedMessages = this.c2dmConnection.HandlesMessageTypes(Enum.GetValues(enumType)); // Create a new Azure logger and hook up all events the connections will raise. this.logger = new AzureLogger(); this.apnsConnection.NotificationError += this.logger.ApnsConnectionError; this.apnsConnection.DeviceIdFormatError += this.logger.ApnsConnectionIllegalDeviceId; this.apnsConnection.NotificationFormatError += this.logger.ApnsConnectionNotificationFormatError; this.apnsConnection.NotificationFailed += this.logger.ApnsConnectionNotificationFailed; this.mpnsConnection.NotificationError += this.logger.MpnsConnectionError; this.mpnsConnection.DeviceIdFormatError += this.logger.MpnsConnectionDeviceIdError; this.mpnsConnection.NotificationFormatError += this.logger.MpnsConnectionNotificationError; this.mpnsConnection.NotificationFailed += this.logger.MpnsConnectionNotificationFailed; this.c2dmConnection.NotificationError += this.logger.C2dmConnectionError; this.c2dmConnection.DeviceIdFormatError += this.logger.C2dmConnectionDeviceIdError; this.c2dmConnection.NotificationFormatError += this.logger.C2dmConnectionNotificationError; this.c2dmConnection.NotificationFailed += this.logger.C2dmConnectionNotificationFailed; this.sds = new SubscriptionDataSource(); PushMessage pushMsg; DateTime lastMsgCheckTime = DateTime.Now; DateTime currentTime; // in the worker loop while (true) { // Sleep for the leftover time and dequeue a message and then process it. We sleep for at most "WorkerRoleLoopTime" in every cycle currentTime = DateTime.Now; int millSecForNextCheck = WorkerRoleLoopTime - currentTime.Subtract(lastMsgCheckTime).Milliseconds; Thread.Sleep(millSecForNextCheck > 0 ? millSecForNextCheck : 0); lastMsgCheckTime = DateTime.Now; if ((pushMsg = this.msgQueue.Deque()) != null) { // Process will look at the message type and send it to appropriate connection this.ProcessMessage(pushMsg); Trace.WriteLine("APMWorkerRole Received message", "Information"); } } }