/// <summary>
 /// 写入内容
 /// </summary>
 /// <param name="context">http上下文</param>
 /// <param name="basicReturn">基本返回</param>
 /// <returns>任务</returns>
 protected async Task WriteContent(HttpContext context, BasicReturnInfo basicReturn)
 {
     context.Response.ContentType = "application/json;charset=UTF-8";
     await context.Response.WriteAsync(basicReturn.ToJsonString());
 }
Пример #2
0
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="receiveMessageFun">接收消息回调</param>
        public void Receive(Func <byte[], byte[]> receiveMessageFun)
        {
            channel.QueueDeclare(queue: amqpQueue.Queue.Name, durable: amqpQueue.Persistent, exclusive: false, autoDelete: amqpQueue.Queue.AutoDelQueue, arguments: null);
            if (amqpQueue.Queue.Qos != null)
            {
                channel.BasicQos(0, amqpQueue.Queue.Qos.GetValueOrDefault(), false);
            }

            channel.QueueBind(amqpQueue.Queue.Name, amqpQueue.ExchangeName, amqpQueue.Queue.Name);

            var consumer = new EventingBasicConsumer(channel);

            channel.BasicConsume(queue: amqpQueue.Queue.Name, autoAck: false, consumer: consumer);

            consumer.Received += (model, ea) =>
            {
                // 错误返回信息
                BasicReturnInfo errorReturn = null;

                // 返回给客户端的数据
                byte[] outData = null;

                // 关联ID
                string           correlationId = null;
                IBasicProperties props         = null;
                IBasicProperties replyProps    = null;
                try
                {
                    props      = ea.BasicProperties;
                    replyProps = channel.CreateBasicProperties();
                    replyProps.CorrelationId = props.CorrelationId;
                    correlationId            = props.CorrelationId;

                    byte[] inData = ea.Body.IsEmpty ? null : ea.Body.ToArray();
                    try
                    {
                        outData = receiveMessageFun(inData);
                    }
                    catch (Exception ex)
                    {
                        var busEx = AmqpUtil.BuilderBusinessException(ex, null, amqpQueue, log, ex.Message);
                        ExceptionHandle.Handle(busEx);

                        log.ErrorAsync("RpcServer回调业务处理出现异常", ex, typeof(RabbitRpcServer).Name, correlationId);

                        errorReturn = new BasicReturnInfo();
                        errorReturn.SetFailureMsg("业务处理出现异常", ex.Message);

                        outData = Encoding.UTF8.GetBytes(errorReturn.ToJsonString());
                    }
                }
                catch (Exception ex)
                {
                    log.ErrorAsync("RpcServer接收消息处理出现异常", ex, typeof(RabbitRpcServer).Name, correlationId);

                    errorReturn = new BasicReturnInfo();
                    errorReturn.SetFailureMsg("RpcServer接收消息处理出现异常", ex.Message);

                    outData = Encoding.UTF8.GetBytes(errorReturn.ToJsonString());
                }
                finally
                {
                    if (props != null && replyProps != null)
                    {
                        try
                        {
                            channel.BasicPublish(exchange: amqpQueue.ExchangeName, routingKey: props.ReplyTo, basicProperties: replyProps, body: outData);
                            channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                        }
                        catch (Exception ex)
                        {
                            log.ErrorAsync("RPC服务端回发送给客户端报错,这里忽略", ex, typeof(RabbitRpcServer).Name);
                        }
                    }
                }
            };
        }