public ConfigurationDiscoveryEndpointProvider(ChannelEndpointElement channelEndpointElement)
        {
            Fx.Assert(channelEndpointElement != null, "The channelEndpointElement parameter must be non null.");

            ConfigurationDiscoveryEndpointProvider.ValidateAndGetDiscoveryEndpoint(channelEndpointElement);
            this.channelEndpointElement = channelEndpointElement;
        }
        static DiscoveryEndpoint ValidateAndGetDiscoveryEndpoint(ChannelEndpointElement channelEndpointElement)
        {
            if (string.IsNullOrEmpty(channelEndpointElement.Kind))
            {
                throw FxTrace.Exception.AsError(
                          new ConfigurationErrorsException(
                              SR2.DiscoveryConfigDiscoveryEndpointMissingKind(
                                  typeof(DiscoveryEndpoint).FullName)));
            }

            ServiceEndpoint serviceEndpoint = ConfigLoader.LookupEndpoint(channelEndpointElement, null);

            if (serviceEndpoint == null)
            {
                throw FxTrace.Exception.AsError(
                          new ConfigurationErrorsException(
                              SR2.DiscoveryConfigInvalidEndpointConfiguration(
                                  channelEndpointElement.Kind)));
            }

            DiscoveryEndpoint discoveryEndpoint = serviceEndpoint as DiscoveryEndpoint;

            if (discoveryEndpoint == null)
            {
                throw FxTrace.Exception.AsError(
                          new InvalidOperationException(
                              SR2.DiscoveryConfigInvalidDiscoveryEndpoint(
                                  typeof(DiscoveryEndpoint).FullName,
                                  channelEndpointElement.Kind,
                                  serviceEndpoint.GetType().FullName)));
            }

            return(discoveryEndpoint);
        }
        public CodeTypeReference GenerateServiceEndpoint(
            ServiceEndpoint endpoint,
            out ChannelEndpointElement channelElement)
        {
            if (config == null)
            {
                throw new InvalidOperationException();
            }

            var cd    = endpoint.Contract;
            var cns   = GetNamespace(cd.Namespace);
            var cache = ExportInterface_internal(cd, cns);

            string bindingSectionName, configurationName;

            GenerateBinding(endpoint.Binding, out bindingSectionName, out configurationName);

            channelElement         = new ChannelEndpointElement();
            channelElement.Binding = bindingSectionName;
            channelElement.BindingConfiguration = configurationName;
            channelElement.Name     = configurationName;
            channelElement.Contract = cache.ConfigurationName;
            channelElement.Address  = endpoint.Address.Uri;

            var section = (ClientSection)config.GetSection("system.serviceModel/client");

            section.Endpoints.Add(channelElement);

            return(cache.GetReference());
        }
