Пример #1
0
        /// <summary>
        /// Registers the contract.
        /// </summary>
        /// <param name="contractType">Type of the contract.</param>
        /// <param name="defaultImplementationType">Default type of the implementation.</param>
        public static void RegisterContract(Type contractType, Type defaultImplementationType)
        {
            DoInitializeCheck();
            if (contractType == null)
            {
                ThrowHelper.ThrowArgumentNullException("contractType");
            }

            ContractValidator.ValidateContractIntegrity(contractType);

            if (defaultImplementationType != null)
            {
                ImplementationValidator.ValidateImplementationIntegrity(defaultImplementationType);
            }

            lock (mContractDescriptors)
            {
                if (mContractDescriptors.ContainsKey(contractType))
                {
                    ThrowHelper.ThrowArgumentException("Contract type has already registered.");
                }
                mContractDescriptors.Add(contractType, new ContractServiceSideDescriptor(contractType, defaultImplementationType));
            }
        }
Пример #2
0
        /// <summary>
        /// Registers the implementation for channel.
        /// </summary>
        /// <param name="contractType">Type of the contract.</param>
        /// <param name="channelId">The channel id.</param>
        /// <param name="defaultImplementationType">Default type of the implementation.</param>
        public static void RegisterImplementationForChannel(Type contractType, string channelId, Type defaultImplementationType)
        {
            DoInitializeCheck();
            if (contractType == null)
            {
                ThrowHelper.ThrowArgumentNullException("contractType");
            }
            if (!ChannelServices.IsChannelRegistered(channelId))
            {
                ThrowHelper.ThrowArgumentException("Default channel identifier has not found.");
            }

            lock (mContractDescriptors)
            {
                if (mContractDescriptors.ContainsKey(contractType))
                {
                    ContractServiceSideDescriptor descriptor = mContractDescriptors[contractType];
                    if (defaultImplementationType == null)
                    {
                        if (descriptor.ImplementationPerChannel.ContainsKey(channelId))
                        {
                            descriptor.ImplementationPerChannel.Remove(channelId);
                        }
                    }
                    else
                    {
                        ImplementationValidator.ValidateImplementationIntegrity(defaultImplementationType);
                        descriptor.ImplementationPerChannel[channelId] = defaultImplementationType;
                    }
                }
                else
                {
                    ThrowHelper.ThrowArgumentException("Contract type has not been registered.");
                }
            }
        }
Пример #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 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) });
            }
        }
Пример #4
0
        /// <summary>
        /// Prevents a default instance of the <see cref="ServiceFactory&lt;TContract&gt;"/> class from being created.
        /// </summary>
        /// <param name="channelId">The channel id.</param>
        /// <param name="implementationType">Type of the implementation.</param>
        private ServiceFactory(String channelId, Type implementationType)
        {
            if (string.IsNullOrEmpty(channelId))
            {
                ThrowHelper.ThrowArgumentNullException("channelId");
            }
            if (implementationType == null)
            {
                ThrowHelper.ThrowArgumentNullException("implementationType");
            }

            if (!ServiceContract.IsAssignableFrom(implementationType))
            {
                throw new ArgumentException("Provided implementation does not implements the contract interface.", "implementationType");
            }

            ContractValidator.ValidateContractIntegrity(ServiceContract);
            ImplementationValidator.ValidateImplementationIntegrity(implementationType);

            this.mChannel = ChannelServices.GetChannelById(channelId);
            if (this.mChannel == null)
            {
                throw new ChannelNotFoundException(channelId);
            }

            this.mImplementationType = implementationType;

            // adatstruktúra adminisztrációja
            // egy factory csak azt adhatja hozzá, ami nincs és csak azt veheti el, ami még nem volt.
            ContractServiceSideDescriptor descriptor = null;
            Dictionary <Type, ContractServiceSideDescriptor> contractDescriptors = ServiceBaseServices.ContractDescriptors;

            lock (contractDescriptors)
            {
                if (contractDescriptors.ContainsKey(ServiceContract))
                {
                    // ismert contract
                    descriptor = contractDescriptors[ServiceContract];
                }
                else
                {
                    // ilyen contract még nincs
                    mControlServiceContract = true;
                    mControlChannel         = true;
                }
            }
            if (descriptor != null)
            {
                lock (descriptor)
                {
                    if (!descriptor.ImplementationPerChannel.ContainsKey(channelId))
                    {
                        mControlChannel = true;
                    }
                    else
                    {
                        if (descriptor.ImplementationPerChannel.ContainsKey(channelId))
                        {
                            // ehhez a csatornához és contracthoz már egy másik implementáció van rendelve
                            Type currentImplType = descriptor.ImplementationPerChannel[channelId];
                            if (!currentImplType.Equals(implementationType))
                            {
                                throw new ArgumentException(String.Format("Unable to register provided implementation type: '{0}'. An other implementation type '{1}' has already definied for channel '{2}' and contract '{3}'.", implementationType.FullName, currentImplType.FullName, channelId, ServiceContract.FullName));
                            }
                        }
                    }
                }
            }
            ChannelServices.UnregisterChannelEvent += new EventHandler <ChannelRegistrationEventArgs>(ChannelUnregisteredEventHandler);
        }