public async Task ReceiveMessageFromTopic <T>(Func <T, MessageProcessResponse> onProcess) { MessageHandlerOptions options = new MessageHandlerOptions(e => { Trace.TraceError(e.Exception.Message); return(Task.CompletedTask); }) { AutoComplete = false, MaxAutoRenewDuration = TimeSpan.FromMinutes(1) }; await Task.Run(() => _subscriptionClient.RegisterMessageHandler( async(message, token) => { try { // Get message string data = Encoding.UTF8.GetString(message.Body); T item = JsonConvert.DeserializeObject <T>(data); // Process message MessageProcessResponse result = onProcess(item); switch (result) { case MessageProcessResponse.Complete: await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken); break; case MessageProcessResponse.Abandon: await _subscriptionClient.AbandonAsync(message.SystemProperties.LockToken); break; case MessageProcessResponse.Dead: await _subscriptionClient.DeadLetterAsync(message.SystemProperties.LockToken); break; } } catch (Exception ex) { await _subscriptionClient.DeadLetterAsync(message.SystemProperties.LockToken); Trace.TraceError(ex.Message); } }, options)); }
/// <summary> /// The OnProcessAsync /// </summary> /// <param name="message">The message<see cref="CoreMessage"/></param> /// <returns>The <see cref="MessageProcessResponse"/></returns> public async Task <MessageProcessResponse> OnProcessAsync(CoreMessage message) { try { Guard.AgainstNull(nameof(message.Body), message?.Body); string msgBody = Encoding.UTF8.GetString(message?.Body); MessageProcessResponse messageProcessResponse = MessageProcessResponse.Abandon; Log.DependencyTrackingTelemetryModule(); DcpMessage dcpMessage = JsonConvert.DeserializeObject <DcpMessage>(msgBody); Dictionary <string, Func <IBusinessValidationStrategy> > strategies = getStrategy.GetListOfStrategy(mediator); Domain.Model.BusinessValidationProxy businessValidationProxy = await strategies[dcpMessage.ServiceOutput.Items[0].ItemId.ToUpperInvariant()]() .ExecuteStrategy(dcpMessage.ServiceOutput.Items[0].ItemId, dcpMessage.ServiceOutput.Items[0]); if (businessValidationProxy.Valid) { List <Item> validItems = GetItemCollection(businessValidationProxy.ValidPaths, dcpMessage); if (validItems != null && validItems.Count > 0) { messageProcessResponse = await mediator.Send(new SendServiceOutPutMessageCommand(validItems, message, businessValidationProxy.Valid, businessValidationProxy.ErrorMessages)); } } List <Item> invalidItems = GetItemCollection(businessValidationProxy.InValidPaths, dcpMessage); if (invalidItems != null && invalidItems.Count > 0) { businessValidationProxy.InValid = false; messageProcessResponse = await mediator.Send(new SendServiceOutPutMessageCommand(invalidItems, message, businessValidationProxy.InValid, businessValidationProxy.ErrorMessages)); } return(messageProcessResponse); } catch (Exception ex) { Log.Error(ex.Message); return(MessageProcessResponse.Abandon); } }