Esempio n. 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);
                    }
                }
            }
        }
Esempio n. 2
0
        private static string[] GetBaseAddresses(ServiceDeploymentConfiguration item)
        {
            var host = new HostElement();

            host.DeserializeElement(item.HostXML);
            return(WcfServiceHelper.GetBaseAddressesFromHostElement(host));
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
 internal static HandlerProvider GetOrCreate(ExpertInfo expertInfo, HostElement hostElement)
 {
     lock (HandlerProvider._storageLocker)
     {
         if (HandlerProvider._storage.ContainsKey(expertInfo.Discriminator))
         {
             return(HandlerProvider._storage[expertInfo.Discriminator]);
         }
         if (!hostElement.Handlers.ContainsKey(expertInfo.HandlerName))
         {
             throw new HandlerLoadException(expertInfo, "Requested application not found in configuration", (Exception)null);
         }
         HandlerElement local_0 = hostElement.Handlers[expertInfo.HandlerName];
         Assembly       local_1;
         try
         {
             local_1 = Assembly.LoadFile(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + local_0.AssemblyName + ".dll");
         }
         catch (Exception exception_3)
         {
             throw new HandlerLoadException(expertInfo, "Requested assembly not found", exception_3);
         }
         Type local_3;
         try
         {
             local_3 = local_1.GetType(local_0.TypeName);
         }
         catch (Exception exception_2)
         {
             throw new HandlerLoadException(expertInfo, "Requested type not found in assembly.", exception_2);
         }
         MqlHandler local_5;
         try
         {
             local_5 = (MqlHandler)Activator.CreateInstance(local_3);
         }
         catch (Exception exception_1)
         {
             throw new HandlerLoadException(expertInfo, "Can't create intance of expert.", exception_1);
         }
         try
         {
             foreach (ParameterElement item_0 in (ConfigurationElementCollection)local_0.InputParameters)
             {
                 PropertyInfo local_8  = local_5.GetType().GetProperty(item_0.PropertyName);
                 Type         local_9  = local_8.PropertyType;
                 object       local_10 = Convert.ChangeType((object)item_0.PropertyValue, local_9);
                 local_8.SetValue((object)local_5, local_10);
             }
         }
         catch (Exception exception_0)
         {
             throw new HandlerLoadException(expertInfo, "Can't set input parameters for expert", exception_0);
         }
         local_5.Discriminator = expertInfo.Discriminator;
         HandlerProvider local_12 = new HandlerProvider(local_5, local_0, expertInfo);
         HandlerProvider._storage.Add(expertInfo.Discriminator, local_12);
         return(local_12);
     }
 }
Esempio n. 5
0
 public static HostPlainConf ToPlainConf(this HostElement element)
 {
     return(new HostPlainConf(
                element.ExternalHostName,
                element.DomesticHostName,
                element.EndPoint.ToIPEndPoint(),
                element.CertificateId));
 }
Esempio n. 6
0
 private static void ApplyServiceHostConfiguration(ServiceHost serviceHost, HostElement hostElement)
 {
     if (hostElement != null && hostElement.Timeouts != null)
     {
         serviceHost.OpenTimeout  = hostElement.Timeouts.OpenTimeout;
         serviceHost.CloseTimeout = hostElement.Timeouts.CloseTimeout;
     }
 }
Esempio n. 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="host_element"></param>
        public HostInfo(HostElement host_element)
        {
            productId = host_element.productId;

            domain   = host_element.domain;
            hostName = host_element.hostName;

            ipAddress  = host_element.ipAddress;
            macAddress = host_element.macAddress;
        }
Esempio n. 8
0
        /// <summary>
        /// Returns a reference to the specified entry in the output cache.
        /// </summary>
        /// <param name="key">A unique identifier for a cached entry in the output cache.</param>
        /// <returns>
        /// The <paramref name="key"/> entry that identifies the specified entry in the cache, or null if the specified entry is not in the cache.
        /// </returns>
        public override object Get(string key)
        {
            if (RequestBlackList(key))
            {
                return(null);
            }

            HostElement hostElement =
                _slaves.FirstOrDefault() ??
                new HostElement
            {
                Host     = _host,
                Port     = _port,
                Password = _password
            };

            string password = hostElement.Password;
            string host     = hostElement.Host;
            int    port     = hostElement.Port;

            RedisClient redis = null;

            try
            {
                redis = !String.IsNullOrEmpty(password) ? new RedisClient(host, port, password) : new RedisClient(host, port);

                if (!redis.ContainsKey(key))
                {
                    return(null);
                }

                var cacheItem = redis.Get <CacheItem>(key);

                if (cacheItem == null || cacheItem.Item == null)
                {
                    return(null);
                }

                return(SerializerHelper.Deserialize(cacheItem.Item));
            }
            finally
            {
                if (redis != null)
                {
                    redis.Dispose();
                }
            }
        }
Esempio n. 9
0
        internal static string[] GetBaseAddressesFromHostElement(HostElement hostElement)
        {
            var list = new List <string>();

            if (hostElement != null && hostElement.BaseAddresses != null)
            {
                for (var i = 0; i < hostElement.BaseAddresses.Count; ++i)
                {
                    if (!list.Contains(hostElement.BaseAddresses[i].BaseAddress))
                    {
                        list.Add(hostElement.BaseAddresses[i].BaseAddress);
                    }
                }
            }
            return(list.ToArray());
        }
Esempio n. 10
0
        /// <summary>
        /// Applies the host timeouts configuration.
        /// </summary>
        /// <param name="host">The host.</param>
        public void ApplyHostTimeoutsConfiguration(ServiceHostBase host)
        {
            if (host == null)
            {
                return;
            }

            if (_element == null)
            {
                lock (SyncLock)
                {
                    if (_element == null)
                    {
                        _element = new HostElement();
                        Deserialize(_element);
                    }
                }
            }

            host.OpenTimeout  = _element.Timeouts.OpenTimeout;
            host.CloseTimeout = _element.Timeouts.CloseTimeout;
        }
Esempio n. 11
0
        private object Retrieve(Func <IRedisClient, string, object> redisCommand, string key)
        {
            HostElement hostElement =
                _slaves.FirstOrDefault() ??
                new HostElement
            {
                Host     = _host,
                Port     = _port,
                Password = _password
            };
            string password = hostElement.Password;
            string host     = hostElement.Host;
            int    port     = hostElement.Port;

            var readOnlyHosts = !String.IsNullOrEmpty(password) ?
                                String.Format("{0}@{1}:{2}", password, host, port) :
                                String.Format("{0}:{1}", host, port);

            if (Environment.MachineName.Equals(host))
            {
                using (IRedisClientsManager redisManager = new BasicRedisClientManager(readOnlyHosts))
                {
                    using (IRedisClient client = redisManager.GetClient())
                    {
                        return(redisCommand.Invoke(client, key));
                    }
                }
            }

            using (var redisManager = new PooledRedisClientManager(readOnlyHosts))
            {
                using (IRedisClient client = redisManager.GetClient())
                {
                    return(redisCommand.Invoke(client, key));
                }
            }
        }
Esempio n. 12
0
 /// <summary>
 /// This method is called by the bootstrapper when the application object is ready to be created and rendered.
 /// </summary>
 public virtual void Create()
 {
     HostElement.Append(RootView.UnderlyingElement); //Add the root view to the host element
 }
Esempio n. 13
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);
            }
        }
