Beispiel #1
0
        /// <summary>
        /// Initializes the <see cref="SyndicationManager"/> using the declaratively configured application settings.
        /// </summary>
        /// <seealso cref="SyndicationResourceSection"/>
        private static void Initialize()
        {
            //------------------------------------------------------------
            //	Verify manager has not already been initialized previously
            //------------------------------------------------------------
            if (syndicationManagerInitialized)
            {
                //------------------------------------------------------------
                //	Throw stored exception if initialization raised an exception
                //------------------------------------------------------------
                if (syndicationManagerInitializationException != null)
                {
                    throw syndicationManagerInitializationException;
                }
            }
            else
            {
                //------------------------------------------------------------
                //	Throw stored exception if initialization raised an exception
                //------------------------------------------------------------
                if (syndicationManagerInitializationException != null)
                {
                    throw syndicationManagerInitializationException;
                }

                lock (syndicationManagerLock)
                {
                    try
                    {
                        //------------------------------------------------------------
                        //	Extract configuration section from application configuration
                        //------------------------------------------------------------
                        SyndicationResourceSection section  = PrivilegedConfigurationManager.GetSyndicationResourceSection();

                        if (section != null)
                        {
                            //------------------------------------------------------------
                            //	Raise exception if no providers were specified
                            //------------------------------------------------------------
                            if (((section.DefaultProvider == null) || (section.Providers == null)) || (section.Providers.Count < 1))
                            {
                                throw new ProviderException(String.Format(null, "The syndication resource providers were not specified. Source configuration file was {0}", section.ElementInformation.Source));
                            }

                            //------------------------------------------------------------
                            //	Fill providers collection
                            //------------------------------------------------------------
                            syndicationManagerProviders = new SyndicationResourceProviderCollection();

                            if (HostingEnvironment.IsHosted)
                            {
                                ProvidersHelper.InstantiateProviders(section.Providers, syndicationManagerProviders, typeof(SyndicationResourceProvider));
                            }
                            else
                            {
                                foreach (ProviderSettings settings in section.Providers)
                                {
                                    Type settingsType   = Type.GetType(settings.Type, true, true);
                                    if (!typeof(SyndicationResourceProvider).IsAssignableFrom(settingsType))
                                    {
                                        throw new ArgumentException(String.Format(null, "The provider must implement type of {0}. Source configuration file was {1}", typeof(SyndicationResourceProvider).FullName, section.ElementInformation.Source));
                                    }

                                    SyndicationResourceProvider provider    = (SyndicationResourceProvider)Activator.CreateInstance(settingsType);
                                    NameValueCollection parameters          = settings.Parameters;
                                    NameValueCollection configuration       = new NameValueCollection(parameters.Count, StringComparer.Ordinal);

                                    foreach (string parameter in parameters)
                                    {
                                        configuration[parameter]    = parameters[parameter];
                                    }

                                    provider.Initialize(settings.Name, configuration);
                                    syndicationManagerProviders.Add(provider);
                                }
                            }

                            //------------------------------------------------------------
                            //	Set and validate default provider
                            //------------------------------------------------------------
                            syndicationManagerDefaultProvider   = syndicationManagerProviders[section.DefaultProvider];

                            if (syndicationManagerDefaultProvider == null)
                            {
                                throw new ConfigurationErrorsException(String.Format(null, "The default syndication resource provider was not found. Source configuration file was {0}", section.ElementInformation.Source), section.ElementInformation.Properties["defaultProvider"].Source, section.ElementInformation.Properties["defaultProvider"].LineNumber);
                            }

                            //------------------------------------------------------------
                            //	Prevent collection from being modified further
                            //------------------------------------------------------------
                            syndicationManagerProviders.SetReadOnly();
                        }
                    }
                    catch(Exception exception)
                    {
                        //------------------------------------------------------------
                        //	Store exception locally and then throw
                        //------------------------------------------------------------
                        syndicationManagerInitializationException   = exception;
                        throw;
                    }

                    //------------------------------------------------------------
                    //	Set initialization indicator flag
                    //------------------------------------------------------------
                    syndicationManagerInitialized   = true;
                }
            }
        }
