Beispiel #1
0
        /// <summary>
        /// Initializes the channel.
        /// </summary>
        /// <param name="pi">The pi.</param>
        public virtual void Initialize(CategoryPropertyItem pi)
        {
            DoDisposeCheck();
            if (pi == null)
            {
                ThrowHelper.ThrowArgumentNullException("pi");
            }

            if (!mInitialized)
            {
                {
                    this.mConnectionData = null;
                    CategoryPropertyItem item = ConfigurationAccessHelper.GetCategoryPropertyByPath(pi.PropertyItems, "RemoteAddress");
                    if (item != null)
                    {
                        this.mConnectionData = AddressEndPoint.Parse(item.EntryValue);
                    }
                    if (item != null)
                    {
                        ConfigurationAccessHelper.ParseLongValue(pi.PropertyItems, "DefaultErrorResponseTimeout", Timeout.Infinite, long.MaxValue, ref mDefaultErrorResponseTimeout);
                    }
                }
                {
                    mServerEndpoints.Clear();
                    CategoryPropertyItem baseAddressesItems = ConfigurationAccessHelper.GetCategoryPropertyByPath(pi.PropertyItems, "BaseAddresses");
                    if (baseAddressesItems != null)
                    {
                        IEnumerator <CategoryPropertyItem> iterator = baseAddressesItems.GetEnumerator();
                        while (iterator.MoveNext())
                        {
                            mServerEndpoints.Add(AddressEndPoint.Parse(iterator.Current.EntryValue));
                        }
                    }
                }
                {
                    mSessionReusable = true;
                    ConfigurationAccessHelper.ParseBooleanValue(pi.PropertyItems, "SessionReusable", ref mSessionReusable);
                }
            }
        }
