internal static DcNS.XsdDataContractImporter CreateDCImporter(CommandProcessorOptions options, CodeCompileUnit codeCompileUnit) { DcNS.XsdDataContractImporter importer = new DcNS.XsdDataContractImporter(codeCompileUnit); DcNS.ImportOptions dcOptions = CreateDCImportOptions(options); importer.Options = dcOptions; return(importer); }
static void Generate(string url, TextWriter writer) { var cr = new ContractReference(); cr.Url = url; var protocol = new DiscoveryClientProtocol(); var wc = new WebClient(); using (var stream = wc.OpenRead(cr.Url)) protocol.Documents.Add(cr.Url, cr.ReadDocument(stream)); var mset = ToMetadataSet(protocol); var importer = new WsdlImporter(mset); var xsdImporter = new XsdDataContractImporter(); var options = new ImportOptions(); options.ReferencedCollectionTypes.Add(typeof(LinkedList<>)); xsdImporter.Options = options; importer.State.Add(typeof(XsdDataContractImporter), xsdImporter); Collection<ContractDescription> contracts = importer.ImportAllContracts(); CodeCompileUnit ccu = new CodeCompileUnit(); CodeNamespace cns = new CodeNamespace("TestNamespace"); ccu.Namespaces.Add(cns); var generator = new ServiceContractGenerator(ccu); foreach (var cd in contracts) generator.GenerateServiceContractType(cd); var provider = new CSharpCodeProvider(); provider.GenerateCodeFromCompileUnit(ccu, writer, null); }
public void TestSimpleList () { var options = new ImportOptions (); var ccu = WsdlHelper.Import (collectionsMetadata, options); var method = ccu.FindMethod ("MyServiceClient", "GetSimpleList"); Assert.That (method, Is.Not.Null, "#1"); Assert.That (method.ReturnType, Is.Not.Null, "#2"); Assert.That (method.ReturnType.ArrayRank, Is.EqualTo (1), "#3"); Assert.That (method.ReturnType.BaseType, Is.EqualTo ("System.Int32"), "#4"); }
public void TestSimpleList2 () { var options = new ImportOptions (); options.ReferencedCollectionTypes.Add (typeof(LinkedList<>)); var ccu = WsdlHelper.Import (collectionsMetadata, options); var method = ccu.FindMethod ("MyServiceClient", "GetSimpleList"); Assert.That (method, Is.Not.Null, "#1"); var ret = method.ReturnType; Assert.That (ret, Is.Not.Null, "#2"); Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3"); Assert.That (ret.BaseType, Is.EqualTo ("System.Collections.Generic.LinkedList`1"), "#4"); Assert.That (ret.TypeArguments.Count, Is.EqualTo (1), "#5"); Assert.That (ret.TypeArguments [0].BaseType, Is.EqualTo ("System.Int32"), "#6"); }
public static CodeCompileUnit Import (MetadataSet metadata, ImportOptions options) { var importer = new WsdlImporter (metadata); var xsdImporter = new XsdDataContractImporter (); xsdImporter.Options = options; importer.State.Add (typeof(XsdDataContractImporter), xsdImporter); var contracts = importer.ImportAllContracts (); CodeCompileUnit ccu = new CodeCompileUnit (); var generator = new ServiceContractGenerator (ccu); if (contracts.Count != 1) throw new InvalidOperationException (string.Format ( "Metadata import failed: found {0} contracts.", contracts.Count)); var contract = contracts.First (); generator.GenerateServiceContractType (contract); return ccu; }
/// <summary> /// Builds a new instance. /// </summary> /// <param name="codeGeneratorContext">The code generator context.</param> /// <returns> /// A new <see cref="XsdDataContractImporter"/> instance. /// </returns> public XsdDataContractImporter Build(ICodeGeneratorContext codeGeneratorContext) { CodeGeneratorOptions codeGeneratorOptions = codeGeneratorContext.CodeGeneratorOptions; CodeDomProvider codeDomProvider = codeGeneratorContext.CodeDomProvider; CodeCompileUnit codeCompileUnit = codeGeneratorContext.CodeCompileUnit; ImportOptions importOptions = new ImportOptions { GenerateSerializable = codeGeneratorOptions.SerializableAttribute, GenerateInternal = codeGeneratorOptions.InternalTypes, ImportXmlType = codeGeneratorOptions.ImportXmlTypes, EnableDataBinding = codeGeneratorOptions.EnableDataBinding, CodeProvider = codeDomProvider }; foreach (KeyValuePair<string, string> mapping in codeGeneratorOptions.NamespaceMappings) { importOptions.Namespaces.Add(mapping.Key, mapping.Value); } return new XsdDataContractImporter(codeCompileUnit) {Options = importOptions}; }
private static DcNS.ImportOptions CreateDCImportOptions(CommandProcessorOptions options) { DcNS.ImportOptions dcOptions = new DcNS.ImportOptions { GenerateInternal = options.InternalTypeAccess == true, EnableDataBinding = options.EnableDataBinding == true }; foreach (Type referencedType in options.ReferencedTypes) { dcOptions.ReferencedTypes.Add(referencedType); } foreach (Type referencedCollectionType in options.ReferencedCollectionTypes) { dcOptions.ReferencedCollectionTypes.Add(referencedCollectionType); } foreach (KeyValuePair <string, string> namespaceMapping in options.NamespaceMappings) { Debug.Assert(!dcOptions.Namespaces.ContainsKey(namespaceMapping.Key), $"Key '{namespaceMapping.Key}' already added to dictionary!"); dcOptions.Namespaces[namespaceMapping.Key] = namespaceMapping.Value; } dcOptions.CodeProvider = options.CodeProvider; return(dcOptions); }
/// <summary> /// Create an appropriate XsdDataContractImporter for the generator /// </summary> /// <param name="proxyOptions">Code/config generation options to use</param> /// <param name="targetCompileUnit">CodeCompileUnit into which we will generate the client code</param> /// <param name="codeDomProvider">CodeDomProvider for the language we will use to generate the client</param> /// <param name="proxyNamespace">CLR namespace in which the client code will be generated</param> /// <param name="typeLoader">Service used to resolve type/assembly names (strings) to actual Types and Assemblies</param> /// <param name="targetFrameworkVersion">Targetted Framework version number</param> /// <param name="importErrors">List of errors encountered. New errors will be added to this list</param> /// <returns></returns> protected static XsdDataContractImporter CreateDataContractImporter( ClientOptions proxyOptions, CodeCompileUnit targetCompileUnit, System.CodeDom.Compiler.CodeDomProvider codeDomProvider, string proxyNamespace, IContractGeneratorReferenceTypeLoader typeLoader, int targetFrameworkVersion, IList<ProxyGenerationError> importErrors) { System.Runtime.Serialization.XsdDataContractImporter xsdDataContractImporter = new System.Runtime.Serialization.XsdDataContractImporter(targetCompileUnit); System.Runtime.Serialization.ImportOptions options = new System.Runtime.Serialization.ImportOptions(); options.CodeProvider = codeDomProvider; // We specify that we want to generate all types from all XML namespaces into // our proxy namespace. By default, each XML namespace get's its own CLR namespace options.Namespaces.Add("*", proxyNamespace); options.GenerateInternal = proxyOptions.GenerateInternalTypes; options.GenerateSerializable = proxyOptions.GenerateSerializableTypes; options.EnableDataBinding = proxyOptions.EnableDataBinding; options.ImportXmlType = proxyOptions.ImportXmlTypes; if (typeLoader != null) { IEnumerable<Type> referencedTypes = LoadSharedDataContractTypes(proxyOptions, typeLoader, targetFrameworkVersion, importErrors); if (referencedTypes != null) { foreach (Type sharedType in referencedTypes) { options.ReferencedTypes.Add(sharedType); } } IEnumerable<Type> referencedCollectionTypes = LoadSharedCollectionTypes(proxyOptions, typeLoader, importErrors); if (referencedCollectionTypes != null) { foreach (Type collectionType in referencedCollectionTypes) { options.ReferencedCollectionTypes.Add(collectionType); } } } foreach (NamespaceMapping namespaceMapping in proxyOptions.NamespaceMappingList) { options.Namespaces.Add(namespaceMapping.TargetNamespace, namespaceMapping.ClrNamespace); } xsdDataContractImporter.Options = options; return xsdDataContractImporter; }
internal CodeExporter(DataContractSet dataContractSet, ImportOptions options, CodeCompileUnit codeCompileUnit) { this.dataContractSet = dataContractSet; this.codeCompileUnit = codeCompileUnit; this.AddReferencedAssembly(Assembly.GetExecutingAssembly()); this.options = options; this.namespaces = new Dictionary<string, string>(); this.clrNamespaces = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); foreach (KeyValuePair<XmlQualifiedName, DataContract> pair in dataContractSet) { DataContract dataContract = pair.Value; if (!dataContract.IsBuiltInDataContract && !(dataContract is CollectionDataContract)) { ContractCodeDomInfo contractCodeDomInfo = this.GetContractCodeDomInfo(dataContract); if (contractCodeDomInfo.IsProcessed && !contractCodeDomInfo.UsesWildcardNamespace) { string clrNamespace = contractCodeDomInfo.ClrNamespace; if ((clrNamespace != null) && !this.clrNamespaces.ContainsKey(clrNamespace)) { this.clrNamespaces.Add(clrNamespace, dataContract.StableName.Namespace); this.namespaces.Add(dataContract.StableName.Namespace, clrNamespace); } } } } if (this.options != null) { foreach (KeyValuePair<string, string> pair2 in options.Namespaces) { string str4; string str5; string key = pair2.Key; string str3 = pair2.Value; if (str3 == null) { str3 = string.Empty; } if (this.clrNamespaces.TryGetValue(str3, out str4)) { if (key != str4) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.Runtime.Serialization.SR.GetString("CLRNamespaceMappedMultipleTimes", new object[] { str4, key, str3 }))); } } else { this.clrNamespaces.Add(str3, key); } if (this.namespaces.TryGetValue(key, out str5)) { if (str3 != str5) { this.namespaces.Remove(key); this.namespaces.Add(key, str3); } } else { this.namespaces.Add(key, str3); } } } foreach (CodeNamespace namespace2 in codeCompileUnit.Namespaces) { string str6 = namespace2.Name ?? string.Empty; if (!this.clrNamespaces.ContainsKey(str6)) { this.clrNamespaces.Add(str6, null); } if (str6.Length == 0) { foreach (CodeTypeDeclaration declaration in namespace2.Types) { this.AddGlobalTypeName(declaration.Name); } } } }
internal CodeExporter(DataContractSet dataContractSet, ImportOptions options, CodeCompileUnit codeCompileUnit) { this.dataContractSet = dataContractSet; this.codeCompileUnit = codeCompileUnit; AddReferencedAssembly(Assembly.GetExecutingAssembly()); this.options = options; this.namespaces = new Dictionary<string, string>(); this.clrNamespaces = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); // Update namespace tables for DataContract(s) that are already processed foreach (KeyValuePair<XmlQualifiedName, DataContract> pair in dataContractSet) { DataContract dataContract = pair.Value; if (!(dataContract.IsBuiltInDataContract || dataContract is CollectionDataContract)) { ContractCodeDomInfo contractCodeDomInfo = GetContractCodeDomInfo(dataContract); if (contractCodeDomInfo.IsProcessed && !contractCodeDomInfo.UsesWildcardNamespace) { string clrNamespace = contractCodeDomInfo.ClrNamespace; if (clrNamespace != null && !this.clrNamespaces.ContainsKey(clrNamespace)) { this.clrNamespaces.Add(clrNamespace, dataContract.StableName.Namespace); this.namespaces.Add(dataContract.StableName.Namespace, clrNamespace); } } } } // Copy options.Namespaces to namespace tables if (this.options != null) { foreach (KeyValuePair<string, string> pair in options.Namespaces) { string dataContractNamespace = pair.Key; string clrNamespace = pair.Value; if (clrNamespace == null) clrNamespace = String.Empty; string currentDataContractNamespace; if (this.clrNamespaces.TryGetValue(clrNamespace, out currentDataContractNamespace)) { if (dataContractNamespace != currentDataContractNamespace) throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.CLRNamespaceMappedMultipleTimes, currentDataContractNamespace, dataContractNamespace, clrNamespace))); } else this.clrNamespaces.Add(clrNamespace, dataContractNamespace); string currentClrNamespace; if (this.namespaces.TryGetValue(dataContractNamespace, out currentClrNamespace)) { if (clrNamespace != currentClrNamespace) { this.namespaces.Remove(dataContractNamespace); this.namespaces.Add(dataContractNamespace, clrNamespace); } } else this.namespaces.Add(dataContractNamespace, clrNamespace); } } // Update namespace tables for pre-existing namespaces in CodeCompileUnit foreach (CodeNamespace codeNS in codeCompileUnit.Namespaces) { string ns = codeNS.Name ?? string.Empty; if (!this.clrNamespaces.ContainsKey(ns)) { this.clrNamespaces.Add(ns, null); } if (ns.Length == 0) { foreach (CodeTypeDeclaration codeTypeDecl in codeNS.Types) { AddGlobalTypeName(codeTypeDecl.Name); } } } }
public void TestCustomCollection2 () { var options = new ImportOptions (); var ccu = WsdlHelper.Import (customCollectionsMetadata, options); var method = ccu.FindMethod ("MyServiceClient", "GetCustomCollection2"); Assert.That (method, Is.Not.Null, "#1"); var ret = method.ReturnType; Assert.That (ret, Is.Not.Null, "#2"); Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3"); Assert.That (ret.BaseType, Is.EqualTo ("TestWCF.Model.MyCollectionOfdouble"), "#4"); Assert.That (ret.TypeArguments.Count, Is.EqualTo (0), "#5"); }
public void TestCustomCollection4 () { var options = new ImportOptions (); options.ReferencedCollectionTypes.Add (typeof (LinkedList<>)); var ccu = WsdlHelper.Import (customCollectionsMetadata, options); var type = ccu.FindType ("MyCollection"); Assert.That (type, Is.Not.Null, "#1a"); Assert.That (type.BaseTypes.Count, Is.EqualTo (1), "#2a"); var baseType = type.BaseTypes[0]; Assert.That (baseType.BaseType, Is.EqualTo ("System.Collections.Generic.LinkedList`1"), "#3a"); Assert.That (baseType.TypeArguments.Count, Is.EqualTo (1), "#4a"); Assert.That (baseType.TypeArguments[0].BaseType, Is.EqualTo ("System.String"), "#5a"); var attr = type.FindAttribute ("System.Runtime.Serialization.CollectionDataContractAttribute"); Assert.That (attr, Is.Not.Null, "#6a"); var nameArg = attr.FindArgument ("Name"); Assert.That (nameArg, Is.Not.Null, "#7a"); Assert.That (((CodePrimitiveExpression)nameArg.Value).Value, Is.EqualTo ("MyCollection"), "#8a"); var nsArg = attr.FindArgument ("Namespace"); Assert.That (nsArg, Is.Not.Null, "#9a"); Assert.That (((CodePrimitiveExpression)nsArg.Value).Value, Is.EqualTo ("http://schemas.datacontract.org/2004/07/TestWCF.Model"), "#10a"); var itemArg = attr.FindArgument ("ItemName"); Assert.That (itemArg, Is.Not.Null); Assert.That (((CodePrimitiveExpression)itemArg.Value).Value, Is.EqualTo ("string"), "#11a"); type = ccu.FindType ("MyCollectionOfdouble"); Assert.That (type, Is.Not.Null, "#1b"); Assert.That (type.BaseTypes.Count, Is.EqualTo (1), "#2b"); baseType = type.BaseTypes[0]; Assert.That (baseType.BaseType, Is.EqualTo ("System.Collections.Generic.LinkedList`1"), "#3b"); Assert.That (baseType.TypeArguments.Count, Is.EqualTo (1), "#4b"); Assert.That (baseType.TypeArguments[0].BaseType, Is.EqualTo ("System.Double"), "#5b"); attr = type.FindAttribute ("System.Runtime.Serialization.CollectionDataContractAttribute"); Assert.That (attr, Is.Not.Null, "#6b"); nameArg = attr.FindArgument ("Name"); Assert.That (nameArg, Is.Not.Null, "#7b"); Assert.That (((CodePrimitiveExpression)nameArg.Value).Value, Is.EqualTo ("MyCollectionOfdouble"), "#8b"); nsArg = attr.FindArgument ("Namespace"); Assert.That (nsArg, Is.Not.Null, "#9b"); Assert.That (((CodePrimitiveExpression)nsArg.Value).Value, Is.EqualTo ("http://schemas.datacontract.org/2004/07/TestWCF.Model"), "#10b"); itemArg = attr.FindArgument ("ItemName"); Assert.That (itemArg, Is.Not.Null); Assert.That (((CodePrimitiveExpression)itemArg.Value).Value, Is.EqualTo ("double"), "#11b"); }
public void TestSimpleDictionary2 () { var options = new ImportOptions (); options.ReferencedCollectionTypes.Add (typeof (SortedList<,>)); var ccu = WsdlHelper.Import (collectionsMetadata, options); var method = ccu.FindMethod ("MyServiceClient", "GetSimpleDictionary"); Assert.That (method, Is.Not.Null, "#1"); var ret = method.ReturnType; Assert.That (ret, Is.Not.Null, "#2"); Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3"); Assert.That (ret.BaseType, Is.EqualTo ("System.Collections.Generic.SortedList`2"), "#4"); Assert.That (ret.TypeArguments.Count, Is.EqualTo (2), "#5"); var keyType = ret.TypeArguments [0]; Assert.That (keyType.BaseType, Is.EqualTo ("System.Int32"), "#6"); var valueType = ret.TypeArguments [1]; Assert.That (valueType.BaseType, Is.EqualTo ("System.String"), "#7"); }
private ImportOptions CreateDataContractImportOptions(ContractGenerationOptions options) { ImportOptions importOptions = new ImportOptions(); importOptions.GenerateSerializable = options.GenerateSerializable; importOptions.GenerateInternal = options.GenerateInternalTypes; importOptions.ImportXmlType = options.ImportXmlType; importOptions.EnableDataBinding = options.EnableDataBinding; importOptions.CodeProvider = options.CodeProvider; foreach (Type type in options.ReferencedTypes) { importOptions.ReferencedTypes.Add(type); } foreach (Type type in options.ReferencedCollectionTypes) { importOptions.ReferencedCollectionTypes.Add(type); } foreach (KeyValuePair<string, string> pair in options.NamespaceMappings) { importOptions.Namespaces.Add(pair.Key, pair.Value); } return importOptions; }
void ConfigureImporter (WsdlImporter importer) { var xsdImporter = new XsdDataContractImporter (); var options = new ImportOptions (); var listMapping = refGroup.ClientOptions.CollectionMappings.FirstOrDefault ( m => m.Category == "List"); if (listMapping != null) { var listType = Dialogs.WCFConfigWidget.GetType (listMapping.TypeName); if (listType != null) options.ReferencedCollectionTypes.Add (listType); } var dictMapping = refGroup.ClientOptions.CollectionMappings.FirstOrDefault ( m => m.Category == "Dictionary"); if (dictMapping != null) { var dictType = Dialogs.WCFConfigWidget.GetType (dictMapping.TypeName); if (dictType != null) options.ReferencedCollectionTypes.Add (dictType); } xsdImporter.Options = options; importer.State.Add (typeof (XsdDataContractImporter), xsdImporter); }
private ImportOptions CreateImportOptions(bool importXmlType) { ImportOptions options = new ImportOptions(); options.ImportXmlType = importXmlType; options.Namespaces.Add(ContractGenerationOptions.NamespaceMappingsAllKeyName, this.targetNamespace); return options; }
private static ImportOptions CreateDataContractImportOptions(MetadataImporterSerializerFormatMode formatMode, CodeDomProvider codeDomProvider) { ImportOptions importOptions = new ImportOptions(); importOptions.GenerateSerializable = true; importOptions.GenerateInternal = false; importOptions.ImportXmlType = formatMode == MetadataImporterSerializerFormatMode.DataContractSerializer; importOptions.EnableDataBinding = false; importOptions.CodeProvider = codeDomProvider; return importOptions; }