Exemplo n.º 4
0
        protected override void OnApplyConfiguration(ServiceEndpoint endpoint, ChannelEndpointElement serviceEndpointElement)
        {
            DynamicEndpoint dynamicEndpoint = (DynamicEndpoint)endpoint;

            if (!dynamicEndpoint.ValidateAndInsertDiscoveryClientBindingElement(dynamicEndpoint.Binding))
            {
                throw FxTrace.Exception.AsError(new ConfigurationErrorsException(SR.DiscoveryClientBindingElementPresentInDynamicEndpoint));
            }

            if (PropertyValueOrigin.Default == this.DiscoveryClientSettings.ElementInformation.Properties[ConfigurationStrings.Endpoint].ValueOrigin)
            {
                dynamicEndpoint.DiscoveryEndpointProvider = new ConfigurationDiscoveryEndpointProvider();
            }
            else
            {
                dynamicEndpoint.DiscoveryEndpointProvider = new ConfigurationDiscoveryEndpointProvider(this.DiscoveryClientSettings.DiscoveryEndpoint);
            }

            this.DiscoveryClientSettings.FindCriteria.ApplyConfiguration(dynamicEndpoint.FindCriteria);

            if (dynamicEndpoint.FindCriteria.ContractTypeNames.Count == 0)
            {
                dynamicEndpoint.FindCriteria.ContractTypeNames.Add(
                    new XmlQualifiedName(dynamicEndpoint.Contract.Name, dynamicEndpoint.Contract.Namespace));
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 获取终结点行为列表
        /// </summary>
        /// <returns>终结点行为列表</returns>
        private ICollection <IEndpointBehavior> GetEndpointBehaviors <T>()
        {
            string endpointName = typeof(T).FullName;

            #region # 验证

            if (!ServiceModelSetting.Endpoints.ContainsKey(endpointName))
            {
                throw new NullReferenceException($"名称为\"{endpointName}\"的终结点未配置!");
            }

            #endregion

            ICollection <IEndpointBehavior> endpointBehaviors = new HashSet <IEndpointBehavior>();
            ChannelEndpointElement          endpoint          = ServiceModelSetting.Endpoints[endpointName];
            if (!string.IsNullOrWhiteSpace(endpoint.BehaviorConfiguration))
            {
                if (!ServiceModelSetting.BehaviorConfigurations.TryGetValue(endpoint.BehaviorConfiguration, out BehaviorConfigurationElement behaviorConfiguration))
                {
                    throw new KeyNotFoundException($"未找到名称为\"{endpoint.BehaviorConfiguration}\"的行为配置!");
                }
                foreach (EndpointBehaviorElement endpointBehaviorElement in behaviorConfiguration.EndpointBehaviors)
                {
                    IEndpointBehavior endpointBehavior = endpointBehaviorElement.CreateEndpointBehavior();
                    endpointBehaviors.Add(endpointBehavior);
                }
            }

            return(endpointBehaviors);
        }
Exemplo n.º 6
0
        /// <summary>
        /// LookupEndpoint
        /// </summary>
        /// <param name="endpointName"></param>
        /// <returns></returns>
        private string LookupEndpoint(string endpointName)
        {
            if (string.IsNullOrWhiteSpace(endpointName))
            {
                return("*");
            }

            Configuration          config          = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ClientSection          clientSection   = ServiceModelSectionGroup.GetSectionGroup(config).Client;
            ChannelEndpointElement endpointElement = null;

            foreach (ChannelEndpointElement element in clientSection.Endpoints)
            {
                if (element.Name.EndsWith("_" + endpointName, StringComparison.OrdinalIgnoreCase) &&
                    element.Contract == typeof(TChannel).FullName)
                {
                    endpointElement = element;
                    break;
                }
            }
            if (endpointElement == null)
            {
                return("*");
            }
            return(endpointElement.Name);
        }
Exemplo n.º 7
0
        private EndpointAddress CreateEndpointAddress(
            ChannelEndpointElement clientEndpointElem)
        {
            EndpointIdentity identity = GetIdentity(clientEndpointElem.Identity);

            return(new EndpointAddress(clientEndpointElem.Address, identity, clientEndpointElem.Headers.Headers));
        }
        /// <summary>
        /// Updates an existing endpoint address in the assigned <see cref="SectionHandler{T}.ConfigSection"/> with a new name and a new address. The old
        /// endpoint can be located using the <paramref name="oldItem"/> parameter. If an old endpoint is not found, a new endpoint
        /// should be inserted.
        /// </summary>
        /// <param name="newKey">The updated key name for the endpoint.</param>
        /// <param name="newValue">The updated value for the endpoint.</param>
        /// <param name="oldKey">The old key name for the endpoint, or null.</param>
        /// <param name="oldItem">A reference to the old <see cref="ChannelEndpointElement"/> object obtained by <see cref="GetEnumerator"/>, or null.</param>
        public override void InsertOrUpdate(string newKey, string newValue, string oldKey = null, object oldItem = null)
        {
            if (newValue != null)
            {
                ChannelEndpointElement[] endpoints = new ChannelEndpointElement[ConfigSection.Endpoints.Count];
                ConfigSection.Endpoints.CopyTo(endpoints, 0);

                var oldKeyEndpoint = endpoints.FirstOrDefault(x => x.Name == oldKey);
                // Preserve the old entry if it exists, as it might have more than just name/address attributes.
                var cee = (oldItem as ChannelEndpointElement) ?? oldKeyEndpoint;

                var newKeyEndpoint = endpoints.FirstOrDefault(x => x.Name == newKey);

                // Make sure there are no entries using the old or new name other than this one
                if (oldKeyEndpoint != null)
                {
                    ConfigSection.Endpoints.Remove(oldKeyEndpoint);
                }

                if (oldKey != newKey && newKeyEndpoint != null)
                {
                    ConfigSection.Endpoints.Remove(newKeyEndpoint);
                }

                // Update values and re-add to the collection
                cee.Name    = newKey;
                cee.Address = new Uri(newValue);
                ConfigSection.Endpoints.Add(cee);
            }
        }
Exemplo n.º 9
0
        private void UpdateServiceAddress(string address, string endpointName)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);

            ClientSection          clientSection   = (ClientSection)config.GetSection("system.serviceModel/client");
            ChannelEndpointElement endpointElement = null;

            foreach (ChannelEndpointElement element in clientSection.Endpoints)
            {
                if (element.Name == endpointName)
                {
                    endpointElement = element;
                    break;
                }
            }
            if (endpointElement != null)
            {
                endpointElement.Address = new Uri(string.Format("{0}://{1}:{2}{3}",
                                                                endpointElement.Address.Scheme,
                                                                address,
                                                                endpointElement.Address.Port.ToString(),
                                                                endpointElement.Address.AbsolutePath));


                config.Save();
                ConfigurationManager.RefreshSection("system.serviceModel/client");
            }
            else
            {
                throw new ApplicationException(string.Format("Could not find {0} endpoint configuration section", endpointName));
            }
        }
