示例#1
0
        private static bool GetFormatter(out MediaTypeFormatter formatter, out string acceptHeader)
        {
            Console.WriteLine("\nSelect a formatter: Json {J}, Xml {X}, or exit {Enter}");
            //Console.WriteLine("\nSelect a formatter: Json {J}, Xml {X}, Protobuf {P}, or exit {Enter}");
            string selection = Console.ReadLine().ToUpper();

            if (selection == "J")
            {
                var jsonFormatter = new JsonMediaTypeFormatter();
                jsonFormatter.JsonPreserveReferences();
                formatter    = jsonFormatter;
                acceptHeader = "application/json";
            }
            else if (selection == "X")
            {
                formatter    = new XmlDataContractSerializerFormatter();
                acceptHeader = "application/xml";
            }
            //else if (selection == "P")
            //{
            //    var protoFormatter = new ProtoBufFormatter();
            //    protoFormatter.ProtobufPreserveReferences(typeof(Category).Assembly);
            //    formatter = protoFormatter;
            //    acceptHeader = MediaTypes.Protobuf;
            //}
            else
            {
                formatter    = null;
                acceptHeader = null;
                return(false);
            }
            return(true);
        }
示例#2
0
        private static bool GetFormatter(out MediaTypeFormatter formatter, out string acceptHeader)
        {
            Console.WriteLine("\nSelect a formatter: Json {J}, Xml {X}, or exit {Enter}");
            //Console.WriteLine("\nSelect a formatter: Json {J}, Xml {X}, Protobuf {P}, or exit {Enter}");
            string selection = Console.ReadLine().ToUpper();

            if (selection == "J")
            {
                var jsonFormatter = new JsonMediaTypeFormatter();
                jsonFormatter.JsonPreserveReferences();
                formatter = jsonFormatter;
                acceptHeader = "application/json";
            }
            else if (selection == "X")
            {
                formatter = new XmlDataContractSerializerFormatter();
                acceptHeader = "application/xml";
            }
            //else if (selection == "P")
            //{
            //    var protoFormatter = new ProtoBufFormatter();
            //    protoFormatter.ProtobufPreserveReferences(typeof(Category).Assembly);
            //    formatter = protoFormatter;
            //    acceptHeader = MediaTypes.Protobuf;
            //}
            else
            {
                formatter = null;
                acceptHeader = null;
                return false;
            }
            return true;
        }