示例#1
0
        public static void Publish(string subject, MicroserviceMessage message)
        {
            // validate params
            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentNullException(nameof(subject));
            }

            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            // serialize the message
            var serializedMessage = JsonConvert.SerializeObject(message);

            // send the message
            _connection.Publish(subject, Encoding.UTF8.GetBytes(serializedMessage));
        }
示例#2
0
        public static async Task <MicroserviceMessage> RequestAsync(string subject, MicroserviceMessage message)
        {
            // validate params
            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentNullException(nameof(subject));
            }

            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            // serialize the message
            var serializedMessage = JsonConvert.SerializeObject(message);

            // send the message
            var response = await _connection.RequestAsync(subject, Encoding.UTF8.GetBytes(serializedMessage)).ConfigureAwait(false);

            // return the response as a Microservice message
            return(response.Data.ToMicroserviceMessage());
        }
示例#3
0
        public async void HandleMsgWith <T>(object sender, MsgHandlerEventArgs e) where T : IMessageHandler
        {
            // store the replyTo subject
            var replyTo = e.Message.Reply;

            // deserialize the microservice msg that was embedded in the nats msg
            var microserviceMsg             = e.Message.Data.ToMicroserviceMessage();
            MicroserviceMessage responseMsg = null;

            try
            {
                // create a new scope to handle the message in. this will create a new instance of the handler class per message
                using (var scope = _serviceScopeFactory.CreateScope())
                {
                    // create a new instance of the message handler
                    var msgHandler = scope.ServiceProvider.GetService <T>();

                    // handle the message
                    responseMsg = await msgHandler.HandleAsync(microserviceMsg).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                microserviceMsg.ErrorMessage       = ex.GetBaseException().Message;
                microserviceMsg.ResponseStatusCode = 500;
                Log.Error(microserviceMsg.ErrorMessage);
            }
            finally
            {
                // send the NATS message (with the response) back to the calling client
                if (null != responseMsg && !string.IsNullOrWhiteSpace(replyTo))
                {
                    NatsHelper.Publish(replyTo, responseMsg);
                }
            }
        }
示例#4
0
 public static Task <MicroserviceMessage> RequestAsync(MicroserviceMessage message)
 {
     return(RequestAsync(message.Subject, message));
 }
示例#5
0
 public static void Publish(MicroserviceMessage message)
 {
     Publish(message.Subject, message);
 }
        public static byte[] ToBytes(this MicroserviceMessage msg, JsonSerializerSettings serializerSettings)
        {
            var serializedMessage = JsonConvert.SerializeObject(msg, serializerSettings);

            return(Encoding.UTF8.GetBytes(serializedMessage));
        }