public static TriggerMessageContext create
            (BrokeredMessage brokeredMessage
            , DataModelDbContext serviceDataModel)
        {
            if (brokeredMessage == null)
            {
                throw new ArgumentNullException(nameof(brokeredMessage));
            }
            if (serviceDataModel == null)
            {
                throw new ArgumentNullException(nameof(serviceDataModel));
            }


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

            var message
                = new Message(
                      requestId: Guid.Parse(brokeredMessage.CorrelationId)
                      , messageId: Guid.Parse(brokeredMessage.MessageId)
                      , message: brokeredMessage.GetBody <string>()
                      , messageType: brokeredMessage.ContentType
                      );

            var messageContext
                = new MessageContext(
                      serviceContext: serviceContext
                      , message: message
                      );

            return
                (new TriggerMessageContext(Guid.NewGuid(), messageContext));
        }
예제 #2
0
        public ServiceContext
            (DataModelDbContext serviceDataModel
            , ServiceCommandFactory serviceCommandFactory
            , IIngressQueue dataIngressTopic)
        {
            if (serviceDataModel == null)
            {
                throw new ArgumentNullException(nameof(serviceDataModel));
            }
            if (serviceCommandFactory == null)
            {
                throw new ArgumentNullException(nameof(serviceCommandFactory));
            }
            if (dataIngressTopic == null)
            {
                throw new ArgumentNullException(nameof(dataIngressTopic));
            }


            ServiceDataModel      = serviceDataModel;
            ServiceCommandFactory = serviceCommandFactory;
            DataIngressTopic      = dataIngressTopic;
        }
        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();
            }
        }