Beispiel #1
0
        /// <summary>
        /// Creates and adds an XmlSerializerImportOptions instance to be used with WsdlImporter.
        /// </summary>
        private void AddXmlSerializerImportOptions()
        {
            XmlSerializerImportOptions xsio = new XmlSerializerImportOptions(compileUnit);
            xsio.CodeProvider = codeProvider;
            // xsio.ClrNamespace = options.ClrNamespace;

            if (options.EnableDataBinding)
            {
                xsio.WebReferenceOptions.CodeGenerationOptions |= System.Xml.Serialization.CodeGenerationOptions.EnableDataBinding;
            }

            if (!options.GenerateProperties)
            {
                xsio.WebReferenceOptions.CodeGenerationOptions &= ~System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;
            }

            if (!options.GenerateOrderIdentifiers)
            {
                xsio.WebReferenceOptions.CodeGenerationOptions &= ~System.Xml.Serialization.CodeGenerationOptions.GenerateOrder;
            }

            xsio.WebReferenceOptions.SchemaImporterExtensions.Add(typeof(TypedDataSetSchemaImporterExtensionFx35).AssemblyQualifiedName);
            xsio.WebReferenceOptions.SchemaImporterExtensions.Add(typeof(DataSetSchemaImporterExtension).AssemblyQualifiedName);

            wsdlImporter.State.Add(typeof(XmlSerializerImportOptions), xsio);
        }
 internal XmlSerializerOperationGenerator(XmlSerializerImportOptions options)
 {
     operationGenerator = new OperationGenerator();
     this.options = options;
     this.codeNamespace = GetTargetCodeNamespace(options);
     partInfoTable = new Dictionary<MessagePartDescription, PartInfo>();
 }
 /// <summary>
 /// Configures the XmlSerializer importer.
 /// </summary>
 /// <param name="importer">The WSDL importer.</param>
 /// <param name="codeCompileUnit">The target code compile unit.</param>
 /// <param name="codeDomProvider">The code dom provider.</param>
 /// <param name="importNamespace">The target CLR namespace.</param>
 private static void AddStateForXmlSerializerImport(
     WsdlImporter importer, CodeCompileUnit codeCompileUnit, CodeDomProvider codeDomProvider, string importNamespace)
 {
     var importOptions = new XmlSerializerImportOptions(codeCompileUnit)
                             {
                                 CodeProvider = codeDomProvider,
                                 WebReferenceOptions =
                                     {
                                         CodeGenerationOptions =
                                             CodeGenerationOptions.GenerateOrder
                                     },
                                 ClrNamespace = importNamespace
                             };
     importOptions.WebReferenceOptions.SchemaImporterExtensions.Add(typeof(DataSetSchemaImporterExtension).AssemblyQualifiedName);
     importOptions.WebReferenceOptions.SchemaImporterExtensions.Add(typeof(TypedDataSetSchemaImporterExtension).AssemblyQualifiedName);
     importer.State.Add(typeof(XmlSerializerImportOptions), importOptions);
 }
 static CodeNamespace GetTargetCodeNamespace(XmlSerializerImportOptions options)
 {
     CodeNamespace targetCodeNamespace = null;
     string clrNamespace = options.ClrNamespace ?? string.Empty;
     foreach (CodeNamespace ns in options.CodeCompileUnit.Namespaces)
     {
         if (ns.Name == clrNamespace)
         {
             targetCodeNamespace = ns;
         }
     }
     if (targetCodeNamespace == null)
     {
         targetCodeNamespace = new CodeNamespace(clrNamespace);
         options.CodeCompileUnit.Namespaces.Add(targetCodeNamespace);
     }
     return targetCodeNamespace;
 }
		/// <summary>
		/// Builds a new instance.
		/// </summary>
		/// <param name="codeGeneratorContext">The code generator context.</param>
		/// <returns>
		/// A new <see cref="XmlSerializerImportOptions"/> instance.
		/// </returns>
		public XmlSerializerImportOptions Build(ICodeGeneratorContext codeGeneratorContext)
		{
			CodeGeneratorOptions codeGeneratorOptions = codeGeneratorContext.CodeGeneratorOptions;
			CodeDomProvider codeDomProvider = codeGeneratorContext.CodeDomProvider;
			CodeCompileUnit codeCompileUnit = codeGeneratorContext.CodeCompileUnit;

			WebReferenceOptions webReferenceOptions = new WebReferenceOptions
			{
				CodeGenerationOptions = CodeGenerationOptions.None
			};

			if (codeGeneratorOptions.OrderMembers)
			{
				webReferenceOptions.CodeGenerationOptions |= CodeGenerationOptions.GenerateOrder;
			}
			if (codeGeneratorOptions.GenerateProperties)
			{
				webReferenceOptions.CodeGenerationOptions |= CodeGenerationOptions.GenerateProperties;
			}
			if (codeGeneratorOptions.EnableDataBinding)
			{
				webReferenceOptions.CodeGenerationOptions |= CodeGenerationOptions.EnableDataBinding;
			}

			string typedDataSetExtension = (codeGeneratorOptions.TargetFrameworkVersion == TargetFrameworkVersion.Version35)
				? typeof(TypedDataSetSchemaImporterExtensionFx35).AssemblyQualifiedName
				: typeof(TypedDataSetSchemaImporterExtension).AssemblyQualifiedName;
			webReferenceOptions.SchemaImporterExtensions.Add(typedDataSetExtension);
			webReferenceOptions.SchemaImporterExtensions.Add(typeof(DataSetSchemaImporterExtension).AssemblyQualifiedName);

			XmlSerializerImportOptions xmlSerializerImportOptions = new XmlSerializerImportOptions(codeCompileUnit)
			{
				WebReferenceOptions = webReferenceOptions,
				CodeProvider = codeDomProvider
			};

			//TODO:Alex:Prevent the need to keep all types in the same namespaces for certain code decorators.
			string namespaceMapping;
			codeGeneratorOptions.NamespaceMappings.TryGetValue("*", out namespaceMapping);
			xmlSerializerImportOptions.ClrNamespace = namespaceMapping;

			return xmlSerializerImportOptions;
		}
        /// <summary>
        /// Create appropriate XmlSerializerImportOptions for the generator
        /// </summary>
        /// <param name="proxyOptions">Options for the code/config generation</param>
        /// <param name="targetCompileUnit">Compile unit we are going to generate the client code in</param>
        /// <param name="codeDomProvider">CodeDom provider for the language we are using</param>
        /// <param name="proxyNamespace">CLR namespace we'll put the client code in</param>
        /// <returns></returns>
        protected static XmlSerializerImportOptions CreateXmlSerializerImportOptions(
                                ClientOptions proxyOptions,
                                CodeCompileUnit targetCompileUnit,
                                System.CodeDom.Compiler.CodeDomProvider codeDomProvider,
                                string proxyNamespace,
                                System.Type typedDataSetSchemaImporterExtension)
        {
            System.ServiceModel.Channels.XmlSerializerImportOptions xmlSerializerOptions = new XmlSerializerImportOptions(targetCompileUnit);
            System.Web.Services.Description.WebReferenceOptions webReferenceOptions = new System.Web.Services.Description.WebReferenceOptions();

            webReferenceOptions.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties | System.Xml.Serialization.CodeGenerationOptions.GenerateOrder;

            if (proxyOptions.EnableDataBinding)
            {
                webReferenceOptions.CodeGenerationOptions |= System.Xml.Serialization.CodeGenerationOptions.EnableDataBinding;
            }

            webReferenceOptions.SchemaImporterExtensions.Add(typedDataSetSchemaImporterExtension.AssemblyQualifiedName);
            webReferenceOptions.SchemaImporterExtensions.Add(typeof(System.Data.DataSetSchemaImporterExtension).AssemblyQualifiedName);

            /* 

















*/

            xmlSerializerOptions.WebReferenceOptions = webReferenceOptions;
            xmlSerializerOptions.CodeProvider = codeDomProvider;

            xmlSerializerOptions.ClrNamespace = proxyNamespace;

            return xmlSerializerOptions;
        }
        private void AddStateForXmlSerializerImport(ContractGenerationOptions options,
            WsdlImporter wsdlImporter, CodeCompileUnit codeCompileUnit)
        {
            XmlSerializerImportOptions importOptions = new XmlSerializerImportOptions(codeCompileUnit);
            importOptions.WebReferenceOptions = new WebReferenceOptions();
            importOptions.WebReferenceOptions.CodeGenerationOptions = CodeGenerationOptions.GenerateOrder |
                                                                      CodeGenerationOptions.GenerateProperties;
            if (options.EnableDataBinding)
            {
                importOptions.WebReferenceOptions.CodeGenerationOptions |= CodeGenerationOptions.EnableDataBinding;
            }
            if (options.GenerateAsyncMethods)
            {
                importOptions.WebReferenceOptions.CodeGenerationOptions |= CodeGenerationOptions.GenerateNewAsync;
            }

            importOptions.WebReferenceOptions.SchemaImporterExtensions.Add(typeof(TypedDataSetSchemaImporterExtension).AssemblyQualifiedName);
            importOptions.WebReferenceOptions.SchemaImporterExtensions.Add(typeof(DataSetSchemaImporterExtension).AssemblyQualifiedName);
            importOptions.CodeProvider = options.CodeProvider;

            string clrNamespace = null;
            options.NamespaceMappings.TryGetValue(ContractGenerationOptions.NamespaceMappingsAllKeyName, out clrNamespace);
            importOptions.ClrNamespace = clrNamespace;

            importOptions.WebReferenceOptions.Verbose = true;
            //importOptions.WebReferenceOptions.Style = ServiceDescriptionImportStyle.Client;

            wsdlImporter.State.Add(typeof(XmlSerializerImportOptions), importOptions);
        }
        void AddStateForXmlSerializerImport(WsdlImporter importer)
        {
            XmlSerializerImportOptions importOptions =
                new XmlSerializerImportOptions(this.codeCompileUnit);
            importOptions.CodeProvider = this.codeDomProvider;

            importOptions.WebReferenceOptions = new WsdlNS.WebReferenceOptions();
            importOptions.WebReferenceOptions.CodeGenerationOptions =
                CodeGenerationOptions.GenerateProperties |
                CodeGenerationOptions.GenerateOrder;

            importOptions.WebReferenceOptions.SchemaImporterExtensions.Add(
                typeof(TypedDataSetSchemaImporterExtension).AssemblyQualifiedName);
            importOptions.WebReferenceOptions.SchemaImporterExtensions.Add(
                typeof(DataSetSchemaImporterExtension).AssemblyQualifiedName);

            importer.State.Add(typeof(XmlSerializerImportOptions), importOptions);
        }
            public XmlSerializerSchemaImporter(WsdlImporter importer)
                : base(importer)
            {

                XmlSerializerImportOptions options;
                if (importer.State.ContainsKey(typeof(XmlSerializerImportOptions)))
                {
                    options = (XmlSerializerImportOptions)importer.State[typeof(XmlSerializerImportOptions)];
                }
                else
                {
                    object compileUnit;
                    if (!importer.State.TryGetValue(typeof(CodeCompileUnit), out compileUnit))
                    {
                        compileUnit = new CodeCompileUnit();
                        importer.State.Add(typeof(CodeCompileUnit), compileUnit);
                    }
                    options = new XmlSerializerImportOptions((CodeCompileUnit)compileUnit);
                    importer.State.Add(typeof(XmlSerializerImportOptions), options);
                }
                WsdlNS.WebReferenceOptions webReferenceOptions = options.WebReferenceOptions;
                codeProvider = options.CodeProvider;

                encodedSchemas = new XmlSchemas();
                literalSchemas = new XmlSchemas();
                CollectEncodedAndLiteralSchemas(importer.WsdlDocuments, encodedSchemas, literalSchemas, schemaSet);

                CodeIdentifiers codeIdentifiers = new CodeIdentifiers();

                //SchemaImporter.ctor is not thread safe: MB49115, VSWhidbey580396
                lock (schemaImporterLock)
                {
                    xmlImporter = new XmlSchemaImporter(literalSchemas, webReferenceOptions.CodeGenerationOptions, options.CodeProvider, new ImportContext(codeIdentifiers, false));
                }

                if (webReferenceOptions != null)
                {
                    foreach (string extTypeName in webReferenceOptions.SchemaImporterExtensions)
                    {
                        xmlImporter.Extensions.Add(extTypeName, Type.GetType(extTypeName, true /*throwOnError*/));
                    }
                }
                //SchemaImporter.ctor is not thread safe: MB49115, VSWhidbey580396
                lock (schemaImporterLock)
                {
                    soapImporter = new SoapSchemaImporter(encodedSchemas, webReferenceOptions.CodeGenerationOptions, options.CodeProvider, new ImportContext(codeIdentifiers, false));
                }
                xmlSerializerOperationGenerator = new XmlSerializerOperationGenerator(options);
            }