Exemplo n.º 10
0
 internal static void InitializeAndValidateUdpChannelEndpointElement(ChannelEndpointElement channelEndpointElement)
 {
     if (!(channelEndpointElement.Address == null || String.IsNullOrEmpty(channelEndpointElement.Address.ToString())))
     {
         throw FxTrace.Exception.AsError(new ConfigurationErrorsException(SR2.DiscoveryConfigAddressSpecifiedForUdpDiscoveryEndpoint(channelEndpointElement.Kind)));
     }
     channelEndpointElement.Address = null;
 }
Exemplo n.º 11
0
 public ClientServiceWrapper(string baseUrl, string configurationName)
 {
     BaseUrl           = baseUrl;
     ConfigurationName = configurationName;
     EndpointElement   = GetEndPointElement(configurationName);
     Factory           = new ChannelFactory <TService>(EndpointElement.Name);
     Service           = CreateService(baseUrl, Factory, EndpointElement);
 }
Exemplo n.º 12
0
    public static string GetServiceUrl()
    {
        ClientSection          clientSection = (ClientSection)WebConfigurationManager.GetSection("system.serviceModel/client");
        ChannelEndpointElement endpoint      = clientSection.Endpoints[0];
        string address = endpoint.Address.ToString();

        return(address.Substring(0, address.LastIndexOf("/")));
    }
Exemplo n.º 13
0
 /// <summary>
 /// When called from a derived class, loads the service description information
 /// from the configuration file and applies it to the runtime being constructed.
 /// </summary>
 /// <param name="endpoint">And endpoint that enables clients to find and communicate with a service.</param>
 /// <param name="channelEndpointElement">An endpoint element that defined the physical
 /// transfer of messages ack and forth between the client and the service.</param>
 protected override void OnApplyConfiguration(ServiceEndpoint endpoint, ChannelEndpointElement channelEndpointElement)
 {
     throw Fx.Exception.AsError(
               new NotSupportedException(
                   Http.SR.HttpEndpointNotSupported(
                       httpEndpointType.Name,
                       httpClientType.Name)));
 }
