Example #1
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);
        }
Example #2
0
        private byte[] SerializeResponse(string contentType, string encodingType, RabbitRes response)
        {
            string str = null;

            switch (contentType.ToLower())
            {
            case "application/json":
                str = JsonConvert.SerializeObject(response);
                break;

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

            default:
                str = JsonConvert.SerializeObject(response);
                break;
            }
            if (null != str)
            {
                return(Encoding.GetEncoding(encodingType).GetBytes(str));
            }
            return(null);
        }