private static void GenerateCodeDomTree(WsdlImporter wsdlImporter, ServiceContractGenerator contractGenerator)
		{
			Collection<ContractDescription> contracts = wsdlImporter.ImportAllContracts();
			Collection<Binding> bindings = wsdlImporter.ImportAllBindings();
			ServiceEndpointCollection endpoints = wsdlImporter.ImportAllEndpoints();

			if (wsdlImporter.Errors.Any(e => !e.IsWarning))
			{
				throw new CodeGenerationException(wsdlImporter.Errors);
			}

			foreach (ContractDescription contract in contracts)
			{
				//TODO:Alex:Make the naming scheme customisable.
				contract.Name = "I" + contract.Name.Replace("Interface", string.Empty);
				contractGenerator.GenerateServiceContractType(contract);
			}

			foreach (Binding binding in bindings)
			{
				string bindingSectionName, configurationName;
				contractGenerator.GenerateBinding(binding, out bindingSectionName, out configurationName);
			}

			foreach (ServiceEndpoint endpoint in endpoints)
			{
				ChannelEndpointElement channelElement;
				contractGenerator.GenerateServiceEndpoint(endpoint, out channelElement);
			}
		}
        /// <summary>
        /// Generate Proxy Code and (if available) configuration
        /// </summary>
        /// <param name="contractGenerator">The contract generator to use</param>
        /// <param name="targetCompileUnit">Compile unit into which we should generate the client code</param>
        /// <param name="proxyNamespace">CLR namespace into which we should generate the client code</param>
        /// <param name="configurationNamespace">Namespace to use in configuration</param>
        /// <param name="contractCollection">The contracts for which we should generate code and optionally config</param>
        /// <param name="bindingCollection">The bindings we should generate config for</param>
        /// <param name="serviceEndpointList">The endpoints we should generate config for</param>
        /// <param name="proxyGenerationErrors">A list of errors encountered while generating the client</param>
        /// <param name="serviceEndpointToChannelEndpointElementMap">Map from service endpoint to the configuration element for the endpoint</param>
        /// <param name="proxyGeneratedContractTypes">The generated contract types</param>
        protected static void GenerateProxy(WsdlImporter importer,
                                            ServiceContractGenerator contractGenerator,
                                            CodeCompileUnit targetCompileUnit,
                                            string proxyNamespace,
                                            string configurationNamespace,
                                            IEnumerable<ContractDescription> contractCollection,
                                            IEnumerable<System.ServiceModel.Channels.Binding> bindingCollection,
                                            List<ServiceEndpoint> serviceEndpointList,
                                            IList<ProxyGenerationError> proxyGenerationErrors,
                                            out Dictionary<ServiceEndpoint, ChannelEndpointElement> serviceEndpointToChannelEndpointElementMap,
                                            out List<GeneratedContractType> proxyGeneratedContractTypes)
        {
            // Parameter checking
            if (serviceEndpointList == null) throw new ArgumentNullException("serviceEndpointList");
            if (bindingCollection == null) throw new ArgumentNullException("bindingCollection");
            if (contractCollection == null) throw new ArgumentNullException("contractCollection");
            if (proxyGenerationErrors == null) throw new ArgumentNullException("proxyGenerationErrors");

            proxyGeneratedContractTypes = new List<GeneratedContractType>();
            serviceEndpointToChannelEndpointElementMap = new Dictionary<ServiceEndpoint, ChannelEndpointElement>();

            try
            {
                HttpBindingExtension httpBindingEx = importer.WsdlImportExtensions.Find<HttpBindingExtension>();

                foreach (ContractDescription contract in contractCollection)
                {
                    if (httpBindingEx == null || !httpBindingEx.IsHttpBindingContract(contract) || serviceEndpointList.Any(endpoint => endpoint.Contract == contract))
                    {
                        CodeTypeReference typeReference = contractGenerator.GenerateServiceContractType(contract);
                        if (typeReference != null)
                        {
                            // keep the (targetNamespace, portType) -> CLR type map table...

                            string baseType = typeReference.BaseType;

                            GeneratedContractType generatedType = new GeneratedContractType(contract.Namespace, contract.Name, baseType, baseType);
                            proxyGeneratedContractTypes.Add(generatedType);
                        }
                    }
                }

                // We should only import the Binding & Endpoints if there is a configuration storage...
                if (contractGenerator.Configuration != null)
                {
                    foreach (ServiceEndpoint endpoint in serviceEndpointList)
                    {
                        ChannelEndpointElement endpointElement = null;
                        contractGenerator.GenerateServiceEndpoint(endpoint, out endpointElement);
                        serviceEndpointToChannelEndpointElementMap[endpoint] = endpointElement;
                    }

                    foreach (System.ServiceModel.Channels.Binding bindingDescription in bindingCollection)
                    {
                        string bindingSectionName = null;
                        string bindingConfigurationName = null;
                        // Generate binding will change the state of the contractGenerator... 
                        contractGenerator.GenerateBinding(bindingDescription, out bindingSectionName, out bindingConfigurationName);
                    }

                }

                PatchConfigurationName(proxyNamespace,
                                       configurationNamespace,
                                       proxyGeneratedContractTypes,
                                       serviceEndpointToChannelEndpointElementMap.Values,
                                       targetCompileUnit);
            }
            finally
            {
                foreach (MetadataConversionError error in contractGenerator.Errors)
                {
                    proxyGenerationErrors.Add(new ProxyGenerationError(error));
                }
            }
        }
