Microsoft.ServiceModel.Web.HelpPageInvoker.CreateExample C# (CSharp) Method

CreateExample() private method

private CreateExample ( Type type, OperationDescription od, bool generateJson ) : Message
type System.Type
od System.ServiceModel.Description.OperationDescription
generateJson bool
return Message
        Message CreateExample(Type type, OperationDescription od, bool generateJson)
        {
            bool usesXmlSerializer = od.Behaviors.Contains(typeof(XmlSerializerOperationBehavior));
            XmlQualifiedName name;
            XmlSchemaSet schemaSet = new XmlSchemaSet();
            IDictionary<XmlQualifiedName, Type> knownTypes = new Dictionary<XmlQualifiedName, Type>();
            if (usesXmlSerializer)
            {
                XmlReflectionImporter importer = new XmlReflectionImporter();
                XmlTypeMapping typeMapping = importer.ImportTypeMapping(type);
                name = new XmlQualifiedName(typeMapping.ElementName, typeMapping.Namespace);
                XmlSchemas schemas = new XmlSchemas();
                XmlSchemaExporter exporter = new XmlSchemaExporter(schemas);
                exporter.ExportTypeMapping(typeMapping);
                foreach (XmlSchema schema in schemas)
                {
                    schemaSet.Add(schema);
                }
            }
            else
            {
                XsdDataContractExporter exporter = new XsdDataContractExporter();
                List<Type> listTypes = new List<Type>(od.KnownTypes);
                listTypes.Add(type);
                exporter.Export(listTypes);
                if (!exporter.CanExport(type))
                {
                    throw new NotSupportedException(String.Format("Example generation is not supported for type '{0}'", type));
                }
                name = exporter.GetRootElementName(type);
                foreach (Type knownType in od.KnownTypes)
                {
                    XmlQualifiedName knownTypeName = exporter.GetSchemaTypeName(knownType);
                    if (!knownTypes.ContainsKey(knownTypeName))
                    {
                        knownTypes.Add(knownTypeName, knownType);
                    }
                }

                foreach (XmlSchema schema in exporter.Schemas.Schemas())
                {
                    schemaSet.Add(schema);
                }
            }
            schemaSet.Compile();

            XmlWriterSettings settings = new XmlWriterSettings
            {
                CloseOutput = false,
                Indent = true,
            };

            if (generateJson)
            {
                var jsonExample = new XDocument();
                using (XmlWriter writer = XmlWriter.Create(jsonExample.CreateWriter(), settings))
                {
                    HelpExampleGenerator.GenerateJsonSample(schemaSet, name, writer, knownTypes);
                }
                var reader = jsonExample.CreateReader();
                reader.MoveToContent();
                var message = Message.CreateMessage(MessageVersion.None, (string)null, reader);
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
                message.Properties[WebBodyFormatMessageProperty.Name] = new WebBodyFormatMessageProperty(WebContentFormat.Json);
                return message;
            }
            else
            {
                var xmlExample = new XDocument();
                using (XmlWriter writer = XmlWriter.Create(xmlExample.CreateWriter(), settings))
                {
                    HelpExampleGenerator.GenerateXmlSample(schemaSet, name, writer);
                }
                var reader = xmlExample.CreateReader();
                reader.MoveToContent();
                var message = Message.CreateMessage(MessageVersion.None, (string)null, reader);
                message.Properties[WebBodyFormatMessageProperty.Name] = new WebBodyFormatMessageProperty(WebContentFormat.Xml);
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
                return message;
            }
        }