public static bool IsValidParameter(Type type, ICustomAttributeProvider attributeProvider, bool allowReferences) { object[] attributes = System.ServiceModel.Description.ServiceReflector.GetCustomAttributes(attributeProvider, typeof(MarshalAsAttribute), true); foreach (MarshalAsAttribute attr in attributes) { UnmanagedType marshalAs = attr.Value; if (marshalAs == UnmanagedType.IDispatch || marshalAs == UnmanagedType.Interface || marshalAs == UnmanagedType.IUnknown) { return allowReferences; } } XsdDataContractExporter exporter = new XsdDataContractExporter(); if (!exporter.CanExport(type)) { return false; } return true; }
public static XmlSchemaSet GetXmlSchemaSet(ICollection<Type> operationTypes) { var exporter = new XsdDataContractExporter(); exporter.Export(operationTypes); exporter.Schemas.Compile(); return exporter.Schemas; }
public override void ExportSchemaType (XsdDataContractExporter exporter) { // .NET also expects a default constructor. var ixs = (IXmlSerializable) Activator.CreateInstance (RuntimeType, true); var xs = ixs.GetSchema (); if (xs != null) exporter.Schemas.Add (xs); }
public void CanExportTest () { XsdDataContractExporter xdce = new XsdDataContractExporter (); Assert.IsTrue (xdce.CanExport (typeof (int)), "#1"); Assert.IsTrue (xdce.CanExport (typeof (dc)), "#2"); //No DataContract/Serializable etc -> changed in 3.5 Assert.IsTrue (xdce.CanExport (this.GetType ()), "#3"); }
static void Main(string[] args) { XsdDataContractExporter xsdexp = new XsdDataContractExporter(); xsdexp.Options = new ExportOptions(); xsdexp.Export(typeof(Employee)); // Write out exported schema to a file using (FileStream fs = new FileStream("sample.xsd", FileMode.Create)) foreach (XmlSchema sch in xsdexp.Schemas.Schemas()) sch.Write(fs); }
public static XmlSchemaSet GetXmlSchemaSet(ICollection<Type> operationTypes) { var exporter = new XsdDataContractExporter(); var types = operationTypes .Where(x => !x.AllAttributes<ExcludeAttribute>() .Any(attr => attr.Feature.HasFlag(Feature.Soap))).ToList(); exporter.Export(types); exporter.Schemas.Compile(); return exporter.Schemas; }
public override void ExportSchemaType(XsdDataContractExporter exporter) { // .NET also expects a default constructor. var ixs = (IXmlSerializable)Activator.CreateInstance(RuntimeType, true); var xs = ixs.GetSchema(); if (xs != null) { exporter.Schemas.Add(xs); } }
public void PrimitiveType () { XsdDataContractExporter xdce = new XsdDataContractExporter (); Assert.AreEqual (1, xdce.Schemas.Count); Assert.IsNull (xdce.GetSchemaType (typeof (int))); Assert.AreEqual (new QName ("int", XmlSchema.Namespace), xdce.GetSchemaTypeName (typeof (int))); xdce.Export (typeof (int)); Assert.IsNull (xdce.GetSchemaType (typeof (int))); Assert.AreEqual (new QName ("int", XmlSchema.Namespace), xdce.GetSchemaTypeName (typeof (int))); }
public void ExistingOptionsNotClobbered() { var exporter = new XsdDataContractExporter() { Options = new ExportOptions() }; var smb = new ServiceMetadataBehavior(); smb.MetadataExporter.State.Add(exporter.GetType(), exporter); host.Description.Behaviors.Add(smb); var exporter2 = host.GetXsdDataContractExporter(); Assert.ReferenceEquals(exporter, exporter2); Assert.ReferenceEquals(exporter.Options, exporter2.Options); }
public void ExportXSDUsingDataContractExporter() { //create schema XsdDataContractExporter _exporter = new XsdDataContractExporter(); Type _ConfigurationDataType = typeof(ConfigurationData); Assert.IsTrue(_exporter.CanExport(_ConfigurationDataType)); _exporter.Export(_ConfigurationDataType); Console.WriteLine("number of schemas: {0}", _exporter.Schemas.Count); Console.WriteLine(); //write out the schema XmlSchemaSet _Schemas = _exporter.Schemas; XmlQualifiedName XmlNameValue = _exporter.GetRootElementName(_ConfigurationDataType); string EmployeeNameSpace = XmlNameValue.Namespace; foreach (XmlSchema _schema in _Schemas.Schemas(EmployeeNameSpace)) _schema.Write(Console.Out); }
public static string GetXsd(Type operationType) { if (operationType == null) return null; var sb = new StringBuilder(); var exporter = new XsdDataContractExporter(); if (exporter.CanExport(operationType)) { exporter.Export(operationType); var mySchemas = exporter.Schemas; var qualifiedName = exporter.GetRootElementName(operationType); if (qualifiedName == null) return null; foreach (XmlSchema schema in mySchemas.Schemas(qualifiedName.Namespace)) { schema.Write(new StringWriter(sb)); } } return sb.ToString(); }
public static bool IsValidParameter(Type type, ICustomAttributeProvider attributeProvider, bool allowReferences) { foreach (MarshalAsAttribute attribute in ServiceReflector.GetCustomAttributes(attributeProvider, typeof(MarshalAsAttribute), true)) { switch (attribute.Value) { case UnmanagedType.IDispatch: case UnmanagedType.Interface: case UnmanagedType.IUnknown: return allowReferences; } } XsdDataContractExporter exporter = new XsdDataContractExporter(); if (!exporter.CanExport(type)) { return false; } return true; }
public void TestRemotingTypeConversions() { XsdDataContractExporter x = new XsdDataContractExporter(); XsdDataContractImporter i = new XsdDataContractImporter(); Type[] ta = new Type[] { typeof(int), typeof(string), typeof(DateTime), typeof(float), typeof(TimeSpan), typeof(Decimal), typeof(bool), typeof(char), typeof(short), typeof(Int16), typeof(long) }; foreach (var t in ta) { Debug.WriteLine(".NET: " + t.Name); var y = x.GetSchemaTypeName(t); Debug.WriteLine(string.Format("XSD: {0} {1}", y.Namespace, y.Name)); var cr = i.GetCodeTypeReference(y); Debug.WriteLine(".NET2 :" + cr.BaseType); } }
public void ExportContract(WsdlExporter exporter, WsdlContractConversionContext context) { XsdDataContractExporter xsdInventoryExporter; object dataContractExporter; if (!exporter.State.TryGetValue(typeof(XsdDataContractExporter), out dataContractExporter)) { xsdInventoryExporter = new XsdDataContractExporter(exporter.GeneratedXmlSchemas); exporter.State.Add(typeof(XsdDataContractExporter), xsdInventoryExporter); } else { xsdInventoryExporter = (XsdDataContractExporter)dataContractExporter; } if (xsdInventoryExporter.Options == null) { xsdInventoryExporter.Options = new ExportOptions(); } xsdInventoryExporter.Options.DataContractSurrogate = this.surrogate; }
public static XsdDataContractExporter GetXsdDataContractExporter(this ServiceHostBase host) { var smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); if (smb != null) { object o; if (!smb.MetadataExporter.State.TryGetValue(typeof(XsdDataContractExporter), out o)) { var wsdlexp = smb.MetadataExporter as WsdlExporter; o = new XsdDataContractExporter(wsdlexp != null ? wsdlexp.GeneratedXmlSchemas : null); smb.MetadataExporter.State.Add(typeof(XsdDataContractExporter), o); } var exp = (XsdDataContractExporter)o; if (exp.Options == null) { exp.Options = new ExportOptions(); } return exp; } return null; }
public void ExportContract(WsdlExporter exporter, WsdlContractConversionContext context) { if (exporter == null) throw new ArgumentNullException("exporter"); object dataContractExporter; XsdDataContractExporter xsdDCExporter; if (!exporter.State.TryGetValue(typeof(XsdDataContractExporter), out dataContractExporter)) { xsdDCExporter = new XsdDataContractExporter(exporter.GeneratedXmlSchemas); exporter.State.Add(typeof(XsdDataContractExporter), xsdDCExporter); } else { xsdDCExporter = (XsdDataContractExporter)dataContractExporter; } if (xsdDCExporter.Options == null) xsdDCExporter.Options = new ExportOptions(); if (xsdDCExporter.Options.DataContractSurrogate == null) xsdDCExporter.Options.DataContractSurrogate = new AllowNonSerializableTypesSurrogate(); }
// If the user schema is generated by WCF, it may contain references to some primitive WCF // types in the following namespaces. We need add those schemas to the schema set by default // so these types can be resolved correctly. // // * http://schemas.microsoft.com/2003/10/Serialization // * http://schemas.microsoft.com/2003/10/Serialization/Arrays // * http://microsoft.com/wsdl/types/ // * http://schemas.datacontract.org/2004/07/System private void AddSchemasForPrimitiveTypes(XmlSchemaSet schemas) { // Add DCS special types XsdDataContractExporter dataContractExporter = new XsdDataContractExporter(schemas); // We want to export Guid, Char, and TimeSpan, however even a single call causes all // schemas to be exported. dataContractExporter.Export(typeof(Guid)); // Export DateTimeOffset, DBNull, array types dataContractExporter.Export(typeof(DateTimeOffset)); dataContractExporter.Export(typeof(DBNull)); dataContractExporter.Export(typeof(bool[])); dataContractExporter.Export(typeof(DateTime[])); dataContractExporter.Export(typeof(decimal[])); dataContractExporter.Export(typeof(double[])); dataContractExporter.Export(typeof(float[])); dataContractExporter.Export(typeof(int[])); dataContractExporter.Export(typeof(long[])); dataContractExporter.Export(typeof(XmlQualifiedName[])); dataContractExporter.Export(typeof(short[])); dataContractExporter.Export(typeof(string[])); dataContractExporter.Export(typeof(uint[])); dataContractExporter.Export(typeof(ulong[])); dataContractExporter.Export(typeof(ushort[])); dataContractExporter.Export(typeof(Char[])); dataContractExporter.Export(typeof(TimeSpan[])); dataContractExporter.Export(typeof(Guid[])); // Arrays of DateTimeOffset and DBNull are not supported // Add XS special types // XmlSchemaExporter takes XmlSchemas so we need that temporarily XmlSchemas xmlSchemas = new XmlSchemas(); XmlReflectionImporter importer = new XmlReflectionImporter(); XmlSchemaExporter xmlExporter = new XmlSchemaExporter(xmlSchemas); xmlExporter.ExportTypeMapping(importer.ImportTypeMapping(typeof(Guid))); xmlExporter.ExportTypeMapping(importer.ImportTypeMapping(typeof(Char))); foreach (XmlSchema schema in xmlSchemas) { schemas.Add(schema); } }
// This is a helper method called after a type has been selected to determine whether the type is valid // for use as a parameter type. This check it too expensive to perform during filtering. public static bool IsValidType(Type type) { bool result = true; if (!IsExemptType(type)) { try { XsdDataContractExporter exporter = new XsdDataContractExporter(); if (!exporter.CanExport(type)) { result = false; } } catch (InvalidDataContractException exception) { DiagnosticUtility.TraceHandledException(exception, TraceEventType.Warning); result = false; } catch (NotImplementedException exception) { // This occurs when a design-time type is involved (for example, as a type parameter), in // which case we don't want to exclude the type from use as parameter type. DiagnosticUtility.TraceHandledException(exception, TraceEventType.Information); } } return result; }
public void Test2 () { XsdDataContractExporter xdce = new XsdDataContractExporter (); xdce.Export (typeof (dc)); XmlSchemaSet set = xdce.Schemas; xdce = new XsdDataContractExporter (set); try { xdce.Export (typeof (dc)); } catch (XmlSchemaException xe) { return; } catch (Exception e) { Assert.Fail ("Expected XmlSchemaException, but got " + e.GetType ().ToString ()); } Assert.Fail ("Expected XmlSchemaException"); }
public void GetSchemaTypeTest () { XsdDataContractExporter xdce = new XsdDataContractExporter (); Assert.IsNull (xdce.GetSchemaType (typeof (dc))); Assert.AreEqual (new QName ("_dc", "http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"), xdce.GetSchemaTypeName (typeof (dc))); }
Message CreateSchema(Type body, bool isXmlSerializerType) { System.Collections.IEnumerable schemas; if (isXmlSerializerType) { XmlReflectionImporter importer = new XmlReflectionImporter(); XmlTypeMapping typeMapping = importer.ImportTypeMapping(body); XmlSchemas s = new XmlSchemas(); XmlSchemaExporter exporter = new XmlSchemaExporter(s); exporter.ExportTypeMapping(typeMapping); schemas = s.GetSchemas(null); } else { XsdDataContractExporter exporter = new XsdDataContractExporter(); exporter.Export(body); schemas = exporter.Schemas.Schemas(); } using (MemoryStream stream = new MemoryStream()) { XmlWriterSettings xws = new XmlWriterSettings() { Indent = true }; using (XmlWriter w = XmlWriter.Create(stream, xws)) { w.WriteStartElement("Schemas"); foreach (XmlSchema schema in schemas) { if (schema.TargetNamespace != "http://www.w3.org/2001/XMLSchema") { schema.Write(w); } } } stream.Seek(0, SeekOrigin.Begin); using (XmlReader reader = XmlReader.Create(stream)) { return Message.CreateMessage(MessageVersion.None, null, XElement.Load(reader, LoadOptions.PreserveWhitespace)); } } }
public abstract void ExportSchemaType(XsdDataContractExporter exporter);
public override void ExportSchemaType(XsdDataContractExporter exporter) { exporter.ExportStandardComplexType(null, this, Members); }
public void GetSchemaTypeName () { var xdce = new XsdDataContractExporter (); // bug #670539 Assert.AreEqual (new XmlQualifiedName ("ArrayOfstring", MSArraysNamespace), xdce.GetSchemaTypeName (typeof (IEnumerable<string>)), "#1"); }
public void Dc3Test2 () { //Check for duplicate dc2 ? XsdDataContractExporter xdce = new XsdDataContractExporter (); xdce.Export (typeof (dc3)); xdce.Export (typeof (dc3)); CheckDcFull (xdce.Schemas); }
public void DcTest () { XsdDataContractExporter xdce = new XsdDataContractExporter (); xdce.Export (typeof (dc)); CheckDcFull (xdce.Schemas); }
public void EnumNoDcTest () { XsdDataContractExporter xdce = new XsdDataContractExporter (); xdce.Export (typeof (EnumNoDc)); CheckEnum (xdce.Schemas, new QName ("EnumNoDc", "http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"), new List<string> (new string [] { "Red", "Green", "Blue" })); }
public void EnumTest () { XsdDataContractExporter xdce = new XsdDataContractExporter (); xdce.Export (typeof (XColors)); CheckEnum (xdce.Schemas, colors_qname, new List<string> (new string [] { "_Red" })); }
public override void ExportSchemaType(XsdDataContractExporter exporter) { exporter.ExportEnumContractType(RuntimeType.GetCustomAttribute <DataContractAttribute> (false), this); }
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; } }
public override void ExportSchemaType(XsdDataContractExporter exporter) { exporter.ExportListContractType(null, this); }
static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(WcfServiceLibrary1.Service1))) { // surrogate definition.. foreach (ServiceEndpoint ep in host.Description.Endpoints) { foreach (OperationDescription op in ep.Contract.Operations) { DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior; if (dataContractBehavior != null) { dataContractBehavior.DataContractSurrogate = new valToTransferAsBytes(); } else { dataContractBehavior = new DataContractSerializerOperationBehavior(op); dataContractBehavior.DataContractSurrogate = new valToTransferAsBytes(); op.Behaviors.Add(dataContractBehavior); } } } // set up exporter with surrogate definition.. WsdlExporter exporter = new WsdlExporter(); object dataContractExporter; XsdDataContractExporter xsdInventoryExporter; if (!exporter.State.TryGetValue(typeof(XsdDataContractExporter), out dataContractExporter)) { xsdInventoryExporter = new XsdDataContractExporter(exporter.GeneratedXmlSchemas); } else xsdInventoryExporter = (XsdDataContractExporter)dataContractExporter; exporter.State.Add(typeof(XsdDataContractExporter), xsdInventoryExporter); if (xsdInventoryExporter.Options == null) xsdInventoryExporter.Options = new ExportOptions(); xsdInventoryExporter.Options.DataContractSurrogate = new valToTransferAsBytes(); // export the endpoints.. ServiceEndpointCollection sec = host.Description.Endpoints; foreach (ServiceEndpoint se in sec) { Console.WriteLine(se.Name); exporter.ExportEndpoint(se); } MetadataSet docs = null; docs = exporter.GetGeneratedMetadata(); host.Description. host.Open(); PrintDescription(host); Console.WriteLine("The Trading Service is available. Press any key to exit."); Console.ReadKey(); host.Close(); } }
public void Ctor1 () { XsdDataContractExporter xdce = new XsdDataContractExporter (); Assert.IsNotNull (xdce.Schemas); }
public override void ExportSchemaType(XsdDataContractExporter exporter) { exporter.ExportStandardComplexType(RuntimeType.GetCustomAttribute <DataContractAttribute> (false), this, Members); }
public override void ExportSchemaType(XsdDataContractExporter exporter) { exporter.ExportDictionaryContractType(a, this, GetGenericDictionaryInterface(RuntimeType)); }
public static bool IsValidParameter(Type type, ICustomAttributeProvider attributeProvider, bool allowReferences, out string typeMismatchDetails) { typeMismatchDetails = type.ToString() + " "; object[] attributes = attributeProvider.GetCustomAttributes(typeof(MarshalAsAttribute), true); foreach (MarshalAsAttribute attr in attributes) { UnmanagedType marshalAs = attr.Value; if (marshalAs == UnmanagedType.IDispatch || marshalAs == UnmanagedType.Interface || marshalAs == UnmanagedType.IUnknown) { if (!allowReferences) typeMismatchDetails += SR.GetString(SR.HasMarshalAsAttributeOfType, marshalAs); return allowReferences; } } XsdDataContractExporter exporter = new XsdDataContractExporter(); if (!exporter.CanExport(type)) { typeMismatchDetails += SR.GetString(SR.CannotBeExportedByDataContractExporter); return false; } return true; }
private void ValidateDataContractType(System.Type type) { if (this.dataContractExporter == null) { this.dataContractExporter = new XsdDataContractExporter(); if ((this.serializerFactory != null) && (this.serializerFactory.DataContractSurrogate != null)) { ExportOptions options = new ExportOptions { DataContractSurrogate = this.serializerFactory.DataContractSurrogate }; this.dataContractExporter.Options = options; } } this.dataContractExporter.GetSchemaTypeName(type); }