/// <summary> /// Generates the <see cref="CodeNamespace"/> based on the provide context. /// </summary> /// <param name="codeGeneratorContext">The code generator context.</param> public CodeNamespace GenerateCode(ICodeGeneratorContext codeGeneratorContext) { CodeGenOptions codeGenOptions = codeGeneratorContext.CodeGenOptions; XmlSchemas xmlSchemas = codeGeneratorContext.XmlSchemas; CodeNamespace codeNamespace = new CodeNamespace(); // Generate DataContracts const CodeGenerationOptions generationOptions = CodeGenerationOptions.GenerateProperties; var exporter = new XmlCodeExporter(codeNamespace); var importer = new XmlSchemaImporter(xmlSchemas, generationOptions, new ImportContext(new CodeIdentifiers(), false)); // TypeName to XmlSchemaType mapping IDictionary <string, XmlSchemaType> typeName2schemaTypeMapping = codeGeneratorContext.TypeName2schemaTypeMapping; IDictionary <XmlQualifiedName, string> elementName2TypeNameMapping = codeGeneratorContext.ElementName2TypeNameMapping; IDictionary <string, string> elementName2TargetNamespaceMapping = codeGeneratorContext.ElementName2TargetNamespaceMapping; foreach (XmlSchema schema in xmlSchemas) { foreach (XmlSchemaElement element in schema.Elements.Values) { XmlTypeMapping typeMapping = importer.ImportTypeMapping(element.QualifiedName); if (element.IsAbstract) { continue; } exporter.ExportTypeMapping(typeMapping); if (typeMapping.XsdTypeName == "anyType") { continue; // ignore no type element } if (string.IsNullOrWhiteSpace(typeMapping.XsdTypeName)) { throw new Exception("Cannot use anonymous type for Request/Response element: " + element.Name + "."); } typeName2schemaTypeMapping[typeMapping.XsdTypeName] = element.ElementSchemaType; elementName2TypeNameMapping[element.QualifiedName] = typeMapping.XsdTypeName; elementName2TargetNamespaceMapping[element.QualifiedName.Name] = schema.TargetNamespace; } foreach (XmlSchemaType complexType in schema.SchemaTypes.Values) { XmlTypeMapping typeMapping = importer.ImportSchemaType(complexType.QualifiedName); if (DataContractGenerator.CouldBeAnArray(complexType)) { continue; } exporter.ExportTypeMapping(typeMapping); typeName2schemaTypeMapping[typeMapping.TypeName] = complexType; } } if (codeNamespace.Types.Count == 0) { throw new Exception("No types were generated."); } // Build type name to code type declaration mapping codeGeneratorContext.CodeTypeMap = DataContractGenerator.BuildCodeTypeMap(codeNamespace); // Decorate data contracts code ICodeExtension codeExtension = new CodeExtension(); codeExtension.Process(codeNamespace, codeGeneratorContext); codeNamespace.ImplementsBaijiSerialization(codeGeneratorContext); // Generate interface code string wsdlFile = codeGeneratorContext.CodeGenOptions.MetadataLocation; InterfaceContract interfaceContract = ServiceDescriptionEngine.GetInterfaceContract(wsdlFile); CodeTypeDeclaration codeType; CodeNamespaceImportCollection imports; this.buildInterfaceCode(codeGeneratorContext, interfaceContract, out codeType, out imports); codeNamespace.Types.Add(codeType); foreach (CodeNamespaceImport @import in imports) { codeNamespace.Imports.Add(@import); } // Import SOA common type namespace before removing code types codeNamespace.Imports.Add(new CodeNamespaceImport(Constants.C_SERVICE_STACK_COMMON_TYPES_NAMESPACE)); // Remove SOA common types since they have already been included in CSerivceStack DLL CodeExtension.RemoveSOACommonTypes(codeNamespace); CodeExtension.RemoveDefaultTypes(codeNamespace); return(codeNamespace); }