Пример #1
0
 /// <summary>
 /// Checks the specified configuration manager.
 /// </summary>
 /// <param name="configurationManager">The configuration manager.</param>
 /// <returns></returns>
 public override ProblemCollection Check(ServiceModelConfigurationManager configurationManager)
 {
     // Check for service endpoints
     foreach (ServiceElement serviceElement in configurationManager.GetServices())
     {
         foreach(ServiceEndpointElement endpointElement in serviceElement.Endpoints)
         {
             if(endpointElement.Binding.Equals(CustomBindingAttributeValue, StringComparison.InvariantCultureIgnoreCase))
             {
                 CheckRuleForCustomBinding(configurationManager.GetCustomBinding(endpointElement.BindingConfiguration));
             }
         }
     }
     // Check for client endponts
     ClientSection client = configurationManager.ServiceModelSection.Client;
     if(client != null)
     {
         foreach(ChannelEndpointElement clientEndpoint in client.Endpoints)
         {
             if (clientEndpoint.Binding.Equals(CustomBindingAttributeValue, StringComparison.InvariantCultureIgnoreCase))
             {
                 CheckRuleForCustomBinding(configurationManager.GetCustomBinding(clientEndpoint.BindingConfiguration));
             }
         }
     }
     return base.Problems;
 }
        public void CanCheckIfContainsServices()
        {
            ServiceModelConfigurationManager manager = LoadManager();

            Assert.IsTrue(manager.GetServices().Count > 0);
            Assert.IsTrue(manager.HasServices);
        }
        public void GetBehaviorExtensionTest()
        {
            NamedServiceModelExtensionCollectionElement <BehaviorExtensionElement> element = LoadManager().GetBehavior(Constants.ServiceBehaviorExtension);
            ServiceCredentialsElement extension = ServiceModelConfigurationManager.GetBehaviorExtensionElement <ServiceCredentialsElement>(element);

            Assert.IsNotNull(extension);
            Assert.AreEqual(Constants.TestCert, extension.ServiceCertificate.FindValue);
        }
        public void CanGetEndpointNamesWithNullPredicate()
        {
            ServiceModelConfigurationManager manager = LoadManager();

            manager.AddEndpoint(Constants.ServiceName, ServiceModelConfigurationManager.GetSecureMetadataExchangeEndpoint());
            IList <string> endpointNames = manager.GetEndpointNames(Constants.ServiceName, null);

            Assert.AreEqual(3, endpointNames.Count);
        }
        public void CanGetClientEndpoint()
        {
            ServiceModelConfigurationManager manager  = LoadManager();
            ChannelEndpointElement           endpoint = manager.GetEndpoint(Constants.ServiceDescription.ClientEndpointName);

            Assert.AreEqual(Constants.ServiceDescription.ClientEndpointName, endpoint.Name);
            Assert.AreEqual(Constants.ServiceDescription.ClientBehaviorConfiguration, endpoint.BehaviorConfiguration);
            Assert.AreEqual(Constants.ServiceDescription.ClientBindingName, endpoint.Binding);
        }
        public void CanAddExistingEndpoint()
        {
            ServiceModelConfigurationManager manager  = LoadManager();
            ServiceEndpointElement           endpoint = manager.GetEndpoint(Constants.ServiceName, Constants.EndpointName);

            manager.AddEndpoint(Constants.ServiceName, endpoint);

            Assert.AreEqual(endpoint, manager.GetEndpoint(Constants.ServiceName, Constants.EndpointName));
        }
        public void ClientBindingTest()
        {
            ServiceModelConfigurationManager manager = LoadManager();
            ClientSection        client          = manager.GetClient();
            CustomBindingElement binding         = manager.GetCustomBinding(client.Endpoints[0].BindingConfiguration);
            SecurityElement      securitySection = binding[typeof(SecurityElement)] as SecurityElement;

            Assert.IsNotNull(securitySection);
            Assert.AreEqual(AuthenticationMode.UserNameForSslNegotiated, securitySection.SecureConversationBootstrap.AuthenticationMode);
        }
        public void CanAddNewEndpoint()
        {
            ServiceEndpointElement           mexEndpoint = ServiceModelConfigurationManager.GetMetadataExchangeEndpoint();
            ServiceModelConfigurationManager manager     = LoadManager();

            manager.AddEndpoint(Constants.ServiceName, mexEndpoint);

            Assert.AreEqual(mexEndpoint,
                            manager.GetEndpoint(Constants.ServiceName, manager.GetEndpointName(mexEndpoint)));
        }
        public void ClientBehaviorTest()
        {
            ServiceModelConfigurationManager manager = LoadManager();
            ClientSection client = manager.GetClient();
            NamedServiceModelExtensionCollectionElement <BehaviorExtensionElement> behavior = manager.GetBehavior(client.Endpoints[0].BehaviorConfiguration);
            ClientCredentialsElement credentialsSection = ServiceModelConfigurationManager.GetBehaviorExtensionElement <ClientCredentialsElement>(behavior);

            Assert.AreEqual(Constants.TestCert, credentialsSection.ClientCertificate.FindValue);
            Assert.AreEqual(System.ServiceModel.Security.X509CertificateValidationMode.ChainTrust,
                            credentialsSection.ServiceCertificate.Authentication.CertificateValidationMode);
        }
        public void AddStandardBindingTest()
        {
            BasicHttpBindingElement          element = new BasicHttpBindingElement("newBasicBinding");
            ServiceModelConfigurationManager manager = LoadManager();

            manager.AddStandardBinding <BasicHttpBinding, BasicHttpBindingElement>(element);

            BasicHttpBindingElement addedElement = manager.GetStandardBinding <BasicHttpBinding, BasicHttpBindingElement>(element.Name);

            Assert.AreEqual(element.Name, addedElement.Name);
        }
        public void AddCustomBindingTest()
        {
            CustomBindingElement             element = new CustomBindingElement("newBasicBinding");
            ServiceModelConfigurationManager manager = LoadManager();

            manager.AddCustomBinding(element);

            CustomBindingElement addedElement = manager.GetCustomBinding("newBasicBinding");

            Assert.AreEqual(element.Name, addedElement.Name);
        }
        public void UpdateBehaviorExtensionTest()
        {
            NamedServiceModelExtensionCollectionElement <BehaviorExtensionElement> element = LoadManager().GetBehavior(Constants.ServiceBehaviorName);
            ServiceCredentialsElement extension = new ServiceCredentialsElement();

            extension.UserNameAuthentication.MembershipProviderName = Constants.MembershipProviderName;
            ServiceCredentialsElement updatedExtension = ServiceModelConfigurationManager.GetBehaviorExtensionElement <ServiceCredentialsElement>(element);

            Assert.IsNotNull(updatedExtension);
            Assert.AreEqual(updatedExtension.BehaviorType, extension.BehaviorType);
            Assert.AreEqual(Constants.MembershipProviderName, updatedExtension.UserNameAuthentication.MembershipProviderName);
        }
        public void CanGetEndpointNamesWithoutMexEndpoints()
        {
            ServiceEndpointElement           mexEndpoint       = ServiceModelConfigurationManager.GetMetadataExchangeEndpoint();
            ServiceEndpointElement           secureMexEndpoint = ServiceModelConfigurationManager.GetSecureMetadataExchangeEndpoint();
            ServiceModelConfigurationManager manager           = LoadManager();

            manager.AddEndpoint(Constants.ServiceName, mexEndpoint);
            manager.AddEndpoint(Constants.ServiceName, secureMexEndpoint);

            IList <string> endpointNames = manager.GetEndpointNames(Constants.ServiceName);

            Assert.IsFalse(endpointNames.Contains(manager.GetEndpointName(mexEndpoint)));
            Assert.IsFalse(endpointNames.Contains(manager.GetEndpointName(secureMexEndpoint)));
        }
        public void AddCustomBindingNewSectionTest()
        {
            // Remove any previous CustomBinding section
            ServiceModelConfigurationManager manager = LoadManager();

            manager.Configuration.Sections.Remove("customBinding");

            CustomBindingElement element = new CustomBindingElement("newBasicBinding");

            manager.AddCustomBinding(element);

            CustomBindingElement addedElement = manager.GetCustomBinding("newBasicBinding");

            Assert.AreEqual(element.Name, addedElement.Name);
        }
        public void UpdateCustomBindingTest()
        {
            ServiceModelConfigurationManager manager = LoadManager();

            manager.AddCustomBinding(new CustomBindingElement(Constants.ServiceBindingName));

            CustomBindingElement element = manager.GetCustomBinding(Constants.ServiceBindingName);

            element.Add(new SecurityElement());
            element.Add(new HttpTransportElement());
            manager.AddCustomBinding(element);

            CustomBindingElement updatedElement = manager.GetCustomBinding(Constants.ServiceBindingName);

            Assert.AreEqual(2, updatedElement.Count);
        }
        /// <summary>
        /// Checks the specified configuration manager.
        /// </summary>
        /// <param name="configurationManager">The configuration manager.</param>
        /// <returns></returns>
        public override ProblemCollection Check(ServiceModelConfigurationManager configurationManager)
        {
            foreach (ServiceBehaviorElement behaviorElement in configurationManager.ServiceModelSection.Behaviors.ServiceBehaviors)
            {
                ServiceDebugElement serviceDebug =
                    ServiceModelConfigurationManager.GetBehaviorExtensionElement<ServiceDebugElement>(behaviorElement);

                if (serviceDebug.IncludeExceptionDetailInFaults)
                {
                    Resolution resolution = base.GetResolution(behaviorElement.Name);
                    Problem problem = new Problem(resolution);
                    problem.SourceFile = base.SourceFile;
                    base.Problems.Add(problem);
                }
            }
            return base.Problems;
        }
 public override ProblemCollection Check(ServiceModelConfigurationManager configurationManager)
 {
     foreach (ServiceElement service in configurationManager.ServiceModelSection.Services.Services)
     {
         List<string> keys = new List<string>();
         foreach (ServiceEndpointElement endpoint in service.Endpoints)
         {
             if (keys.Contains(endpoint.Address.OriginalString))
             {
                 Resolution resolution = base.GetResolution(endpoint.Address.OriginalString);
                 Problem problem = new Problem(resolution);
                 base.Problems.Add(problem);
                 continue;
             }
             keys.Add(endpoint.Address.OriginalString);
         }
     }
     return base.Problems;
 }
        /// <summary>
        /// Checks the specified configuration manager.
        /// </summary>
        /// <param name="configurationManager">The configuration manager.</param>
        /// <returns></returns>
        public override ProblemCollection Check(ServiceModelConfigurationManager configurationManager)
        {
            foreach (ServiceBehaviorElement behaviorElement in configurationManager.ServiceModelSection.Behaviors.ServiceBehaviors)
            {
                ServiceCredentialsElement serviceCredentials =
                    ServiceModelConfigurationManager.GetBehaviorExtensionElement<ServiceCredentialsElement>(behaviorElement);

                X509RevocationMode revocationMode = serviceCredentials.ClientCertificate.Authentication.RevocationMode;

                if (revocationMode == X509RevocationMode.NoCheck)
                {
                    Resolution resolution = base.GetResolution(revocationMode.ToString());
                    Problem problem = new Problem(resolution);
                    problem.SourceFile = base.SourceFile;
                    base.Problems.Add(problem);
                }
            }
            return base.Problems;
        }
        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 GetBindingNamesTest()
        {
            IList <string> actual = LoadManager().GetBindingNames();

            Assert.IsNotNull(actual);
            Assert.IsTrue(actual.Count > 0);
            bool passed = false;

            foreach (string bindingName in actual)
            {
                string[] names = ServiceModelConfigurationManager.SplitBindingName(bindingName);
                if (names[0] == Constants.ServiceBindingName &&
                    names[1] == Constants.WSHttpBindingName)
                {
                    passed = true;
                    break;
                }
            }
            Assert.IsTrue(passed);
        }
        /// <summary>
        /// Checks the specified configuration manager.
        /// </summary>
        /// <param name="configurationManager">The configuration manager.</param>
        /// <returns></returns>
        public override ProblemCollection Check(ServiceModelConfigurationManager configurationManager)
        {
            foreach (EndpointBehaviorElement behaviorElement in configurationManager.ServiceModelSection.Behaviors.EndpointBehaviors)
            {
                ClientCredentialsElement clientCredentials =
                    ServiceModelConfigurationManager.GetBehaviorExtensionElement<ClientCredentialsElement>(behaviorElement);

                var validationMode = clientCredentials.ServiceCertificate.Authentication.CertificateValidationMode;

                if (validationMode != X509CertificateValidationMode.ChainTrust)
                {
                    Resolution resolution = base.GetResolution(validationMode.ToString(),
                        X509CertificateValidationMode.ChainTrust.ToString());
                    Problem problem = new Problem(resolution);
                    problem.SourceFile = base.SourceFile;
                    base.Problems.Add(problem);
                }
            }
            return base.Problems;
        }
        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 UpdateServiceTest()
        {
            ServiceModelConfigurationManager manager = LoadManager();

            manager.UpdateService(Constants.ServiceName, Constants.EndpointName, "someBinding", "customBinding", "someBehavior");

            ServiceElement service = manager.GetService(Constants.ServiceName);

            Assert.AreEqual("someBehavior", service.BehaviorConfiguration);

            ServiceEndpointElement endpointFound = null;

            foreach (ServiceEndpointElement endpoint in service.Endpoints)
            {
                if (endpoint.Binding.Equals("customBinding"))
                {
                    endpointFound = endpoint;
                    break;
                }
            }
            Assert.IsNotNull(endpointFound);
            Assert.AreEqual("customBinding", endpointFound.Binding);
            Assert.AreEqual("someBinding", endpointFound.BindingConfiguration);
        }
