Пример #1
0
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory()
            {
                Uri = "amqp://*****:*****@192.168.1.25:5672/test"
            };

            using (IConnection conn = factory.CreateConnection())
            {
                using (IModel channel = conn.CreateModel())
                {
                    string input = string.Empty;
                    while ("exit" != (input = Console.ReadLine()))
                    {
                        RabbitReq request = new RabbitReq();
                        request.Data.Add("column1", input);
                        request.Data.Add("column2", "test str 2");
                        request.Data.Add("column3", new { Account = "xingchao", Password = "******" });
                        var bodyBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request));

                        IBasicProperties prop = new BasicProperties();
                        prop.ContentType     = "application/json";
                        prop.ContentEncoding = "UTF-8";

                        channel.BasicPublish("publish", "", prop, bodyBytes);
                        channel.ConfirmSelect();
                        var r = channel.WaitForConfirms();
                        Console.WriteLine(r ? "发布成功" : "发布失败");
                    }
                }
            }
        }
Пример #2
0
        private RabbitReq DeserializeRequest(string contentType, string body)
        {
            RabbitReq request = null;

            switch (contentType.ToLower())
            {
            case "application/json":
                request = JsonConvert.DeserializeObject <RabbitReq>(body);
                break;

            case "text/xml":
                // todo
                break;

            default:
                request = JsonConvert.DeserializeObject <RabbitReq>(body);
                break;
            }
            return(request);
        }
Пример #3
0
        public override byte[] HandleSimpleCall(bool isRedelivered, IBasicProperties requestProperties, byte[] body, out IBasicProperties replyProperties)
        {
            replyProperties           = requestProperties;
            replyProperties.MessageId = Guid.NewGuid().ToString().ToUpper();

            string bodyStr = Encoding.GetEncoding(requestProperties.ContentEncoding).GetString(body);

            Console.WriteLine(bodyStr);
            RabbitReq request = DeserializeRequest(requestProperties.ContentType, bodyStr);

            if (null != request)
            {
                RabbitRes response = null;
                switch (request.FunctionName)
                {
                case "Login":
                    // do something
                    var data = new Dictionary <string, object>();
                    data.Add("result", $"{request.Parameters["Account"]} {request.Parameters["Password"]} done.");
                    response = new RabbitRes()
                    {
                        Code        = 200,
                        Description = "Success",
                        Data        = data
                    };
                    break;

                default:
                    break;
                }
                if (null != response)
                {
                    return(SerializeResponse(requestProperties.ContentType, requestProperties.ContentEncoding, response));
                }
            }
            return(null);
        }
Пример #4
0
        static void Main(string[] args)
        {
            string input   = "";
            var    factory = new ConnectionFactory()
            {
                Uri = "amqp://*****:*****@192.168.1.25:5672/test"
            };

            // 心跳超时,默认60秒
            factory.RequestedHeartbeat = 60;
            using (IConnection conn = factory.CreateConnection())
            {
                using (IModel channel = conn.CreateModel())
                {
                    while ("exit" != (input = Console.ReadLine()))
                    {
                        var msgBody = new RabbitReq();
                        msgBody.FunctionName = "Login";
                        msgBody.Parameters.Add("Account", "xingchao");
                        msgBody.Parameters.Add("Password", "123456");
                        var msgBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(msgBody));
                        var prop     = new BasicProperties();
                        prop.ContentType     = "application/json";
                        prop.ContentEncoding = "UTF-8";

                        //var client = new SimpleRpcClient(channel, "ExchangeName", ExchangeType.Direct, "RoutingKey");
                        var client = new SimpleRpcClient(channel, "QueueName");
                        IBasicProperties replyProp = new BasicProperties();
                        var replyMsgBytes          = client.Call(prop, msgBytes, out replyProp);

                        var response = JsonConvert.DeserializeObject <RabbitRes>(Encoding.UTF8.GetString(replyMsgBytes));

                        Console.WriteLine(JsonConvert.SerializeObject(response));
                    }
                }
            }
        }