Exemplo n.º 14
0
 /// <summary>
 /// When called from a derived class, initializes and verifies the format of the
 /// specified service endpoint element in a service application configuration file.
 /// </summary>
 /// <param name="channelEndpointElement">An endpoint element that defineds the physical transfer
 /// of messages back and forth between the client and the service.</param>
 protected override void OnInitializeAndValidate(ChannelEndpointElement channelEndpointElement)
 {
     throw Fx.Exception.AsError(
               new NotSupportedException(
                   Http.SR.HttpEndpointNotSupported(
                       httpEndpointType.Name,
                       httpClientType.Name)));
 }
        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);
        }
        private static Type GetChannelType(ChannelEndpointElement endpointElement)
        {
            ChannelEndpointElement element = endpointElement;

            return((from asm in AppDomain.CurrentDomain.GetAssemblies()
                    let contractType = asm.GetType(element.Contract)
                                       where contractType != null
                                       select contractType)
                   .FirstOrDefault());
        }
        /// <summary>
        /// Gets an <see cref="IEnumerator{T}"/> that iterates over the key/value pairs contained in the assigned <see cref="SectionHandler{T}.ConfigSection"/>. />
        /// </summary>
        /// <returns>An enumerator over pairs where the key is the existing name for each setting in the system.serviceModel/client section, and the value
        /// is a reference to the <see cref="ChannelEndpointElement"/> object referred to by that name.</returns>
        public override IEnumerator <KeyValuePair <string, object> > GetEnumerator()
        {
            // The Endpoints collection may change on us while we enumerate. :/
            ChannelEndpointElement[] endpoints = new ChannelEndpointElement[ConfigSection.Endpoints.Count];
            ConfigSection.Endpoints.CopyTo(endpoints, 0);

            foreach (ChannelEndpointElement cee in endpoints)
            {
                yield return(new KeyValuePair <string, object>(cee.Name, cee));
            }
        }
Exemplo n.º 18
0
        /// <summary>
        /// Loads the serviceEndpoint description from the specified configuration file
        /// </summary>
        /// <returns></returns>
        protected override ServiceEndpoint CreateDescription()
        {
            ServiceEndpoint          serviceEndpoint  = base.CreateDescription();
            ExeConfigurationFileMap  map              = GetExeConfigurationFileMap(serviceEndpoint);
            Configuration            config           = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            ServiceModelSectionGroup group            = ServiceModelSectionGroup.GetSectionGroup(config);
            ChannelEndpointElement   selectedEndpoint = GetSelectedEndpoint(group, serviceEndpoint);

            SetSelectedEndpoint(selectedEndpoint, group, serviceEndpoint);
            return(serviceEndpoint);
        }
Exemplo n.º 19
0
        public static TServiceContract CreateService <TServiceContract>(string bindingName)
        {
            ChannelEndpointElement endpoint = ConfigHelper.GetEndpoint(bindingName);

            if (endpoint == null)
            {
                throw new Exception("Endpoint not found!");
            }

            return(CreateService <TServiceContract>(endpoint.Address.ToString(), endpoint.Binding));
        }
