public void CanCreateInstanceWithDefaultOptions()
        {
            ContractGenerator generator = new ContractGenerator(new ContractGenerationOptions());

            Assert.IsNull(generator.Configuration);
            Assert.AreEqual(0, generator.GeneratedChannelElements.Count);
            Assert.IsNotNull(generator.CodeCompileUnit);
        }
        public void ShouldGenerateContractFromHostWithDefaultOptions()
        {
            ContractGenerationOptions options = new ContractGenerationOptions();
			options.OutputConfiguration = ConfigurationLoader.LoadConfiguration(@"SampleData\Empty.config");
            ContractGenerator generator = new ContractGenerator(options);
            generator.Generate(GetMetadataSet(HostAddress));

            ContractGeneratorCommonAsserts(generator, options);
        }
        public void ShouldGenerateContractFromWsdlWithDefaultOptions()
        {
            ContractGenerationOptions options = new ContractGenerationOptions();
			options.OutputConfiguration = ConfigurationLoader.LoadConfiguration(@"SampleData\Empty.config", "ShouldGenerateContractFromWsdlWithDefaultOptions");
            Uri wsdlLocation = new Uri(ConfigurationLoader.GetConfigurationFilePath(@"SampleData\DescriptionModel\MockService.wsdl"));
            ContractGenerator generator = new ContractGenerator(options);
            generator.Generate(GetMetadataSet(wsdlLocation));

            ContractGeneratorCommonAsserts(generator, options);
        }
Пример #4
0
        /// <summary>
        /// Generates the code compile unit.
        /// </summary>
        /// <param name="importer">The importer.</param>
        /// <returns></returns>
        public CodeCompileUnit GenerateCodeCompileUnit(WsdlImporter importer)
        {
            Guard.ArgumentNotNull(importer, "importer");

            // now generate the full collection types
            ContractGenerator generator = CreateContractGenerator(this.useXmlSerializerImporter);

            generator.Generate(importer, false);
            return(generator.CodeCompileUnit);
        }
