Пример #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 override void OnApplyConfiguration(ServiceEndpoint endpoint, ServiceEndpointElement serviceEndpointElement)
        {
            if (endpoint == null)
            {
                throw new ArgumentNullException("endpoint");
            }
            AnnouncementEndpoint ae = (AnnouncementEndpoint)endpoint;

            if (!ae.DiscoveryVersion.Equals(DiscoveryVersion))
            {
                throw new ArgumentException("Argument AnnouncementEndpoint is initialized with different DiscoveryVersion");
            }
            ae.MaxAnnouncementDelay = MaxAnnouncementDelay;
            ae.Address = serviceEndpointElement.CreateEndpointAddress();                                                        // it depends on InternalVisibleTo(System.ServiceModel)
            ae.Binding = ConfigUtil.CreateBinding(serviceEndpointElement.Binding, serviceEndpointElement.BindingConfiguration); // it depends on InternalVisibleTo(System.ServiceModel)
        }
Пример #3
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
        }
Пример #5
0
        protected virtual void ApplyConfiguration()
        {
            if (Description == null)
            {
                throw new InvalidOperationException("ApplyConfiguration requires that the Description property be initialized. Either provide a valid ServiceDescription in the CreateDescription method or override the ApplyConfiguration method to provide an alternative implementation");
            }

            ServiceElement service = GetServiceElement();

            //TODO: Should we call here LoadServiceElement ?
            if (service != null)
            {
                //base addresses
                HostElement host = service.Host;
                foreach (BaseAddressElement baseAddress in host.BaseAddresses)
                {
                    AddBaseAddress(new Uri(baseAddress.BaseAddress));
                }

                // behaviors
                // TODO: use EvaluationContext of ServiceElement.
                ServiceBehaviorElement behavior = ConfigUtil.BehaviorsSection.ServiceBehaviors [service.BehaviorConfiguration];
                if (behavior != null)
                {
                    foreach (var bxe in behavior)
                    {
                        IServiceBehavior b = (IServiceBehavior)bxe.CreateBehavior();
                        Description.Behaviors.Add(b);
                    }
                }

                // services
                foreach (ServiceEndpointElement endpoint in service.Endpoints)
                {
                    // FIXME: consider BindingName as well
                    ServiceEndpoint se = AddServiceEndpoint(
                        endpoint.Contract,
                        ConfigUtil.CreateBinding(endpoint.Binding, endpoint.BindingConfiguration),
                        endpoint.Address.ToString());
                    // 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);
                        }
                    }
                }
            }
            // TODO: consider commonBehaviors here

            // ensure ServiceAuthorizationBehavior
            Authorization = Description.Behaviors.Find <ServiceAuthorizationBehavior> ();
            if (Authorization == null)
            {
                Authorization = new ServiceAuthorizationBehavior();
                Description.Behaviors.Add(Authorization);
            }

            // ensure ServiceDebugBehavior
            ServiceDebugBehavior debugBehavior = Description.Behaviors.Find <ServiceDebugBehavior> ();

            if (debugBehavior == null)
            {
                debugBehavior = new ServiceDebugBehavior();
                Description.Behaviors.Add(debugBehavior);
            }
        }
Пример #6
0
 public static ServiceEndpoint CreateEndpoint(this ChannelEndpointElement el)
 {
     // depends on System.ServiceModel internals (by InternalVisibleTo).
     // FIXME: is IRequestReplyRouter okay for every case?
     return(new ServiceEndpoint(ContractDescription.GetContract(typeof(IRequestReplyRouter)), ConfigUtil.CreateBinding(el.Binding, el.BindingConfiguration), new EndpointAddress(el.Address)));
 }