Пример #1
0
        /// <summary>
        /// Registers the contract.
        /// </summary>
        /// <param name="contractType">Type of the contract.</param>
        /// <param name="defaultChannelId">The default channel id.</param>
        /// <param name="defaultProxyType">Default type of the proxy.</param>
        /// <exception cref="System.InvalidOperationException">Contract has already registered.</exception>
        public static void RegisterContract(Type contractType, string defaultChannelId, Type defaultProxyType)
        {
            DoInitializeCheck();
            if (contractType == null)
            {
                ThrowHelper.ThrowArgumentNullException("contractType");
            }

            ContractValidator.ValidateContractIntegrity(contractType);
            if (!ChannelServices.IsChannelRegistered(defaultChannelId))
            {
                ThrowHelper.ThrowArgumentException("Default channel identifier has not found.");
            }
            if (defaultProxyType != null)
            {
                ImplementationValidator.ValidateProxyIntegration(defaultProxyType);
            }
            lock (mContractDescriptors)
            {
                if (mContractDescriptors.ContainsKey(contractType))
                {
                    throw new InvalidOperationException("Contract has already registered.");
                }
                ContractClientSideDescriptor descriptor = new ContractClientSideDescriptor(contractType, defaultChannelId, defaultProxyType);
                mContractDescriptors.Add(contractType, descriptor);
            }
        }
Пример #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))
         {
             ContractClientSideDescriptor descriptor = mContractDescriptors[contractType];
             if (defaultImplementationType == null)
             {
                 if (descriptor.ImplementationPerChannel.ContainsKey(channelId))
                 {
                     descriptor.ImplementationPerChannel.Remove(channelId);
                 }
             }
             else
             {
                 ImplementationValidator.ValidateProxyIntegration(defaultImplementationType);
                 if (descriptor.ImplementationPerChannel.ContainsKey(channelId))
                 {
                     descriptor.ImplementationPerChannel[channelId] = defaultImplementationType;
                 }
                 else
                 {
                     descriptor.ImplementationPerChannel.Add(channelId, defaultImplementationType);
                 }
             }
         }
         else
         {
             ThrowHelper.ThrowArgumentException("Contract type has not been registered.");
         }
     }
 }
Пример #3
0
        /// <summary>
        /// Gets the contract defaults.
        /// </summary>
        /// <param name="contractType">Type of the contract.</param>
        /// <returns>Client side contract descriptor</returns>
        public static ContractClientSideDescriptor GetContractDefaults(Type contractType)
        {
            DoInitializeCheck();
            if (contractType == null)
            {
                ThrowHelper.ThrowArgumentNullException("contractType");
            }

            ContractClientSideDescriptor result = null;

            lock (mContractDescriptors)
            {
                if (mContractDescriptors.ContainsKey(contractType))
                {
                    result = mContractDescriptors[contractType];
                }
            }

            return(result);
        }
Пример #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ProxyFactory&lt;TContract&gt;"/> class.
 /// </summary>
 /// <param name="channelId">The channel id.</param>
 public ProxyFactory(string channelId)
 {
     if (string.IsNullOrEmpty(channelId))
     {
         ThrowHelper.ThrowArgumentNullException("channelId");
     }
     if (ProxyServices.ContractDescriptors.ContainsKey(ContractInterface))
     {
         ContractClientSideDescriptor descriptor = ProxyServices.ContractDescriptors[ContractInterface];
         foreach (KeyValuePair <String, Type> entry in descriptor.ImplementationPerChannel)
         {
             if (entry.Key.Equals(channelId))
             {
                 ValidateConstructorParameters(ContractInterface, entry.Value, channelId);
                 break;
             }
         }
         // itt csak azt vizsgálom, hogy a for ciklus megtalálta-e a channel-t és a hozzá tartozó proxy type-ot.
         if (mChannel == null)
         {
             // nem találtam, megnézem, hogy a default jó-e
             if (!string.IsNullOrEmpty(descriptor.DefaultChannelId) && descriptor.DefaultProxyType != null && descriptor.DefaultChannelId.Equals(channelId))
             {
                 // jó lesz a default
                 ValidateConstructorParameters(ContractInterface, descriptor.DefaultProxyType, descriptor.DefaultChannelId);
             }
             else
             {
                 // hiba, ehhez a channel-hez nincs proxy definiálva
                 throw new InitializationException(String.Format("Proxy type definition not found for this channel '{0}' and contract '{1}'.", channelId, ContractInterface.FullName));
             }
         }
     }
     else
     {
         // nincs ilyen contract definiálva
         throw new InitializationException(String.Format("No configuration exist for this type of contract: {0}", ContractInterface.FullName));
     }
 }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ProxyFactory&lt;TContract&gt;"/> class.
 /// </summary>
 public ProxyFactory()
 {
     // default channel and proxy implementation for the contract
     if (ProxyServices.ContractDescriptors.ContainsKey(ContractInterface))
     {
         ContractClientSideDescriptor descriptor = ProxyServices.ContractDescriptors[ContractInterface];
         if (!string.IsNullOrEmpty(descriptor.DefaultChannelId) && descriptor.DefaultProxyType != null)
         {
             ValidateConstructorParameters(ContractInterface, descriptor.DefaultProxyType, descriptor.DefaultChannelId);
         }
         else
         {
             // nincs default konfiguráció ehhez a contract-hoz
             throw new InitializationException(String.Format("Default configuration not found for this contract type: {0}", ContractInterface.FullName));
         }
     }
     else
     {
         // nincs ilyen contract definiálva
         throw new InitializationException(String.Format("No configuration exist for this type of contract: {0}", ContractInterface.FullName));
     }
 }
Пример #6
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) });
            }
        }