Пример #5
0
        /// <summary>
        /// Read the schemas resolving dependencies
        /// </summary>
        /// <param name="schemaSources">The schema sources. (files or URIs)</param>
        /// <returns>Read schema files</returns>
        public XmlSchemaSet ReadSchemas(string[] schemaSources)
        {
            Guard.ArgumentNotNull(schemaSources, "schemaSources");

            MetadataSet metadataDocuments = new MetadataSet();

            foreach (string schemaSource in schemaSources)
            {
                MetadataDiscovery discovery   = new MetadataDiscovery(schemaSource);
                MetadataSet       metadataSet = discovery.InspectMetadata();
                foreach (MetadataSection section in metadataSet.MetadataSections)
                {
                    metadataDocuments.MetadataSections.Add(section);
                }
            }

            ContractGenerator generator = CreateContractGenerator(this.useXmlSerializerImporter);

            return(ReadSchemas(generator.CreateWsdlImporter(metadataDocuments)));
        }
        public void ShouldGenerateProtectionLevelSignValueFromHost()
        {
            ContractGenerationOptions options = new ContractGenerationOptions();
            ContractGenerator generator = new ContractGenerator(options);
            generator.Generate(GetMetadataSet(HostAddress));

            string proxyClass = GetClassFromCcu(options.CodeProvider, generator.CodeCompileUnit);
            Assert.IsNotNull(proxyClass);
            Assert.IsTrue(proxyClass.Contains("ProtectionLevel.Sign"), "Contains ProtectionLevel value.");
        }
        private void ContractGeneratorCommonAsserts(ContractGenerator generator, ContractGenerationOptions options)
        {
            // Assert proxy class
            string proxyClass = GetClassFromCcu(options.CodeProvider, generator.CodeCompileUnit);
            Assert.IsNotNull(proxyClass);
            Assert.IsTrue(proxyClass.Contains("IMockServiceContract"));

            // Assert config file
            ServiceModelConfigurationManager manager = new ServiceModelConfigurationManager(generator.Configuration);
            ClientSection client = manager.GetClient();

            Assert.AreEqual(1, client.Endpoints.Count);
            ChannelEndpointElement createdEndpoint = client.Endpoints[0];
            Assert.AreEqual(Constants.Uris.TestContractGenerationEndpointAddress, createdEndpoint.Address.AbsoluteUri);
            Assert.AreEqual(Constants.ServiceDescription.WsHttpHostClientName, createdEndpoint.Name);
            Assert.AreEqual(Constants.ServiceDescription.WsHttpHostClientBinding, createdEndpoint.Binding);
        }
		public void ShouldMergeConfigContentWithSameEndpoint()
		{
			ContractGenerationOptions options = new ContractGenerationOptions();
			options.OutputConfigurationFile = ConfigurationLoader.GetConfigurationFilePath(@"SampleData\Empty.config", "ShouldMergeConfigContentWithSameEndpoint", true);

			string originalConfig = File.ReadAllText(options.OutputConfigurationFile);
			ContractGenerator generator = new ContractGenerator(options);
			MetadataSet metadata = GetMetadataSet(HostAddress);
			WsdlImporter importer = generator.CreateWsdlImporter(metadata);
			generator.Generate(importer);
			// generate twice in order to update and get only one endpoint in config
			generator.Generate(importer);

			Assert.AreNotEqual(originalConfig, File.ReadAllText(options.OutputConfigurationFile));

			// Assert config file
			ServiceModelConfigurationManager manager = new ServiceModelConfigurationManager(generator.Configuration);
			ClientSection client = manager.GetClient();
			Assert.AreEqual<int>(2, client.Endpoints.Count);
		}
		public void ShouldGenerateContractFromHostWithDataContractSerializerType()
        {
            ContractGenerationOptions options = new ContractGenerationOptions();
			options.OutputConfiguration = ConfigurationLoader.LoadConfiguration(@"SampleData\Empty.config");
            options.ClrNamespace = "Test.Namespace1";
            ContractGenerator generator = new ContractGenerator(options);
            generator.Generate(GetMetadataSet(HostAddress));

            string proxyClass = GetClassFromCcu(options.CodeProvider, generator.CodeCompileUnit);
            Assert.IsNotNull(proxyClass);
            Assert.IsTrue(proxyClass.Contains(options.ClrNamespace), "Contains namespace");
            Assert.IsTrue(proxyClass.Contains("MyDataContract"), "Contains MyDataContract class");

			CompilerResults results = DynamicCompilation.CompileAssemblyFromSource(proxyClass);

			Type generatedType = results.CompiledAssembly.GetType(options.ClrNamespace + ".MyDataContract", false);
			Assert.IsNotNull(generatedType);
        }
        public void ShouldGenerateVBProxy()
        {
            ContractGenerationOptions options = new ContractGenerationOptions();
            options.CodeProvider = new VBCodeProvider();
            ContractGenerator generator = new ContractGenerator(options);
			Uri wsdlLocation = new Uri(ConfigurationLoader.GetConfigurationFilePath(@"SampleData\DescriptionModel\MockService.wsdl"));
            generator.Generate(GetMetadataSet(wsdlLocation));

            string proxyClass = GetClassFromCcu(options.CodeProvider, generator.CodeCompileUnit);
            Assert.IsNotNull(proxyClass);
            Assert.IsTrue(proxyClass.Contains("Public Sub New()"), "Contains VB code");
        }
        public void ShouldGenerateOneEndpointFromHostWithMultipleEndpoints()
        {
            ContractGenerationOptions options = new ContractGenerationOptions();
			options.OutputConfiguration = ConfigurationLoader.LoadConfiguration(@"SampleData\Empty.config");
            options.ImportedEndpointNames.Add(Constants.ServiceDescription.WsHttpHostClientName);
            ContractGenerator generator = new ContractGenerator(options);
            MetadataSet metadata = GetMetadataSet(HostAddressMultiEndpoint);
            generator.Generate(GetMetadataSet(HostAddressMultiEndpoint));

            ContractGeneratorCommonAsserts(generator, options);
        }
        public void ThrowOnCustomWsdlImporterNotFound()
        {
            ContractGenerationOptions options = new ContractGenerationOptions();
			options.OutputConfiguration = ConfigurationLoader.LoadConfiguration(@"SampleData\DescriptionModel\WsdlImporterNotFound.config");
            ContractGenerator generator = new ContractGenerator(options);
            generator.Generate(GetMetadataSet(HostAddress));
        }
        public void ShouldGenerateConfigFileWithWsHttpBinding()
        {
            ContractGenerationOptions options = new ContractGenerationOptions();
			options.OutputConfigurationFile = ConfigurationLoader.GetConfigurationFilePath(@"SampleData\Empty.config", "ShouldGenerateConfigFileWithWsHttpBinding");
            ContractGenerator generator = new ContractGenerator(options);
            generator.Generate(GetMetadataSet(HostAddress));
            string configFileContent = File.ReadAllText(options.OutputConfigurationFile);
            Assert.IsTrue(configFileContent.Contains("<wsHttpBinding>"));
        }
        public void ShouldGenerateAndSaveConfigFile()
        {
            ContractGenerationOptions options = new ContractGenerationOptions();
			options.OutputConfigurationFile = ConfigurationLoader.GetConfigurationFilePath(@"SampleData\Empty.config", "ShouldGenerateAndSaveConfigFile", true);

            string originalConfig = File.ReadAllText(options.OutputConfigurationFile);
            ContractGenerator generator = new ContractGenerator(options);
            generator.Generate(GetMetadataSet(HostAddress));

            Assert.AreNotEqual(originalConfig, File.ReadAllText(options.OutputConfigurationFile));
        }
        public void ShouldGenerateProtectionLevelSignValueFromWsdlFile()
        {
            ContractGenerationOptions options = new ContractGenerationOptions();
            ContractGenerator generator = new ContractGenerator(options);
			Uri wsdlLocation = new Uri(ConfigurationLoader.GetConfigurationFilePath(@"SampleData\DescriptionModel\MockService.wsdl"));
            generator.Generate(GetMetadataSet(wsdlLocation));

            string proxyClass = GetClassFromCcu(options.CodeProvider, generator.CodeCompileUnit);
            Assert.IsNotNull(proxyClass);
            Assert.IsTrue(proxyClass.Contains("ProtectionLevel.Sign"), "Contains ProtectionLevel value.");
        }