Beispiel #2
0
        public static void Initialize()
        {
            if (!mInitialized)
            {
                Raiser.CallDelegatorBySync(EventInitialization, new object[] { null, new ServiceInitializationStateEventArgs(ServiceInitializationStateEnum.Before) });
                ChannelServices.Initialize();
                if (LOGGER.IsInfoEnabled)
                {
                    LOGGER.Info("Initializing ServiceBase services.");
                }

                CategoryPropertyItem pi = ConfigurationAccessHelper.GetCategoryPropertyByPath(RemotingConfiguration.Settings.CategoryPropertyItems, "Services");
                if (pi != null)
                {
                    IEnumerator <CategoryPropertyItem> iterator = pi.GetEnumerator();
                    try
                    {
                        while (iterator.MoveNext())
                        {
                            pi = iterator.Current;
                            if (string.IsNullOrEmpty(pi.Id))
                            {
                                throw new InvalidConfigurationException("Contract type not definied. Empty item found in configuration.");
                            }
                            Type contractType = null;
                            Type defaultImplementationType = null;

                            try
                            {
                                contractType = TypeHelper.GetTypeFromString(pi.Id);
                                if (ContractDescriptors.ContainsKey(contractType))
                                {
                                    throw new InvalidConfigurationException(String.Format("Duplicated contract type configuration found in services. Contract: {0}", contractType.FullName));
                                }
                                ContractValidator.ValidateContractIntegrity(contractType);
                            }
                            catch (Exception ex)
                            {
                                throw new InvalidConfigurationValueException(String.Format("Unable to resolve contract type: {0}", pi.Id), ex);
                            }

                            if (!string.IsNullOrEmpty(pi.EntryValue))
                            {
                                try
                                {
                                    defaultImplementationType = TypeHelper.GetTypeFromString(pi.EntryValue);
                                    if (!contractType.IsAssignableFrom(defaultImplementationType))
                                    {
                                        throw new InvalidProxyImplementationException(String.Format("Provided default implementation type '{0}' does not implement contract interface '{1}'.", defaultImplementationType.FullName, contractType.FullName));
                                    }
                                    ImplementationValidator.ValidateImplementationIntegrity(defaultImplementationType);
                                }
                                catch (Exception ex)
                                {
                                    throw new InvalidConfigurationValueException(String.Format("Unable to resolve implementation type: {0}", pi.EntryValue), ex);
                                }
                            }

                            ContractServiceSideDescriptor descriptor = new ContractServiceSideDescriptor(contractType, defaultImplementationType);

                            IEnumerator <CategoryPropertyItem> channelIterator = pi.GetEnumerator();
                            while (channelIterator.MoveNext())
                            {
                                CategoryPropertyItem channelImplementationItem = channelIterator.Current;
                                if (string.IsNullOrEmpty(channelImplementationItem.Id))
                                {
                                    throw new InvalidConfigurationValueException(String.Format("Channel identifier is missing from a configuration item of the contract '{0}'", pi.Id));
                                }
                                if (string.IsNullOrEmpty(channelImplementationItem.EntryValue))
                                {
                                    throw new InvalidConfigurationValueException(String.Format("Implementation type is missing from a configuration item of the contract '{0}'", pi.Id));
                                }
                                if (!ChannelServices.IsChannelRegistered(channelImplementationItem.Id))
                                {
                                    throw new InvalidConfigurationValueException(String.Format("Unregistered channel provided '{0}' in configuration section of the contract: {1}.", channelImplementationItem.Id, pi.Id));
                                }
                                Type type = null;
                                try
                                {
                                    type = TypeHelper.GetTypeFromString(channelImplementationItem.EntryValue);
                                    if (!contractType.IsAssignableFrom(type))
                                    {
                                        throw new InvalidProxyImplementationException(String.Format("Provided implementation type '{0}' does not implement contract interface '{1}'.", type.FullName, contractType.FullName));
                                    }
                                    ImplementationValidator.ValidateImplementationIntegrity(type);
                                }
                                catch (Exception ex)
                                {
                                    throw new InvalidConfigurationValueException(String.Format("Unable to resolve non-default implementation type: {0} for contract: {1} for the channel: {2}", channelImplementationItem.EntryValue, pi.Id, channelImplementationItem.Id), ex);
                                }
                                if (descriptor.ImplementationPerChannel.ContainsKey(channelImplementationItem.Id))
                                {
                                    throw new InvalidConfigurationException(String.Format("Duplicated channel identifier at contract '{0}'.", pi.Id));
                                }
                                descriptor.ImplementationPerChannel.Add(channelImplementationItem.Id, type);
                            }

                            ContractDescriptors.Add(contractType, descriptor);
                        }
                        ChannelServices.StartListeningChannels();
                    }
                    catch (Exception ex)
                    {
                        ContractDescriptors.Clear();
                        throw ex;
                    }
                }

                mInitialized = true;
                if (LOGGER.IsInfoEnabled)
                {
                    LOGGER.Info("ServiceBase services successfully initialized.");
                }
                Raiser.CallDelegatorBySync(EventInitialization, new object[] { null, new ServiceInitializationStateEventArgs(ServiceInitializationStateEnum.After) });
            }
        }
