コード例 #1
0
        internal static ProviderBase InstantiateProvider(NameValueCollection providerSettings, Type providerType)
        {
            ProviderBase provider = null;

            try {
                string pnName = GetAndRemoveStringValue(providerSettings, "name");
                string pnType = GetAndRemoveStringValue(providerSettings, "type");
                if (string.IsNullOrEmpty(pnType))
                {
                    throw new ArgumentException(SR.GetString(SR.Provider_no_type_name));
                }
                Type t = ConfigUtil.GetType(pnType, "type", null, null, true, true);

                if (!providerType.IsAssignableFrom(t))
                {
                    throw new ArgumentException(SR.GetString(SR.Provider_must_implement_type, providerType.ToString()));
                }
                provider = (ProviderBase)HttpRuntime.CreatePublicInstanceByWebObjectActivator(t);

                // Because providers modify the parameters collection (i.e. delete stuff), pass in a clone of the collection
                NameValueCollection cloneParams = new NameValueCollection(providerSettings.Count, StringComparer.Ordinal);
                foreach (string key in providerSettings)
                {
                    cloneParams[key] = providerSettings[key];
                }
                provider.Initialize(pnName, cloneParams);

                TelemetryLogger.LogProvider(t);
            }
            catch (Exception e) {
                if (e is ConfigurationException)
                {
                    throw;
                }
                throw new ConfigurationErrorsException(e.Message, e);
            }

            return(provider);
        }
コード例 #2
0
        public static ProviderBase InstantiateProvider(ProviderSettings providerSettings, Type providerType)
        {
            ProviderBase provider = null;

            try {
                string pnType = (providerSettings.Type == null) ? null : providerSettings.Type.Trim();
                if (string.IsNullOrEmpty(pnType))
                {
                    throw new ArgumentException(SR.GetString(SR.Provider_no_type_name));
                }
                Type t = ConfigUtil.GetType(pnType, "type", providerSettings, true, true);

                if (!providerType.IsAssignableFrom(t))
                {
                    throw new ArgumentException(SR.GetString(SR.Provider_must_implement_type, providerType.ToString()));
                }
                provider = (ProviderBase)HttpRuntime.CreatePublicInstanceByWebObjectActivator(t);

                // Because providers modify the parameters collection (i.e. delete stuff), pass in a clone of the collection
                NameValueCollection pars        = providerSettings.Parameters;
                NameValueCollection cloneParams = new NameValueCollection(pars.Count, StringComparer.Ordinal);
                foreach (string key in pars)
                {
                    cloneParams[key] = pars[key];
                }
                provider.Initialize(providerSettings.Name, cloneParams);

                TelemetryLogger.LogProvider(t);
            } catch (Exception e) {
                if (e is ConfigurationException)
                {
                    throw;
                }
                throw new ConfigurationErrorsException(e.Message, e, providerSettings.ElementInformation.Properties["type"].Source, providerSettings.ElementInformation.Properties["type"].LineNumber);
            }

            return(provider);
        }