Example #1
0
        /// <summary>
        /// Create channel factory from service config db
        /// </summary>
        /// <typeparam name="T">The service contract</typeparam>
        /// <returns>The built channel factory</returns>
        private static ChannelFactory <T> CreateChannelFactory <T>()
        {
            var config = ServiceConfigurationStore.GetClientConfiguration(typeof(T));

            if (config != null)
            {
                string[] baseAddresses = null;
                if (!string.IsNullOrEmpty(config.HostXML))
                {
                    var hostElement = new HostElement();
                    hostElement.DeserializeElement(config.HostXML);
                    baseAddresses = WcfServiceHelper.GetBaseAddressesFromHostElement(hostElement);
                }

                if (config.Endpoint != null)
                {
                    var binding = WcfServiceHelper.GetBinding(config.Endpoint);
                    if (binding != null)
                    {
                        var address = WcfServiceHelper.BuildEndpointAddress(config.Endpoint, baseAddresses);
                        address = string.Format(address, config.Endpoint.FarmAddress);
                        var cf = new ChannelFactory <T>(binding, address);
                        WcfServiceHelper.ApplyEndpointBehaviorConfiguration(cf.Endpoint, config.Endpoint);
                        return(cf);
                    }
                }
                else
                {
                    throw new ConfigurationErrorsException("Could not find any endpoint for specified service contract - " + typeof(T).GetQualifiedTypeName());
                }
            }

            return(null);
        }
Example #2
0
        internal static Binding GetBinding(EndpointConfiguration endpoint)
        {
            var bindingTypeDesc = ServiceConfigurationStore.GetBindingType(endpoint.BindingType_id);
            var bindingType     = Type.GetType(bindingTypeDesc.ClassName);

            if (bindingType == null)
            {
                throw new ConfigurationErrorsException(string.Format("Specified binding type - {0} could not be loaded!", bindingTypeDesc.ClassName));
            }
            var binding = Activator.CreateInstance(bindingType) as Binding;

            if (binding == null)
            {
                throw new ConfigurationErrorsException(string.Format("Specified binding type - {0} could not be initialized!", bindingTypeDesc.ClassName));
            }

            var bindingElementType = Type.GetType(bindingTypeDesc.ConfigurationElementTypeClassName);

            if (bindingElementType == null)
            {
                throw new ConfigurationErrorsException(string.Format("Specified binding configuration element type - {0} could not be loaded!", bindingTypeDesc.ConfigurationElementTypeClassName));
            }
            var bindingElement = Activator.CreateInstance(bindingElementType) as StandardBindingElement;

            if (bindingElement == null)
            {
                throw new ConfigurationErrorsException(string.Format("Specified binding binding configuration element type - {0} could not be initialized!", bindingTypeDesc.ConfigurationElementTypeClassName));
            }
            bindingElement.DeserializeElement(endpoint.BindingXML);
            bindingElement.ApplyConfiguration(binding);

            return(binding);
        }
        private static void ApplyServiceBehaviorConfiguration(ServiceHost serviceHost, ServiceConfiguration config)
        {
            if (!string.IsNullOrEmpty(config.ServiceBehaviorXML))
            {
                var doc = new XmlDocument();
                doc.LoadXml(config.ServiceBehaviorXML);
                var customBehaviorElements = new List <BehaviorExtensionElement>();
                foreach (XmlNode node in doc.FirstChild.ChildNodes)
                {
                    var customBehaviorTypeDesc = ServiceConfigurationStore.GetCustomBehaviorType(node.Name);
                    if (customBehaviorTypeDesc != null)
                    {
                        var customBehaviorElementType =
                            Type.GetType(customBehaviorTypeDesc.ConfigurationElementTypeClassName);
                        if (customBehaviorElementType == null)
                        {
                            throw new ConfigurationErrorsException(
                                      string.Format(
                                          "Specified service behavior configuration element type - {0} could not be loaded!",
                                          customBehaviorTypeDesc.ConfigurationElementTypeClassName));
                        }
                        var customBehaviorElement =
                            Activator.CreateInstance(customBehaviorElementType) as BehaviorExtensionElement;
                        if (customBehaviorElement == null)
                        {
                            throw new ConfigurationErrorsException(
                                      string.Format(
                                          "Specified service behavior configuration element type - {0} could not be initialized!",
                                          customBehaviorTypeDesc.ConfigurationElementTypeClassName));
                        }
                        customBehaviorElement.DeserializeElement(node.OuterXml);
                        customBehaviorElements.Add(customBehaviorElement);
                        node.ParentNode.RemoveChild(node);
                    }
                }
                var serviceBehaviorElement = new ServiceBehaviorElement();
                serviceBehaviorElement.DeserializeElement(doc.OuterXml);
                foreach (var item in serviceBehaviorElement)
                {
                    serviceHost.Description.Behaviors.Add(item.CreateServiceBehavior());
                }
                foreach (var item in customBehaviorElements)
                {
                    serviceHost.Description.Behaviors.Add(item.CreateServiceBehavior());
                }
            }

            if (!IsBehaviorConfigured <ServiceMetadataBehavior>(serviceHost))
            {
                var smb = new ServiceMetadataBehavior();
                serviceHost.Description.Behaviors.Add(smb);
            }
        }