Beispiel #2
0
        //============================================================
        //	PRIVATE METHODS
        //============================================================
        #region Initialize()
        /// <summary>
        /// Initializes the <see cref="SyndicationManager"/> using the declaratively configured application settings.
        /// </summary>
        /// <seealso cref="SyndicationResourceSection"/>
        private static void Initialize()
        {
            //------------------------------------------------------------
            //	Verify manager has not already been initialized previously
            //------------------------------------------------------------
            if (syndicationManagerInitialized)
            {
                //------------------------------------------------------------
                //	Throw stored exception if initialization raised an exception
                //------------------------------------------------------------
                if (syndicationManagerInitializationException != null)
                {
                    throw syndicationManagerInitializationException;
                }
            }
            else
            {
                //------------------------------------------------------------
                //	Throw stored exception if initialization raised an exception
                //------------------------------------------------------------
                if (syndicationManagerInitializationException != null)
                {
                    throw syndicationManagerInitializationException;
                }

                lock (syndicationManagerLock)
                {
                    try
                    {
                        //------------------------------------------------------------
                        //	Extract configuration section from application configuration
                        //------------------------------------------------------------
                        SyndicationResourceSection section = PrivilegedConfigurationManager.GetSyndicationResourceSection();

                        if (section != null)
                        {
                            //------------------------------------------------------------
                            //	Raise exception if no providers were specified
                            //------------------------------------------------------------
                            if (((section.DefaultProvider == null) || (section.Providers == null)) || (section.Providers.Count < 1))
                            {
                                throw new ProviderException(String.Format(null, "The syndication resource providers were not specified. Source configuration file was {0}", section.ElementInformation.Source));
                            }

                            //------------------------------------------------------------
                            //	Fill providers collection
                            //------------------------------------------------------------
                            syndicationManagerProviders = new SyndicationResourceProviderCollection();

                            if (HostingEnvironment.IsHosted)
                            {
                                ProvidersHelper.InstantiateProviders(section.Providers, syndicationManagerProviders, typeof(SyndicationResourceProvider));
                            }
                            else
                            {
                                foreach (ProviderSettings settings in section.Providers)
                                {
                                    Type settingsType = Type.GetType(settings.Type, true, true);
                                    if (!typeof(SyndicationResourceProvider).IsAssignableFrom(settingsType))
                                    {
                                        throw new ArgumentException(String.Format(null, "The provider must implement type of {0}. Source configuration file was {1}", typeof(SyndicationResourceProvider).FullName, section.ElementInformation.Source));
                                    }

                                    SyndicationResourceProvider provider      = (SyndicationResourceProvider)Activator.CreateInstance(settingsType);
                                    NameValueCollection         parameters    = settings.Parameters;
                                    NameValueCollection         configuration = new NameValueCollection(parameters.Count, StringComparer.Ordinal);

                                    foreach (string parameter in parameters)
                                    {
                                        configuration[parameter] = parameters[parameter];
                                    }

                                    provider.Initialize(settings.Name, configuration);
                                    syndicationManagerProviders.Add(provider);
                                }
                            }

                            //------------------------------------------------------------
                            //	Set and validate default provider
                            //------------------------------------------------------------
                            syndicationManagerDefaultProvider = syndicationManagerProviders[section.DefaultProvider];

                            if (syndicationManagerDefaultProvider == null)
                            {
                                throw new ConfigurationErrorsException(String.Format(null, "The default syndication resource provider was not found. Source configuration file was {0}", section.ElementInformation.Source), section.ElementInformation.Properties["defaultProvider"].Source, section.ElementInformation.Properties["defaultProvider"].LineNumber);
                            }

                            //------------------------------------------------------------
                            //	Prevent collection from being modified further
                            //------------------------------------------------------------
                            syndicationManagerProviders.SetReadOnly();
                        }
                    }
                    catch (Exception exception)
                    {
                        //------------------------------------------------------------
                        //	Store exception locally and then throw
                        //------------------------------------------------------------
                        syndicationManagerInitializationException = exception;
                        throw;
                    }

                    //------------------------------------------------------------
                    //	Set initialization indicator flag
                    //------------------------------------------------------------
                    syndicationManagerInitialized = true;
                }
            }
        }