Exemplo n.º 20
0
        /// <summary>
        /// Loads the serviceEndpoint description from the specified configuration file
        /// </summary>
        /// <returns>
        /// The <see cref="ServiceEndpoint"/>.
        /// </returns>
        protected override ServiceEndpoint CreateDescription()
        {
            var serviceEndpoint = base.CreateDescription();

            if (endpointConfigurationName != null)
            {
                serviceEndpoint.Name = endpointConfigurationName;
            }

            var map = new ExeConfigurationFileMap();

            map.ExeConfigFilename = configurationPath;

            var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            var group  = ServiceModelSectionGroup.GetSectionGroup(config);

            ChannelEndpointElement selectedEndpoint = null;

            foreach (ChannelEndpointElement endpoint in group.Client.Endpoints)
            {
                if (endpoint.Contract == serviceEndpoint.Contract.ConfigurationName &&
                    (endpointConfigurationName == null || endpointConfigurationName == endpoint.Name))
                {
                    selectedEndpoint = endpoint;
                    break;
                }
            }

            if (selectedEndpoint != null)
            {
                if (serviceEndpoint.Binding == null)
                {
                    serviceEndpoint.Binding = CreateBinding(selectedEndpoint.Binding, group);
                }

                if (serviceEndpoint.Address == null)
                {
                    serviceEndpoint.Address = new EndpointAddress(
                        selectedEndpoint.Address,
                        GetIdentity(selectedEndpoint.Identity),
                        selectedEndpoint.Headers.Headers);
                }

                if (serviceEndpoint.Behaviors.Count == 0 && selectedEndpoint.BehaviorConfiguration != null)
                {
                    AddBehaviors(selectedEndpoint.BehaviorConfiguration, serviceEndpoint, group);
                }

                serviceEndpoint.Name = selectedEndpoint.Contract;
            }

            return(serviceEndpoint);
        }
Exemplo n.º 21
0
        public void TestEmptyProps()
        {
            ChannelEndpointElement empty = new ChannelEndpointElement();

            Assert.AreEqual("", empty.Name, "#01");
            Assert.AreEqual(null, empty.Contract, "#02");
            Assert.AreEqual(null, empty.Binding, "#03");
            Assert.AreEqual(null, empty.Address, "#04");
            Assert.AreEqual("", empty.BindingConfiguration, "#05");
            Assert.AreEqual("", empty.BehaviorConfiguration, "#06");
            Assert.IsNotNull(empty.Headers, "#07");
            Assert.IsNotNull(empty.Identity, "#08");
        }
        public static ChannelEndpointElement GetServiceModelEndpoint(String name)
        {
            ClientSection clientSection = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;

            List<ChannelEndpointElement> channelEndpointElements = new List<ChannelEndpointElement>();
            foreach (ChannelEndpointElement endpointElement in clientSection.Endpoints)
            {
                channelEndpointElements.Add(endpointElement);
            }

            ChannelEndpointElement channelEndpointElement = channelEndpointElements.Single(x => x.Name == name);
            return channelEndpointElement;
        }
        private ChannelEndpointElement CreateEndPoint(string name, string address, string binding, string bindingConfiguration, string contract)
        {
            Uri addressUri = new Uri(address);

            ChannelEndpointElement endpoint = new ChannelEndpointElement();

            endpoint.Name    = name;
            endpoint.Binding = binding;
            endpoint.BindingConfiguration = bindingConfiguration;
            endpoint.Address  = addressUri;
            endpoint.Contract = contract;

            return(endpoint);
        }
Exemplo n.º 24
0
        public void Endpoints()
        {
            ServiceModelSectionGroup         g   = GetConfig("Test/config/test1");
            ChannelEndpointElementCollection col = g.Client.Endpoints;

            Assert.AreEqual(1, col.Count, "initial count");
            ChannelEndpointElement e = col [0];

            Assert.AreEqual(String.Empty, e.Name, "0.Name");
            Assert.AreEqual("IFoo", e.Contract, "0.Contract");
            Assert.AreEqual("basicHttpBinding", e.Binding, "0.Binding");
            col.Add(new ChannelEndpointElement());
            Assert.AreEqual(2, col.Count, "after Add()");
        }
        internal ChannelEndpointElement WriteChannelDescription(ServiceEndpoint endpoint, string typeName)
        {
            ChannelEndpointElement element = null;
            BindingDictionaryValue value2  = this.CreateBindingConfig(endpoint.Binding);

            element = new ChannelEndpointElement(endpoint.Address, typeName)
            {
                Name = NamingHelper.GetUniqueName(NamingHelper.CodeName(endpoint.Name), new NamingHelper.DoesNameExist(this.CheckIfChannelNameInUse), null),
                BindingConfiguration = value2.BindingName,
                Binding = value2.BindingSectionName
            };
            this.channels.Add(element);
            return(element);
        }