Example #4
0
        internal static void ApplyEndpointBehaviorConfiguration(ServiceEndpoint endpoint, EndpointConfiguration config)
        {
            if (string.IsNullOrEmpty(config.EndpointBehaviorXML))
            {
                return;
            }

            var doc = new XmlDocument();

            doc.LoadXml(config.EndpointBehaviorXML);
            var customBehaviorElements = new List <BehaviorExtensionElement>();

            foreach (XmlNode node in doc.FirstChild.ChildNodes)
            {
                var customBehaviorTypeDesc = ServiceConfigurationStore.GetCustomBehaviorType(node.Name);
                if (customBehaviorTypeDesc != null)
                {
                    var customBehaviorElementType = Type.GetType(customBehaviorTypeDesc.ConfigurationElementTypeClassName);
                    if (customBehaviorElementType == null)
                    {
                        throw new ConfigurationErrorsException(string.Format("Specified endpoint behavior configuration element type - {0} could not be loaded!", customBehaviorTypeDesc.ConfigurationElementTypeClassName));
                    }
                    var customBehaviorElement = Activator.CreateInstance(customBehaviorElementType) as BehaviorExtensionElement;
                    if (customBehaviorElement == null)
                    {
                        throw new ConfigurationErrorsException(string.Format("Specified endpoint behavior configuration element type - {0} could not be initialized!", customBehaviorTypeDesc.ConfigurationElementTypeClassName));
                    }
                    customBehaviorElement.DeserializeElement(node.OuterXml);
                    customBehaviorElements.Add(customBehaviorElement);
                    node.ParentNode.RemoveChild(node);
                }
            }
            var endpointBehaviorElement = new EndpointBehaviorElement();

            endpointBehaviorElement.DeserializeElement(doc.OuterXml);
            foreach (var item in endpointBehaviorElement)
            {
                endpoint.Behaviors.Add(item.CreateEndpointBehavior());
            }
            foreach (var item in customBehaviorElements)
            {
                endpoint.Behaviors.Add(item.CreateEndpointBehavior());
            }
        }
        private ServiceHost GetServiceHost(ServiceConfiguration config, Type serviceImplType, object singleton, Uri[] baseAddresses)
        {
            var serviceHostTypeDesc = ServiceConfigurationStore.GetServiceHostType(config.ServiceHostType_id);
            var serviceHostType     = Type.GetType(serviceHostTypeDesc.ClassName);

            if (serviceHostType == null)
            {
                throw new ConfigurationErrorsException(string.Format("Specified service host type - {0} could not be loaded!", serviceHostTypeDesc.ClassName));
            }
            baseAddresses = BuildBaseAddresses(baseAddresses, config);
            var serviceHost = (singleton == null ?
                               Activator.CreateInstance(serviceHostType, new object[] { serviceImplType, baseAddresses }) as ServiceHost :
                               Activator.CreateInstance(serviceHostType, new[] { singleton, baseAddresses }) as ServiceHost
                               );

            if (serviceHost == null)
            {
                throw new ConfigurationErrorsException(string.Format("Specified service host type - {0} could not be initialized!", serviceHostTypeDesc.ClassName));
            }

            return(serviceHost);
        }
Example #6
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var serviceCriteria = new ServiceCriteria();
                dsService.Criteria = serviceCriteria.AddSortBy(serviceCriteria.ServiceName, false);

                var bindingCriteria = new BindingCriteria();
                dsBinding.Criteria = bindingCriteria.AddSortBy(bindingCriteria.BindingName, false);

                var bindingTypeCriteria = new BindingTypeCriteria();
                dsBindingType.Criteria = bindingTypeCriteria.AddSortBy(bindingTypeCriteria.BindingTypeFriendlyName, false);

                var endpointCriteria = new EndpointCriteria();
                dsEndpoint.Criteria = endpointCriteria;

                using (var serviceLocator = ServiceManager.GetServiceLocator(typeof(ISimpleServiceDemo)))
                {
                    litSayHello.Text = serviceLocator.GetService <ISimpleServiceDemo>().SayHellod();
                    var clientConfig = ServiceConfigurationStore.GetClientConfiguration(typeof(ISimpleServiceDemo));
                    litSayHello.Text += string.Format(" [Binding Type: {0}]", ServiceConfigurationStore.GetBindingType(clientConfig.Endpoint.BindingType_id).FriendlyName);
                }
            }
        }
