Beispiel #1
0
        private void handleReceivedMessage(object sender, BasicDeliverEventArgs e)
        {
            try
            {
                var msgObj = bodySerializer.GetObject <T>(e.Body);
                if (msgObj == null)
                {
                    DebugText?.Invoke("RPCSubscription Callback Failed to parse received message to type " + typeof(T).Name + " using BodySerializer: " + bodySerializer.GetType().Name);
                    return;
                }

                object result = null;

                try
                {
                    result = Callback?.Invoke(msgObj);
                }
                catch (Exception err)
                {
                    DebugText?.Invoke("RPCSubscription Callback Failed to execute Callback -> " + err.ToString());

                    if (!AutoAck)
                    {
                        channel.BasicNack(e.DeliveryTag, false, AutoRequeue);
                    }
                }

                if (result == null)
                {
                    if (!AutoAck)
                    {
                        channel.BasicNack(e.DeliveryTag, false, AutoRequeue);
                    }

                    DebugText?.Invoke("RPCSubscription Callback Returned null");
                    return;
                }

                if (!AutoAck)
                {
                    channel.BasicAck(e.DeliveryTag, false);
                }

                if (e.BasicProperties == null || e.BasicProperties.ReplyTo == null)
                {
                    return;
                }

                var responseProps = channel.CreateBasicProperties();
                responseProps.CorrelationId = e.BasicProperties.CorrelationId;

                channel.BasicPublish(
                    exchange: "",
                    routingKey: e.BasicProperties.ReplyTo,
                    body: bodySerializer.GetBytes(result),
                    basicProperties: responseProps
                    );
            }
            catch (Exception err)
            {
                DebugText.Invoke("RPCSubscription Callback Failed to handle received message -> " + err.ToString());
            }
        }
Beispiel #2
0
        private void handleReceivedMessage(object sender, BasicDeliverEventArgs e)
        {
            try
            {
                var msgObj = bodySerializer.GetObject <T>(e.Body);
                if (msgObj == null)
                {
                    DebugText?.Invoke("RPCSubscription Callback Failed to parse received message to type " + typeof(T).Name + " using BodySerializer: " + bodySerializer.GetType().Name);
                    return;
                }

                try
                {
                    Callback?.Invoke(msgObj);
                    if (!AutoAck)
                    {
                        channel.BasicAck(e.DeliveryTag, false);
                    }
                }
                catch (Exception err)
                {
                    DebugText?.Invoke("RPCSubscription Callback Failed to execute Callback -> " + err.ToString());

                    if (!AutoAck)
                    {
                        channel.BasicNack(e.DeliveryTag, false, AutoRequeue);
                    }
                }
            }
            catch (Exception err)
            {
                DebugText.Invoke("RPCSubscription Callback Failed to handle received message -> " + err.ToString());
            }
        }