Exemplo n.º 26
0
        internal static Client <ProvisioningServiceClient> ConstructProvisioningServiceClient(string endpointConfigName, string userName, string userPassword)
        {
            ProvisioningServiceClient client    = null;
            ChannelEndpointElement    channelEE = _configDictionary[typeof(ProvisioningService).FullName].Find(delegate(ChannelEndpointElement cee) { return(cee.Name == endpointConfigName); });

            if (null != channelEE)
            {
                client = new ProvisioningServiceClient(endpointConfigName, channelEE.Address.ToString());
                client.ClientCredentials.UserName.UserName = userName;
                client.ClientCredentials.UserName.Password = userPassword;
            }

            return(ClientFactory.CreateClient <ProvisioningServiceClient, ProvisioningService>(client));
        }
Exemplo n.º 27
0
        internal static ReportingV200810ClientProxy.ReportServiceClient ConstructReportServiceClientV200810(string endpointConfigName, string userName, string userPassword)
        {
            ReportingV200810ClientProxy.ReportServiceClient client = null;
            ChannelEndpointElement channelEE = _configDictionary[typeof(ReportingV200810ClientProxy.IReportService).FullName].Find(delegate(ChannelEndpointElement cee) { return(cee.Name == endpointConfigName); });

            if (null != channelEE)
            {
                client = new ReportingV200810ClientProxy.ReportServiceClient(endpointConfigName, channelEE.Address.ToString());
                client.ClientCredentials.UserName.UserName = userName;
                client.ClientCredentials.UserName.Password = userPassword;
            }

            return(client);
        }
Exemplo n.º 28
0
        protected override ServiceEndpoint CreateDescription()
        {
            ServiceEndpoint serviceEndpoint = base.CreateDescription();

            serviceEndpoint.Name = string.IsNullOrEmpty(this.endpointConfigurationName)
                                       ? string.Empty
                                       : this.endpointConfigurationName;
            Configuration config =
                ConfigurationManager.OpenMappedExeConfiguration(
                    new ExeConfigurationFileMap {
                ExeConfigFilename = this.configurationPath
            },
                    ConfigurationUserLevel.None);
            ServiceModelSectionGroup sectionGroup           = ServiceModelSectionGroup.GetSectionGroup(config);
            ChannelEndpointElement   channelEndpointElement = null;

            if (sectionGroup != null)
            {
                foreach (ChannelEndpointElement channelEndpointElement2 in sectionGroup.Client.Endpoints)
                {
                    if (channelEndpointElement2.Contract == serviceEndpoint.Contract.ConfigurationName &&
                        (this.endpointConfigurationName == null ||
                         this.endpointConfigurationName == channelEndpointElement2.Name))
                    {
                        channelEndpointElement = channelEndpointElement2;
                        break;
                    }
                }
                if (channelEndpointElement != null)
                {
                    if (serviceEndpoint.Binding == null)
                    {
                        serviceEndpoint.Binding = this.CreateBinding(channelEndpointElement.Binding, sectionGroup);
                    }
                    if (serviceEndpoint.Address == null)
                    {
                        serviceEndpoint.Address = new EndpointAddress(
                            channelEndpointElement.Address,
                            this.GetIdentity(channelEndpointElement.Identity),
                            channelEndpointElement.Headers.Headers);
                    }
                    if (serviceEndpoint.Behaviors.Count == 0 && channelEndpointElement.BehaviorConfiguration != null)
                    {
                        this.AddBehaviors(channelEndpointElement.BehaviorConfiguration, serviceEndpoint, sectionGroup);
                    }
                    serviceEndpoint.Name = channelEndpointElement.Contract;
                }
            }
            return(serviceEndpoint);
        }