Example #7
0
        private static string GetBaseAddress(IEnumerable baseAddresses, EndpointConfiguration endpointConfig)
        {
            var channelType   = ServiceConfigurationStore.GetBindingType(endpointConfig.BindingType_id).ChannelType;
            var addressPrefix = "http";

            switch (channelType)
            {
            case ChannelType.HTTP:
                addressPrefix = "http";
                break;

            case ChannelType.TCP:
                addressPrefix = "net.tcp";
                break;

            case ChannelType.IPC:
                addressPrefix = "net.pipe";
                break;

            case ChannelType.MSMQ:
                addressPrefix = "net.msmq";
                break;
            }
            if (baseAddresses != null)
            {
                foreach (var item in baseAddresses)
                {
                    if (item.ToString().ToLowerInvariant().StartsWith(addressPrefix))
                    {
                        return(item.ToString());
                    }
                }
            }

            throw new ConfigurationErrorsException("Could not find a base address in configuration store for channel type - " + channelType);
        }
        /// <summary>
        /// Create service host
        /// </summary>
        /// <param name="serviceType">The service implementation type</param>
        /// <param name="baseAddresses">
        ///     The baseAddress are always ignored because we could get the
        ///     addresses from the endpoint config
        /// </param>
        /// <returns></returns>
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException("serviceType");
            }

            var         config      = ServiceConfigurationStore.GetServiceConfiguration(serviceType.GetQualifiedTypeName());
            HostElement hostElement = null;

            if (!string.IsNullOrEmpty(config.HostXML))
            {
                hostElement = new HostElement();
                hostElement.DeserializeElement(config.HostXML);
                if (baseAddresses == null || baseAddresses.Length == 0)
                {
                    var baseAddressTemplates = WcfServiceHelper.GetBaseAddressesFromHostElement(hostElement);
                    if (baseAddressTemplates != null && baseAddressTemplates.Length > 0)
                    {
                        baseAddresses = new Uri[baseAddressTemplates.Length];
                        for (var i = 0; i < baseAddressTemplates.Length; ++i)
                        {
                            baseAddresses[i] = new Uri(string.Format(baseAddressTemplates[i], Environment.MachineName.ToLowerInvariant()));
                        }
                    }
                }
            }
            if (baseAddresses == null || baseAddresses.Length == 0)
            {
                baseAddresses = GetBaseAddressesFromEndpoints(config.Endpoints);
            }
            object singleton;
            var    serviceImplType = ServiceManager.GetServiceImplementationType(serviceType, out singleton);
            var    serviceHost     = GetServiceHost(config, serviceImplType, singleton, baseAddresses);

            ApplyServiceHostConfiguration(serviceHost, hostElement);
            ApplyServiceBehaviorConfiguration(serviceHost, config);

            var bindingCache    = new Dictionary <string, Binding>();
            var mexBindingCache = new Dictionary <string, Binding>();

            foreach (var endpointConfig in config.Endpoints)
            {
                var address = endpointConfig.ListenUri;
                address = (address == null ? string.Empty : string.Format(address, Environment.MachineName.ToLowerInvariant()));
                var serviceContract = Type.GetType(endpointConfig.ServiceContract);
                if (serviceContract == null)
                {
                    throw new ConfigurationErrorsException(string.Format("Specified service contract - {0} could not be loaded!", endpointConfig.ServiceContract));
                }

                var cacheKey = WcfServiceHelper.BuildEndpointAddress(endpointConfig, baseAddresses);

                Binding binding;
                bindingCache.TryGetValue(cacheKey, out binding);
                if (binding == null)
                {
                    binding = WcfServiceHelper.GetBinding(endpointConfig);
                    bindingCache.Add(cacheKey, binding);
                }

                if (binding == null)
                {
                    continue;
                }

                if (endpointConfig.MexBindingEnabled)
                {
                    Binding mexBinding;
                    mexBindingCache.TryGetValue(cacheKey, out mexBinding);
                    if (mexBinding == null)
                    {
                        mexBinding = new CustomBinding(binding);
                        mexBindingCache.Add(cacheKey, mexBinding);
                        serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), mexBinding, "mex");
                    }
                }

                if (!endpointConfig.AddMexBindingOnly)
                {
                    ServiceEndpoint serviceEndpoint;
                    if (endpointConfig.EndpointAddress != endpointConfig.ListenUri)
                    {
                        serviceEndpoint = serviceHost.AddServiceEndpoint(
                            serviceContract, binding,
                            endpointConfig.EndpointAddress, new Uri(address));
                    }
                    else
                    {
                        serviceEndpoint = serviceHost.AddServiceEndpoint(
                            serviceContract, binding, address);
                    }
                    if (endpointConfig.ListenUriMode.HasValue)
                    {
                        serviceEndpoint.ListenUriMode = (ListenUriMode)Enum.Parse(
                            typeof(ListenUriMode),
                            endpointConfig.ListenUriMode.ToString());
                    }

                    WcfServiceHelper.ApplyEndpointBehaviorConfiguration(serviceEndpoint, endpointConfig);
                }
            }

            return(serviceHost);
        }