Beispiel #1
0
        void ApplyServiceElement(ServiceElement service)
        {
            //base addresses
            HostElement host = service.Host;

            foreach (BaseAddressElement baseAddress in host.BaseAddresses)
            {
                AddBaseAddress(new Uri(baseAddress.BaseAddress));
            }

            // behaviors
            AddServiceBehaviors(service.BehaviorConfiguration, true);

            // endpoints
            foreach (ServiceEndpointElement endpoint in service.Endpoints)
            {
                ServiceEndpoint se;

                var binding = String.IsNullOrEmpty(endpoint.Binding) ? null : ConfigUtil.CreateBinding(endpoint.Binding, endpoint.BindingConfiguration);

                if (!String.IsNullOrEmpty(endpoint.Kind))
                {
                    var contract = String.IsNullOrEmpty(endpoint.Contract) ? null : GetContract(endpoint.Contract, false);
                    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(CreateUri(se.Binding.Scheme, endpoint.Address));
                    }
                    if (se.Binding == null && se.Address != null)                     // look for protocol mapping
                    {
                        se.Binding = ConfigUtil.GetBindingByProtocolMapping(se.Address.Uri);
                    }

                    AddServiceEndpoint(se);
                }
                else
                {
                    if (binding == null && endpoint.Address != null)                     // look for protocol mapping
                    {
                        binding = ConfigUtil.GetBindingByProtocolMapping(endpoint.Address);
                    }
                    se = AddServiceEndpoint(endpoint.Contract, binding, endpoint.Address);
                }

                // endpoint behaviors
                EndpointBehaviorElement epbehavior = ConfigUtil.BehaviorsSection.EndpointBehaviors [endpoint.BehaviorConfiguration];
                if (epbehavior != null)
                {
                    foreach (var bxe in epbehavior)
                    {
                        IEndpointBehavior b = (IEndpointBehavior)bxe.CreateBehavior();
                        se.Behaviors.Add(b);
                    }
                }
            }
        }
        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
        }