Beispiel #3
0
        public static void Initialize()
        {
            if (!mInitialized)
            {
                Raiser.CallDelegatorBySync(EventInitialization, new object[] { null, new ServiceInitializationStateEventArgs(ServiceInitializationStateEnum.Before) });
                ChannelServices.Initialize();
                if (LOGGER.IsInfoEnabled)
                {
                    LOGGER.Info("Initializing client proxy services.");
                }

                CategoryPropertyItem pi = ConfigurationAccessHelper.GetCategoryPropertyByPath(RemotingConfiguration.Settings.CategoryPropertyItems, "Clients");
                if (pi != null)
                {
                    try
                    {
                        IEnumerator <CategoryPropertyItem> iterator = pi.GetEnumerator();
                        while (iterator.MoveNext())
                        {
                            pi = iterator.Current;
                            if (string.IsNullOrEmpty(pi.Id))
                            {
                                throw new InvalidConfigurationException("Contract type not definied. Empty item found in configuration.");
                            }
                            Type   contractType     = null;
                            String defaultChannelId = null;
                            Type   defaultProxyType = null;

                            try
                            {
                                contractType = TypeHelper.GetTypeFromString(pi.Id);
                                if (mContractDescriptors.ContainsKey(contractType))
                                {
                                    throw new InvalidConfigurationException(String.Format("Duplicated contract type configuration found in clients. Contract: {0}", contractType.FullName));
                                }
                                ContractValidator.ValidateContractIntegrity(contractType);
                            }
                            catch (Exception ex)
                            {
                                throw new InvalidConfigurationValueException(String.Format("Unable to resolve contract type: {0}", pi.Id), ex);
                            }

                            {
                                CategoryPropertyItem defaultChannelIdPropertyItem = ConfigurationAccessHelper.GetCategoryPropertyByPath(RemotingConfiguration.Settings.CategoryPropertyItems, String.Format("Clients/{0}/Defaults/DefaultChannelId", pi.Id));
                                if (defaultChannelIdPropertyItem != null)
                                {
                                    defaultChannelId = defaultChannelIdPropertyItem.EntryValue;
                                    if (string.Empty.Equals(defaultChannelId))
                                    {
                                        defaultChannelId = null;
                                    }
                                }
                            }

                            {
                                CategoryPropertyItem defaultProxyTypePropertyItem = ConfigurationAccessHelper.GetCategoryPropertyByPath(RemotingConfiguration.Settings.CategoryPropertyItems, String.Format("Clients/{0}/Defaults/DefaultProxyType", pi.Id));
                                if (defaultProxyTypePropertyItem != null)
                                {
                                    if (defaultProxyTypePropertyItem.EntryValue != null && !string.Empty.Equals(defaultProxyTypePropertyItem.EntryValue))
                                    {
                                        try
                                        {
                                            defaultProxyType = TypeHelper.GetTypeFromString(defaultProxyTypePropertyItem.EntryValue);
                                            ImplementationValidator.ValidateProxyIntegration(defaultProxyType);
                                        }
                                        catch (Exception ex)
                                        {
                                            throw new InvalidConfigurationValueException(String.Format("Unable to resolve proxy type: {0} for contract: {1}", defaultProxyTypePropertyItem.EntryValue, pi.Id), ex);
                                        }
                                    }
                                }
                            }
                            if (!(defaultChannelId == null && defaultProxyType == null))
                            {
                                if (defaultChannelId == null || defaultProxyType == null)
                                {
                                    throw new InvalidConfigurationException(String.Format("Both of 'DefaultChannelId' and 'DefaultProxyType' values are must be filled in configuration. Contract type: {0}", pi.Id));
                                }
                                if (!contractType.IsAssignableFrom(defaultProxyType))
                                {
                                    throw new InvalidProxyImplementationException(String.Format("Provided default proxy type '{0}' does not implement contract interface '{1}'.", defaultProxyType.FullName, contractType.FullName));
                                }
                            }

                            ContractClientSideDescriptor descriptor = new ContractClientSideDescriptor(contractType, defaultChannelId, defaultProxyType);

                            {
                                CategoryPropertyItem customDefinitionsPropertyItem = ConfigurationAccessHelper.GetCategoryPropertyByPath(RemotingConfiguration.Settings.CategoryPropertyItems, String.Format("Clients/{0}/CustomDefinitions", pi.Id));
                                if (customDefinitionsPropertyItem != null)
                                {
                                    IEnumerator <CategoryPropertyItem> customDefinitionsIterator = customDefinitionsPropertyItem.GetEnumerator();
                                    while (customDefinitionsIterator.MoveNext())
                                    {
                                        CategoryPropertyItem channelProxyItem = customDefinitionsIterator.Current;
                                        if (string.IsNullOrEmpty(channelProxyItem.Id))
                                        {
                                            throw new InvalidConfigurationValueException(String.Format("Channel identifier is missing from a configuration item at the CustomDefinitions of the contract '{0}'", pi.Id));
                                        }
                                        if (string.IsNullOrEmpty(channelProxyItem.EntryValue))
                                        {
                                            throw new InvalidConfigurationValueException(String.Format("Proxy type is missing from a configuration item at the CustomDefinitions of the contract '{0}'", pi.Id));
                                        }
                                        if (!ChannelServices.IsChannelRegistered(channelProxyItem.Id))
                                        {
                                            throw new InvalidConfigurationValueException(String.Format("Unregistered channel provided '{0}' in CustomDefinitions configuration section of the contract: {1}.", channelProxyItem.Id, pi.Id));
                                        }
                                        Type type = null;
                                        try
                                        {
                                            type = TypeHelper.GetTypeFromString(channelProxyItem.EntryValue);
                                            if (!contractType.IsAssignableFrom(type))
                                            {
                                                throw new InvalidProxyImplementationException(String.Format("Provided proxy type '{0}' does not implement contract interface '{1}'.", type.FullName, contractType.FullName));
                                            }
                                            ImplementationValidator.ValidateProxyIntegration(type);
                                        }
                                        catch (Exception ex)
                                        {
                                            throw new InvalidConfigurationValueException(String.Format("Unable to resolve non-default proxy type: {0} for contract: {1} for the channel: {2}", channelProxyItem.EntryValue, pi.Id, channelProxyItem.Id), ex);
                                        }
                                        if (descriptor.ImplementationPerChannel.ContainsKey(channelProxyItem.Id))
                                        {
                                            throw new InvalidConfigurationException(String.Format("Duplicated channel identifier in CustomDefinitions section at contract '{0}'.", pi.Id));
                                        }
                                        descriptor.ImplementationPerChannel.Add(channelProxyItem.Id, type);
                                    }
                                }
                            }
                            mContractDescriptors.Add(contractType, descriptor);
                        }
                        mSingletonInstance = new ProxyServices();
                        //if (mContractDescriptors.Count > 0)
                        //{
                        //    mSingletonInstance = new ProxyServices();
                        //}
                    }
                    catch (Exception ex)
                    {
                        mContractDescriptors.Clear();
                        throw ex;
                    }
                }
                //ChannelServices.startListeningChannels();

                mInitialized = true;
                if (LOGGER.IsInfoEnabled)
                {
                    LOGGER.Info("Client proxy services successfully initialized.");
                }
                Raiser.CallDelegatorBySync(EventInitialization, new object[] { null, new ServiceInitializationStateEventArgs(ServiceInitializationStateEnum.After) });
            }
        }