Пример #24
0
 private Collection<IWsdlImportExtension> LoadWsdlImportExtensions()
 {
     if (this.configuration != null)
     {
         ServiceModelConfigurationManager manager = new ServiceModelConfigurationManager(this.configuration);
         ClientSection client = manager.GetClient();
         if (client != null)
         {
             return client.Metadata.LoadWsdlImportExtensions();
         }
     }
     return null;
 }
Пример #25
0
 /// <summary>
 /// Checks the specified configuration manager.
 /// </summary>
 /// <param name="configurationManager">The configuration manager.</param>
 /// <returns></returns>
 public override ProblemCollection Check(ServiceModelConfigurationManager configurationManager)
 {
     //store the config manager for later use
     this.configurationManager = configurationManager;
     return base.Problems;
 }
        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);
        }
Пример #27
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:ContractBindingRule"/> class.
 /// </summary>
 /// <param name="name">The name.</param>
 protected ContractBindingRule(string name) : base(name)
 {
     configurationManager = null;
 }
		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 override ProblemCollection Check(ServiceModelConfigurationManager configurationManager)
 {
     return base.Problems;
 }
 public override ProblemCollection Check(ServiceModelConfigurationManager configurationManager)
 {
     return(base.Problems);
 }
 /// <summary>
 /// Checks the specified configuration manager.
 /// </summary>
 /// <param name="configurationManager">The configuration manager.</param>
 /// <returns></returns>
 public abstract ProblemCollection Check(ServiceModelConfigurationManager configurationManager);
