private async Task ReceiveIssueCreated(Ideas.Shared.ServiceBus.Message msg, CancellationToken token, Func <IssueCreatedEventArgs, CancellationToken, Task> issueCreatedHandler)
        {
            // TODO: add logging and error handling
            var args = new IssueCreatedEventArgs();

            if (msg.MessageProperties.ContainsKey("Title"))
            {
                args.Title = msg.MessageProperties["Title"] as string;
            }
            if (msg.MessageProperties.ContainsKey("Description"))
            {
                args.Description = msg.MessageProperties["Description"] as string;
            }
            if (msg.MessageProperties.ContainsKey("RemedyStatus"))
            {
                args.RemedyStatus = msg.MessageProperties["RemedyStatus"] as string;
            }
            if (msg.MessageProperties.ContainsKey("ReferenceId"))
            {
                args.ReferenceId = msg.MessageProperties["ReferenceId"] as string;
            }
            if (msg.MessageProperties.ContainsKey("AssigneeEmail"))
            {
                args.AssigneeEmail = msg.MessageProperties["AssigneeEmail"] as string;
            }
            if (msg.MessageProperties.ContainsKey("AssigneeGroup"))
            {
                args.AssigneeGroup = msg.MessageProperties["AssigneeGroup"] as string;
            }
            if (msg.MessageProperties.ContainsKey("RequestorEmail"))
            {
                args.RequestorEmail = msg.MessageProperties["RequestorEmail"] as string;
            }
            if (msg.MessageProperties.ContainsKey("RequestorTelephone"))
            {
                args.RequestorTelephone = msg.MessageProperties["RequestorTelephone"] as string;
            }
            if (msg.MessageProperties.ContainsKey("RequestorGivenName"))
            {
                args.RequestorGivenName = msg.MessageProperties["RequestorGivenName"] as string;
            }
            if (msg.MessageProperties.ContainsKey("RequestorSurnName"))
            {
                args.RequestorSurnName = msg.MessageProperties["RequestorSurnName"] as string;
            }
            if (msg.MessageProperties.ContainsKey("RequestorDisplayName"))
            {
                args.RequestorDisplayName = msg.MessageProperties["RequestorDisplayName"] as string;
            }
            if (msg.MessageProperties.ContainsKey("Urgency"))
            {
                args.Urgency = msg.MessageProperties["Urgency"] as string;
            }
            var createdDate = await GetMessageProperty <DateTime>(msg, propertyName : nameof(IssueCreatedEventArgs.CreatedDate));

            args.CreatedDate = createdDate.Item;

            await issueCreatedHandler(args, token);
        }
        private async Task <bool> EnsureMessageLabel(Ideas.Shared.ServiceBus.Message message, string label)
        {
            if (message.Label == label)
            {
                return(true);
            }
            else
            {
                await _messageReceiver.DeadLetterAsync(message.LockToken, $"Label was unexpected. Expected '{ label }', got '{ message.Label }';");

                return(false);
            }
        }
        protected virtual async Task <GetItemResult <T> > GetMessageProperty <T>(Ideas.Shared.ServiceBus.Message message, string propertyName, bool allowNull = false)
        {
            if (message == null)
            {
                throw new ArgumentNullException("msg");
            }

            var result = new GetItemResult <T>();

            if (!message.MessageProperties.ContainsKey(propertyName))
            {
                string errorMessage = $"{ propertyName } not found in message";
                result.SetMessageDeadLettered(errorMessage);
                await _messageReceiver.DeadLetterAsync(message.LockToken, errorMessage);
            }
            else
            {
                object propertyObj = message.MessageProperties[propertyName];
                if (propertyObj == null)
                {
                    if (!allowNull)
                    {
                        string errorMessage = $"{ propertyName } was null";
                        _logger.Error("{PropertyName} was empty in Service Bus message", propertyName);
                        result.SetMessageDeadLettered(errorMessage);
                        await _messageReceiver.DeadLetterAsync(message.LockToken, errorMessage);
                    }
                    // else return null (or default) and don't dead letter
                }
                else
                {
                    try
                    {
                        result.Item = (T)propertyObj;
                    }
                    catch (Exception)
                    {
                        string errorMessage = $"{ propertyName } was not of type { typeof(T).FullName }";
                        _logger.Error("{PropertyName} had value {Value}, which was not of the expected type '{Type}", propertyName, propertyObj, typeof(T).FullName);
                        result.SetMessageDeadLettered(errorMessage);
                        await _messageReceiver.DeadLetterAsync(message.LockToken, errorMessage);
                    }
                }
            }

            return(result);
        }
Example #4
0
        public void RegisterMessageHandler(Func <Ideas.Shared.ServiceBus.Message, CancellationToken, Task> handler, MessageHandlerOptions messageHandlerOptions)
        {
            _subscriptionClient.RegisterMessageHandler(async(msg, token) =>
            {
                _logger.Debug("Received service bus message {MessageId}: {Label}", msg.MessageId, msg.Label);

                // transofrm msg to Message
                var messageDto = new Ideas.Shared.ServiceBus.Message()
                {
                    Id                = Guid.Parse(msg.MessageId),
                    Label             = msg.Label,
                    MessageProperties = msg.UserProperties,
                    CreatedDateUtc    = msg.SystemProperties.EnqueuedTimeUtc,
                    LockToken         = msg.SystemProperties.LockToken
                };
                await handler(messageDto, token);
            }, messageHandlerOptions);
        }