Esempio n. 14
0
 private void Deserialize()
 {
     _element = new HostElement();
     Deserialize(_element);
 }
Esempio n. 15
0
        internal static HandlerProvider GetOrCreate(TradePlatform.MT4.Core.Utils.ExpertInfo expertInfo, HostElement hostElement)
        {
            Assembly        assembly;
            Type            type;
            MqlHandler      mqlHandler;
            HandlerProvider item;

            lock (HandlerProvider._storageLocker)
            {
                if (!HandlerProvider._storage.ContainsKey(expertInfo.Discriminator))
                {
                    if (hostElement.Handlers.ContainsKey(expertInfo.HandlerName))
                    {
                        HandlerElement handlerElement = hostElement.Handlers[expertInfo.HandlerName];
                        try
                        {
                            assembly = Assembly.LoadFile(string.Concat(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "\\", handlerElement.AssemblyName, ".dll"));
                        }
                        catch (Exception exception1)
                        {
                            Exception exception = exception1;
                            throw new HandlerLoadException(expertInfo, "Requested assembly not found", exception);
                        }
                        try
                        {
                            type = assembly.GetType(handlerElement.TypeName);
                        }
                        catch (Exception exception3)
                        {
                            Exception exception2 = exception3;
                            throw new HandlerLoadException(expertInfo, "Requested type not found in assembly.", exception2);
                        }
                        try
                        {
                            mqlHandler = (MqlHandler)Activator.CreateInstance(type);
                        }
                        catch (Exception exception5)
                        {
                            Exception exception4 = exception5;
                            throw new HandlerLoadException(expertInfo, "Can't create intance of expert.", exception4);
                        }
                        try
                        {
                            foreach (ParameterElement inputParameter in handlerElement.InputParameters)
                            {
                                PropertyInfo property     = mqlHandler.GetType().GetProperty(inputParameter.PropertyName);
                                Type         propertyType = property.PropertyType;
                                object       obj          = Convert.ChangeType(inputParameter.PropertyValue, propertyType);
                                property.SetValue(mqlHandler, obj);
                            }
                        }
                        catch (Exception exception7)
                        {
                            Exception exception6 = exception7;
                            throw new HandlerLoadException(expertInfo, "Can't set input parameters for expert", exception6);
                        }
                        HandlerProvider handlerProvider = new HandlerProvider(mqlHandler, handlerElement, expertInfo);
                        HandlerProvider._storage.TryAdd(expertInfo.Discriminator, handlerProvider);
                        item = handlerProvider;
                    }
                    else
                    {
                        throw new HandlerLoadException(expertInfo, "Requested application not found in configuration", null);
                    }
                }
                else
                {
                    item = HandlerProvider._storage[expertInfo.Discriminator];
                }
            }
            return(item);
        }
Esempio n. 16
0
        /// <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);
        }
Esempio n. 17
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="host_info"></param>
 /// <param name="product_info"></param>
 public ServerInfo(HostElement host_info, ProductElement product_info)
 {
     host    = new HostInfo(host_info);
     product = new ProductInfo(product_info);
 }