Exemple #1
0
        protected virtual void ApplyConfiguration(string endpointConfig)
        {
            if (endpointConfig == null)
            {
                return;
            }

#if NET_2_1
            try {
                // It should automatically use XmlXapResolver
                var cfg = new SilverlightClientConfigLoader().Load(XmlReader.Create("ServiceReferences.ClientConfig"));

                SilverlightClientConfigLoader.ServiceEndpointConfiguration se = null;
                if (endpointConfig == "*")
                {
                    se = cfg.GetServiceEndpointConfiguration(Endpoint.Contract.Name);
                }
                if (se == null)
                {
                    se = cfg.GetServiceEndpointConfiguration(endpointConfig);
                }

                if (se.Binding != null && Endpoint.Binding == null)
                {
                    Endpoint.Binding = se.Binding;
                }
                else                 // ignore it
                {
                    Console.WriteLine("WARNING: Configured binding not found in configuration {0}", endpointConfig);
                }
                if (se.Address != null && Endpoint.Address == null)
                {
                    Endpoint.Address = se.Address;
                }
                else                 // ignore it
                {
                    Console.WriteLine("WARNING: Configured endpoint address not found in configuration {0}", endpointConfig);
                }
            } catch (Exception) {
                // ignore it.
                Console.WriteLine("WARNING: failed to load endpoint configuration for {0}", endpointConfig);
            }
#else
            string                 contractName = Endpoint.Contract.ConfigurationName;
            ClientSection          client       = ConfigUtil.ClientSection;
            ChannelEndpointElement res          = null;
            foreach (ChannelEndpointElement el in client.Endpoints)
            {
                if (el.Contract == contractName && (endpointConfig == el.Name || endpointConfig == "*"))
                {
                    if (res != null)
                    {
                        throw new InvalidOperationException(String.Format("More then one endpoint matching contract {0} was found.", contractName));
                    }
                    res = el;
                }
            }

            if (res == null)
            {
                throw new InvalidOperationException(String.Format("Client endpoint configuration '{0}' was not found in {1} endpoints.", endpointConfig, client.Endpoints.Count));
            }

            if (Endpoint.Binding == null)
            {
                Endpoint.Binding = ConfigUtil.CreateBinding(res.Binding, res.BindingConfiguration);
            }
            if (Endpoint.Address == null)
            {
                Endpoint.Address = new EndpointAddress(res.Address);
            }

            if (res.BehaviorConfiguration != "")
            {
                ApplyBehavior(res.BehaviorConfiguration);
            }
#endif
        }
        protected virtual void ApplyConfiguration(string endpointConfig)
        {
            if (endpointConfig == null)
            {
                return;
            }

#if NET_2_1 || XAMMAC_4_5
            try {
                // It should automatically use XmlXapResolver
                var cfg = new SilverlightClientConfigLoader().Load(XmlReader.Create("ServiceReferences.ClientConfig"));

                SilverlightClientConfigLoader.ServiceEndpointConfiguration se = null;
                if (endpointConfig == "*")
                {
                    se = cfg.GetServiceEndpointConfiguration(Endpoint.Contract.Name);
                }
                if (se == null)
                {
                    se = cfg.GetServiceEndpointConfiguration(endpointConfig);
                }

                if (se.Binding != null && Endpoint.Binding == null)
                {
                    Endpoint.Binding = se.Binding;
                }
                else                 // ignore it
                {
                    Console.WriteLine("WARNING: Configured binding not found in configuration {0}", endpointConfig);
                }
                if (se.Address != null && Endpoint.Address == null)
                {
                    Endpoint.Address = se.Address;
                }
                else                 // ignore it
                {
                    Console.WriteLine("WARNING: Configured endpoint address not found in configuration {0}", endpointConfig);
                }
            } catch (Exception) {
                // ignore it.
                Console.WriteLine("WARNING: failed to load endpoint configuration for {0}", endpointConfig);
            }
#else
            string                 contractName = Endpoint.Contract.ConfigurationName;
            ClientSection          client       = ConfigUtil.ClientSection;
            ChannelEndpointElement endpoint     = null;

            foreach (ChannelEndpointElement el in client.Endpoints)
            {
                if (el.Contract == contractName && (endpointConfig == el.Name || endpointConfig == "*"))
                {
                    if (endpoint != null)
                    {
                        throw new InvalidOperationException(String.Format("More then one endpoint matching contract {0} was found.", contractName));
                    }
                    endpoint = el;
                }
            }

            if (endpoint == null)
            {
                throw new InvalidOperationException(String.Format("Client endpoint configuration '{0}' was not found in {1} endpoints.", endpointConfig, client.Endpoints.Count));
            }

            var binding      = String.IsNullOrEmpty(endpoint.Binding) ? null : ConfigUtil.CreateBinding(endpoint.Binding, endpoint.BindingConfiguration);
            var contractType = ConfigUtil.GetTypeFromConfigString(endpoint.Contract, NamedConfigCategory.Contract);
            if (contractType == null)
            {
                throw new ArgumentException(String.Format("Contract '{0}' was not found", endpoint.Contract));
            }
            var contract = String.IsNullOrEmpty(endpoint.Contract) ? Endpoint.Contract : ContractDescription.GetContract(contractType);

            if (!String.IsNullOrEmpty(endpoint.Kind))
            {
                var se = ConfigUtil.ConfigureStandardEndpoint(contract, endpoint);
                if (se.Binding == null)
                {
                    se.Binding = binding;
                }
                if (se.Address == null && se.Binding != null)                 // standard endpoint might have empty address
                {
                    se.Address = new EndpointAddress(endpoint.Address);
                }
                if (se.Binding == null && se.Address != null)                 // look for protocol mapping
                {
                    se.Binding = ConfigUtil.GetBindingByProtocolMapping(se.Address.Uri);
                }

                service_endpoint = se;
            }
            else
            {
                if (binding == null && endpoint.Address != null)                 // look for protocol mapping
                {
                    Endpoint.Binding = ConfigUtil.GetBindingByProtocolMapping(endpoint.Address);
                }
            }
            if (Endpoint.Binding == null)
            {
                Endpoint.Binding = ConfigUtil.CreateBinding(endpoint.Binding, endpoint.BindingConfiguration);
            }
            if (Endpoint.Address == null)
            {
                Endpoint.Address = new EndpointAddress(endpoint.Address);
            }

            if (endpoint.BehaviorConfiguration != "")
            {
                ApplyBehavior(endpoint.BehaviorConfiguration);
            }
#endif
        }