Пример #32
0
        public CodeGenerationResults Generate(IArtifactLink link)
        {
            CodeGenerationResults result           = new CodeGenerationResults();
            string       serviceImplementationName = string.Empty;
            string       serviceContractName       = string.Empty;
            string       serviceNamespace          = string.Empty;
            const string behavior = "_Behavior";

            if (link is IModelReference)
            {
                this.serviceProvider = Utility.GetData <IServiceProvider>(link);
                ProjectNode project = Utility.GetData <ProjectNode>(link);

                ServiceDescription serviceDescription = ((IModelReference)link).ModelElement as ServiceDescription;
                Configuration      configuration      = GetConfiguration(link, project);

                // abort if we got errors in config file
                if (configuration == null)
                {
                    return(result);
                }

                try
                {
                    ServiceReference serviceReference = (ServiceReference)serviceDescription;
                    SCModel.Service  service          = GetMelReference <SCModel.Service>(serviceReference.ServiceImplementationType);
                    serviceImplementationName = ResolveTypeReference(service);
                    serviceContractName       = GetServiceContractName(service.ServiceContract);
                    serviceNamespace          = service.ServiceContract.Namespace;

                    ServiceModelConfigurationManager manager = new ServiceModelConfigurationManager(configuration);

                    ServiceElement serviceElement = new ServiceElement();
                    serviceElement.Name = serviceImplementationName;
                    serviceElement.BehaviorConfiguration = string.Concat(serviceImplementationName, behavior);

                    foreach (Endpoint endpoint in serviceDescription.Endpoints)
                    {
                        ServiceEndpointElement endpointElement = new ServiceEndpointElement();
                        endpointElement.Name             = endpoint.Name;
                        endpointElement.Contract         = serviceContractName;
                        endpointElement.Binding          = ((WcfEndpoint)endpoint.ObjectExtender).BindingType.ToString();
                        endpointElement.Address          = new Uri(endpoint.Address ?? string.Empty, UriKind.RelativeOrAbsolute);
                        endpointElement.BindingNamespace = serviceNamespace;
                        serviceElement.Endpoints.Add(endpointElement);
                    }

                    manager.UpdateService(serviceElement);

                    ServiceBehaviorElement behaviorElement = new ServiceBehaviorElement();
                    behaviorElement.Name = string.Concat(serviceImplementationName, behavior);
                    ServiceDebugElement debugElement = new ServiceDebugElement();
                    debugElement.IncludeExceptionDetailInFaults = false;
                    behaviorElement.Add(debugElement);

                    if (((WcfServiceDescription)serviceDescription.ObjectExtender).EnableMetadataPublishing)
                    {
                        ServiceMetadataPublishingElement metadataPublishingElement = new ServiceMetadataPublishingElement();
                        metadataPublishingElement.HttpGetEnabled = true;
                        behaviorElement.Add(metadataPublishingElement);
                        ServiceEndpointElement mexEndpointElement = ServiceModelConfigurationManager.GetMetadataExchangeEndpoint();
                        serviceElement.Endpoints.Add(mexEndpointElement);
                    }

                    manager.UpdateBehavior(behaviorElement);
                    manager.Save();

                    result.Add(link.ItemPath, File.ReadAllText(configuration.FilePath));
                }
                finally
                {
                    if (configuration != null && File.Exists(configuration.FilePath))
                    {
                        File.Delete(configuration.FilePath);
                    }
                }
            }

            return(result);
        }