/// <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); }
/// <summary> /// Build base addresses from endpoints /// </summary> /// <param name="baseAddresses">The baseAddresses passed in from outside</param> /// <param name="config">The service configuration</param> /// <returns>The base addresses</returns> protected virtual Uri[] BuildBaseAddresses(Uri[] baseAddresses, ServiceConfiguration config) { if (config == null) { throw new ArgumentNullException("config"); } var list = new List <Uri>(); foreach (var endpoint in config.Endpoints) { var address = WcfServiceHelper.BuildEndpointAddress(endpoint, baseAddresses); address = string.Format(address, Environment.MachineName.ToLowerInvariant()); var uri = new Uri(address); if (!list.Contains(uri)) { list.Add(uri); } } return(list.ToArray()); }
/// <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); }
/// <summary> /// To be overriden to get service contracts from /// the service type specified in .svc file /// </summary> /// <param name="type">The service type specified in .svc file</param> /// <returns></returns> protected virtual IList <Type> GetServiceContractTypes(Type type) { return(WcfServiceHelper.GetServiceContracts(type)); }