示例#1
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);
        }