/// <summary> /// Parses a download feed message /// </summary> /// <param name="version">The version of the protocol being used</param> /// <param name="operationType">The operation that is being performed</param> /// <param name="messageBody">The ValueSet that contains all of the fields</param> private static ISocialInfoMessage ParseDownloadFeedMessage( UInt32 version, OperationType operationType, ValueSet messageBody) { UInt32 operationId = (UInt32)ParseField(messageBody, MessageBodyPayloadField.OperationId); string ownerRemoteId = ParseField(messageBody, MessageBodyPayloadField.OwnerRemoteId) as string; // This is an optional field string lastFeedItemRemoteId = ParseField( messageBody, MessageBodyPayloadField.LastFeedItemRemoteId, false) as string; DateTimeOffset?lastFeedItemTimestamp = ParseField(messageBody, MessageBodyPayloadField.LastFeedItemTimestamp) as DateTimeOffset?; UInt32 itemCount = (UInt32)ParseField(messageBody, MessageBodyPayloadField.ItemCount); bool isFetchMore = (bool)ParseField(messageBody, MessageBodyPayloadField.IsFetchMore); Type objectType = null; switch (operationType) { case OperationType.DownloadContactFeed: objectType = typeof(DownloadContactFeedMessage); break; case OperationType.DownloadDashboardFeed: objectType = typeof(DownloadDashboardFeedMessage); break; case OperationType.DownloadHomeFeed: objectType = typeof(DownloadHomeFeedMessage); break; default: throw new ArgumentException("Unexpected feed type"); } // Create the message using the type information from above ISocialInfoMessage message = Activator.CreateInstance( objectType, new object[] { version, operationId, ownerRemoteId, lastFeedItemRemoteId, lastFeedItemTimestamp.Value.DateTime, itemCount, isFetchMore }) as ISocialInfoMessage; return(message); }
/// <summary> /// Parses a message and creates the appropriate ISocialMessage type. /// </summary> /// <param name="messageBody"> /// The ValueSet that contains all of the key-value pairs /// </param> /// <returns>An ISocialInfoMessage for the operation</returns> public static ISocialInfoMessage CreateSocialInfoMessage(ValueSet messageBody) { // Get the operation type to determine which type of message to create. UInt32 type = (UInt32)ParseField(messageBody, MessageBodyPayloadField.Type); // Get the version of the message UInt32 version = (UInt32)ParseField(messageBody, MessageBodyPayloadField.Version); // At this point, we have enough information to construct the specific message type. OperationType operationType = (OperationType)(type); ISocialInfoMessage message = null; switch (operationType) { case OperationType.GoodBye: message = new GoodByeMessage(version); break; case OperationType.DownloadContactFeed: case OperationType.DownloadDashboardFeed: case OperationType.DownloadHomeFeed: message = ParseDownloadFeedMessage(version, operationType, messageBody); break; case OperationType.GetNext: message = new GetNextOperationMessage(version); break; case OperationType.OperationResult: message = ParseOperationResultMessage(version, messageBody); break; default: throw new ArgumentException("The operation type is not valid."); } return(message); }
public async void Run(IBackgroundTaskInstance taskInstance) { // Get the task deferral and the app service connection this.mTaskDeferral = taskInstance.GetDeferral(); AppServiceTriggerDetails triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails; this.mAppServiceConnection = triggerDetails.AppServiceConnection; // Continue processing messages until the GoodBye message is sent bool continueProcessing = true; // Before exiting, this method needs to complete the deferral that // was acquired. Catch all exceptions that may be thrown. try { while (continueProcessing) { // Create a next operation message to send to the app GetNextOperationMessage getNextOperationMessage = new GetNextOperationMessage(SocialAppContactVersion); // Send a message to the app AppServiceResponse response = await this.mAppServiceConnection.SendMessageAsync( getNextOperationMessage.Serialize()); if (response == null) { throw new InvalidOperationException("A null response was received."); } // Check the status if (response.Status != AppServiceResponseStatus.Success) { throw new Exception(String.Format( "App service response was unsuccessful. Status = {0}", response.Status)); } // Parse the response to get the correct message type ISocialInfoMessage message = SocialInfoMessageFactory.CreateSocialInfoMessage(response.Message); // Check the version of the message // If the version of the message is not the expected version by this agent, // then there was a protocol change in the People app. You will need to // contact the People app team at Microsoft to get the most up-to-date // protocol definition. if (message.MajorVersion != cSocialAppContactMajorVersion) { throw new InvalidOperationException( "This version of the protocol is not supported."); } // Handle the message based on its type switch (message.OperationType) { case OperationType.DownloadContactFeed: case OperationType.DownloadDashboardFeed: case OperationType.DownloadHomeFeed: // Cast the message to an ISocialInfoDownloadFeedMessage ISocialInfoDownloadFeedMessage downloadFeedMessage = message as ISocialInfoDownloadFeedMessage; // Keep track of the operation ID to report it UInt32 operationId = downloadFeedMessage.OperationId; // Save the error code associated with the operation UInt32 errorCode = 0; try { // Download the feed await downloadFeedMessage.DownloadFeedAsync(); } catch (Exception exception) { errorCode = (UInt32)exception.HResult; } // Create the operation result message OperationResultMessage resultMessage = new OperationResultMessage( SocialAppContactVersion, errorCode, downloadFeedMessage.OperationId); // Send the result back to the app await this.mAppServiceConnection.SendMessageAsync( resultMessage.Serialize()); break; case OperationType.GoodBye: continueProcessing = false; break; default: throw new InvalidOperationException( "The selected operation type is not supported."); } } } catch { // Throw the exception throw; } finally { // Complete the task deferral this.mTaskDeferral.Complete(); this.mTaskDeferral = null; this.mAppServiceConnection = null; } }