Beispiel #4
0
        public static void Initialize()
        {
            if (!mInitialized)
            {
                if (LOGGER.IsInfoEnabled)
                {
                    LOGGER.Info("Initializing channel services.");
                }
                CategoryPropertyItem pi = ConfigurationAccessHelper.GetCategoryPropertyByPath(RemotingConfiguration.Settings.CategoryPropertyItems, "Channels");
                if (pi != null)
                {
                    try
                    {
                        IEnumerator <CategoryPropertyItem> iterator = pi.GetEnumerator();
                        while (iterator.MoveNext())
                        {
                            pi = iterator.Current;
                            if (string.IsNullOrEmpty(pi.Id))
                            {
                                throw new InvalidConfigurationException("Channel id has not been definied.");
                            }
                            if (string.IsNullOrEmpty(pi.EntryValue))
                            {
                                throw new InvalidConfigurationException("Channel class name has not been definied.");
                            }
                            Channel channel = null;
                            try
                            {
                                if (LOGGER.IsInfoEnabled)
                                {
                                    LOGGER.Info(string.Format("Create channel instance from type '{0}'. ChannelId: '{1}'", pi.EntryValue, pi.Id));
                                }
                                Type cls = TypeHelper.GetTypeFromString(pi.EntryValue);
                                channel = (Channel)cls.GetConstructor(new Type[] { }).Invoke(null);
                            }
                            catch (Exception ex)
                            {
                                throw new InvalidConfigurationValueException(String.Format("Unable to create instance of channel type: {0}", pi.EntryValue), ex);
                            }
                            mChannels.Add(channel);
                            if (LOGGER.IsInfoEnabled)
                            {
                                LOGGER.Info(string.Format("Initializing channel instance, type '{0}'. ChannelId: '{1}'", pi.EntryValue, pi.Id));
                            }
                            channel.Initialize(pi);
                            //channel.startListening();
                        }
                    }
                    catch (Exception ex)
                    {
                        // system restoring
                        if (mChannels.Count > 0)
                        {
                            foreach (Channel c in mChannels)
                            {
                                c.StopListening();
                                c.Dispose();
                            }
                            mChannels.Clear();
                        }
                        throw new InitializationException(String.Format("Unable to initialize {0}.", typeof(ChannelServices).Name), ex);
                    }
                }

                mInitialized = true;
                if (LOGGER.IsInfoEnabled)
                {
                    LOGGER.Info("Channel services successfully initialized.");
                }
            }
        }