Exemplo n.º 29
0
        public static IClientChannel GetClientConfigChannel <T>(string name, string host = "")
        {
            ClientSection clientSection = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;

            if (clientSection == null)
            {
                throw new Exception("Errore nella configurazione WCF. Aggiungere la sezione system.serviceModel/client.");
            }

            ChannelEndpointElement endpoint = clientSection.Endpoints.OfType <ChannelEndpointElement>().FirstOrDefault(x => x.Name == name);

            if (endpoint == null)
            {
                throw new Exception($"Errore nella configurazione WCF. Aggiungere la sezione endpoint con chiave: {name}.");
            }

            Binding binding = null;

            switch (endpoint.Binding)
            {
            case "basicHttpBinding":
                binding = (BasicHttpBinding)GetBinding(typeof(BasicHttpBinding), endpoint.BindingConfiguration);
                break;

            case "wsHttpBinding":
                binding = (WSHttpBinding)GetBinding(typeof(WSHttpBinding), endpoint.BindingConfiguration);
                break;

            case "netNamedPipeBinding":
                binding = (NetNamedPipeBinding)GetBinding(typeof(NetNamedPipeBinding), endpoint.BindingConfiguration);
                break;

            case "netTcpBinding":
                binding = (NetTcpBinding)GetBinding(typeof(NetTcpBinding), endpoint.BindingConfiguration);
                break;

            default:
                throw new Exception($"Errore nella configurazione WCF. Binding non supportato: {endpoint.Binding}.");
            }

            string address = endpoint.Address.ToString();

            if (!string.IsNullOrEmpty(host))
            {
                string hostName = endpoint.Address.Host;
                address = address.Replace(hostName, host);
            }
            return((IClientChannel)CreateChannelFactory <T>(address, binding).CreateChannel());
        }
Exemplo n.º 30
0
        public void AddMachineConfigurationInfo()
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenMachineConfiguration();
            Debug.Assert(config != null, "Machine.Config returned null");
            ServiceModelSectionGroup sectionGroup = config.GetSectionGroup("system.serviceModel") as ServiceModelSectionGroup;

            if (sectionGroup != null)
            {
                bool          channelEndpointElementExists = false;
                ClientSection clientSection = sectionGroup.Client;
                foreach (ChannelEndpointElement elem in clientSection.Endpoints)
                {
                    if (elem.Binding.Equals(BINDING_NAME, StringComparison.OrdinalIgnoreCase) && elem.Name.Equals(BINDING_SCHEME, StringComparison.OrdinalIgnoreCase) && elem.Contract.Equals("IMetadataExchange", StringComparison.OrdinalIgnoreCase))
                    {
                        channelEndpointElementExists = true;
                        break;
                    }
                }
                if (!channelEndpointElementExists)
                {
                    Debug.WriteLine("Adding ChannelEndpointElement for : " + BINDING_NAME);
                    ChannelEndpointElement elem = new ChannelEndpointElement();
                    elem.Binding  = BINDING_NAME;
                    elem.Name     = BINDING_SCHEME;
                    elem.Contract = "IMetadataExchange";
                    sectionGroup.Client.Endpoints.Add(elem);
                    Debug.WriteLine("Added ChannelEndpointElement for : " + BINDING_NAME);
                }

                if (!sectionGroup.Extensions.BindingElementExtensions.ContainsKey(BINDINGELEM_NAME))
                {
                    ExtensionElement ext = new ExtensionElement(BINDINGELEM_NAME, bindingElementExtensionType.FullName + "," + bindingElementExtensionType.Assembly.FullName);
                    sectionGroup.Extensions.BindingElementExtensions.Add(ext);
                }

                if (!sectionGroup.Extensions.BindingExtensions.ContainsKey(BINDING_NAME))
                {
                    ExtensionElement ext = new ExtensionElement(BINDING_NAME, bindingSectionType.FullName + "," + bindingSectionType.Assembly.FullName);
                    sectionGroup.Extensions.BindingExtensions.Add(ext);
                }

                config.Save();
            }
            else
            {
                throw new InstallException("Machine.Config doesn't contain system.serviceModel node");
            }
        }