private void publishUnpublishedOutcomes()
        {
            Trace.TraceInformation($"{Name} - started publishing unpublished messages");

            var serviceDataModel
                = new DataModelDbContext();

            try {
                var serviceContext
                    = new ServiceContext(
                          serviceDataModel: serviceDataModel
                          , serviceCommandFactory: new IngressGatewayCommandFactory()
                          , dataIngressTopic: new DataIngressTopic()
                          );


                foreach (var message in serviceDataModel.GetUnpublishedMessages())
                {
                    Trace.TraceInformation($"{Name} - [{message.MessageId}] Publishing unpublished messages");

                    var outcomeContext
                        = new MessageContext(serviceContext, message);

                    publishOutcome(outcomeContext);

                    Trace.TraceInformation($"{Name} - [{message.MessageId}] Published unpublished messages");
                }
            } catch (Exception e) {
                Trace.TraceError($"{Name} - Message : {e.Message}, Stack trace : {e.StackTrace}");
                throw;
            } finally {
                serviceDataModel.Dispose();
            }

            Trace.TraceInformation($"{Name} - completed publishing unpublished messages");
        }
        private void handleMessage
            (BrokeredMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }


            Trace.TraceInformation($"{Name} - [{message.MessageId}] Received message");

            var serviceDataModel
                = new DataModelDbContext();

            try {
                var messageContext
                    = IngressGatewayTriggerMessageContextFactory
                      .create(message, serviceDataModel);

                // There may be stale messages in the queue if we had a dirty shutdown
                // as such we check each message first to see if it has been processed.
                //
                // The way the service is written if we have processed a message there
                // will always be a message entry with the same id.
                if (!messageContext
                    .MessageContext
                    .ServiceContext
                    .ServiceDataModel
                    .HasMessage(messageContext.MessageContext.Message.MessageId))
                {
                    Trace.TraceInformation($"{Name} - [{message.MessageId}] Processing message");

                    // Process message performs any state changes and then
                    // writes the state changes, the message, and message
                    // outcome in one transaction.
                    //
                    // Publish outcome post the message outcome to the
                    // dataIngress topic and then set the message to published.
                    //
                    // This along with publishing unpublished messages when
                    // starting the service and the above check to see if
                    // the message has previously been published should
                    // ensure consistency so long as the message bus technology
                    // can detect duplicate messages which Service bus can.
                    // It is needed becasue we have two separate components
                    // ( the database and the Azure Service bus ) and you can
                    // never tell when the service may fail.
                    messageContext
                    .fmap(processMessage)
                    .fmap(publishOutcome)
                    ;
                }

                Trace.TraceInformation($"{Name} - [{message.MessageId}] Completed message");
            } catch (Exception e) {
                Trace.TraceError($"{Name} - [{message.MessageId}] Error - Message: {e.Message}, Stack trace: {e.StackTrace}");
                message.Abandon();
                throw;
            } finally {
                serviceDataModel.Dispose();
            }
        }