Exemplo n.º 3
0
        private void GenerateConfig(
            ServiceContractGenerator contractGenerator, 
            ServiceEndpointCollection endpoints)
        {
			List<string> addedEndpoints = new List<string>();
            foreach (ServiceEndpoint endpoint in endpoints)
            {
                // filter by endpoint address so we generate only the endpoint 
                // that matches the endpoint names in ImportedEndpointNames
                if (!addedEndpoints.Contains(endpoint.Name) &&
					(options.ImportedEndpointNames.Count == 0 ||
                     options.ImportedEndpointNames.Contains(endpoint.Name)))
                {
                    // generate service endpoint
                    ChannelEndpointElement channelElement;
                    contractGenerator.GenerateServiceEndpoint(endpoint, out channelElement);
                    this.generatedChannelElements.Add(channelElement);
                    // generate the binding
                    string bindingSectionName;
                    string configurationName;
                    contractGenerator.GenerateBinding(endpoint.Binding, out bindingSectionName, out configurationName);
                    ThrowOnMetadataConversionErrors(contractGenerator.Errors);
					addedEndpoints.Add(endpoint.Name);
                }
            }

            // Save changes if specified.
            if (!string.IsNullOrEmpty(options.OutputConfigurationFile))
            {
                configuration.Save(ConfigurationSaveMode.Modified);
            }
        }
        /// <summary>
        /// Generates the basic CodeNamespace using .NET Fx code generation API.
        /// </summary>
        private void CreateBasicCodeDomTree()
        {
            TweakWsdlImporter();

            Collection<ContractDescription> contracts = wsdlImporter.ImportAllContracts();
            Collection<Binding> bindings = wsdlImporter.ImportAllBindings();
            ServiceEndpointCollection endpoints = wsdlImporter.ImportAllEndpoints();

			if (wsdlImporter.Errors.Any(e => !e.IsWarning))
			{
				throw new ClientServiceGenerationException(wsdlImporter.Errors);
			}

            ServiceContractGenerator scg = new ServiceContractGenerator(compileUnit, Configuration);
            TweakServiceContractGenerator(scg);

            foreach (ContractDescription contract in contracts)
            {
				contract.Name = "I" + contract.Name.Replace("Interface", string.Empty);
                CodeTypeReference ctr = scg.GenerateServiceContractType(contract);
            }

            foreach (Binding binding in bindings)
            {
				string bindingSectionName, configurationName;
				scg.GenerateBinding(binding, out bindingSectionName, out configurationName);
            }

            foreach (ServiceEndpoint endpoint in endpoints)
            {
				ChannelEndpointElement channelElement;
				scg.GenerateServiceEndpoint(endpoint, out channelElement);
            }
        }
Exemplo n.º 5
0
        internal static void CreateConfig(Binding binding, string filename)
        {
            if (false && File.Exists (filename))
                File.Delete (filename);

            var element = new BasicHttpBindingElement ("Test");
            foreach (PropertyInformation prop in element.ElementInformation.Properties) {
                Console.WriteLine ("PROP: {0} {1}", prop.Name, prop.IsRequired);
            }

            var fileMap = new ExeConfigurationFileMap ();
            fileMap.ExeConfigFilename = filename;
            var config = ConfigurationManager.OpenMappedExeConfiguration (
                fileMap, ConfigurationUserLevel.None);

            Console.WriteLine ("CREATE CONFIG: {0}", binding.Name);

            var generator = new ServiceContractGenerator (config);

            string sectionName, configName;
            generator.GenerateBinding (binding, out sectionName, out configName);

            Console.WriteLine ("CONFIG: {0} {1} {2}", binding, sectionName, configName);

            config.Save (ConfigurationSaveMode.Minimal);
            Console.WriteLine ("CONFIG: {0}", config.FilePath);

            NormalizeConfig (filename);
            Dump (config.FilePath);
        }