public static ProviderBase InstantiateProvider(ProviderSettings providerSettings, Type providerType)
        {
            ProviderBase base2 = null;

            try
            {
                string str = (providerSettings.Type == null) ? null : providerSettings.Type.Trim();
                if (string.IsNullOrEmpty(str))
                {
                    throw new ArgumentException(System.Web.SR.GetString("Provider_no_type_name"));
                }
                Type c = ConfigUtil.GetType(str, "type", providerSettings, true, true);
                if (!providerType.IsAssignableFrom(c))
                {
                    throw new ArgumentException(System.Web.SR.GetString("Provider_must_implement_type", new object[] { providerType.ToString() }));
                }
                base2 = (ProviderBase)HttpRuntime.CreatePublicInstance(c);
                NameValueCollection parameters = providerSettings.Parameters;
                NameValueCollection config     = new NameValueCollection(parameters.Count, StringComparer.Ordinal);
                foreach (string str2 in parameters)
                {
                    config[str2] = parameters[str2];
                }
                base2.Initialize(providerSettings.Name, config);
            }
            catch (Exception exception)
            {
                if (exception is ConfigurationException)
                {
                    throw;
                }
                throw new ConfigurationErrorsException(exception.Message, exception, providerSettings.ElementInformation.Properties["type"].Source, providerSettings.ElementInformation.Properties["type"].LineNumber);
            }
            return(base2);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 创建Provider
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public static ProviderBase CreateProvider(ConfigWithProviders config)
        {
            string cacheKey = config.ConfigName + "::" + config.DefaultProvider;

            ProviderBase objProvider = HttpRuntime.Cache.Get(cacheKey) as ProviderBase;

            if (objProvider == null)
            {
                try
                {
                    // Read the configuration specific information for this provider
                    ConfigWithProviders.Provider provider = config.Providers[config.DefaultProvider];

                    // The assembly should be in \bin or GAC
                    Type type = Type.GetType(provider.Type);
                    objProvider = (ProviderBase)Activator.CreateInstance(type);

                    objProvider.Initialize(provider.Name, provider.Attributes);

                    // 添加到缓存
                    if (objProvider != null)
                    {
                        HttpRuntime.Cache.Insert(cacheKey, objProvider);
                    }
                }
                catch (Exception e)
                {
                    throw new KissException(Resource.LoadProviderFailed, e);
                }
            }

            return(objProvider);
        }
 /// <summary>
 /// Initialize all services and features synchronously.
 /// </summary>
 public void InitializeAllServices()
 {
     // Find and wire up the first un-initialized provider
     if (_services.Values.Count(i => !i.Initialized) > 0)
     {
         ProviderBase pb = _services.Values.First(i => !i.Initialized);
         pb.InitializeCompleted += (sender, e) => InitializeAllServices();
         pb.Initialize();
     }
     else
     {
         // Fires the initialization finished event
         base.Initialize();
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static ProviderBase Instantiate(
            Config.DataAccessSection config, ProviderSettings settings, Type providerType)
        {
            ProviderBase __base = null;

            try {
                string __typeName = (settings.Type == null) ? null : settings.Type.Trim();
                if (string.IsNullOrEmpty(__typeName))
                {
                    throw new ArgumentException("Provider doesn't have type name");
                }
                Type __type = Type.GetType(__typeName); //ConfigUtil.GetType(text1, "type", settings);
                if (!providerType.IsAssignableFrom(__type))
                {
                    throw new ArgumentException(string.Format(
                                                    "Provider must implement type {0}", providerType.ToString()));
                }
                __base = (ProviderBase)Activator.CreateInstance(__type);
                NameValueCollection __parameters     = settings.Parameters;
                NameValueCollection __providerConfig = new NameValueCollection(__parameters.Count, StringComparer.InvariantCulture);
                foreach (string __param in __parameters)
                {
                    __providerConfig[__param] = __parameters[__param];
                }
                if (__providerConfig["connectionName"] == null)
                {
                    __providerConfig["connectionName"] = config.DefaultConnectionName;
                }
                if (__providerConfig["defaultCommandType"] == null)
                {
                    __providerConfig["defaultCommandType"] = config.DefaultCommandType;
                }
                //if (__providerConfig["defaultRollbackBehaviour"] == null) {
                //    __providerConfig["defaultRollbackBehaviour"] = config.DefaultRollbackBehaviour;
                //}
                __base.Initialize(settings.Name, __providerConfig);
            }
            catch (Exception exception1) {
                if (exception1 is ConfigurationException)
                {
                    throw;
                }
                throw new ConfigurationErrorsException(exception1.Message, settings.ElementInformation.Properties["type"].Source, settings.ElementInformation.Properties["type"].LineNumber);
            }
            return(__base);
        }
Ejemplo n.º 5
0
        public static ProviderBase InstantiateProvider(ProviderSettings providerSettings, Type providerType)
        {
            Type settingsType = Type.GetType(providerSettings.Type);

            if (settingsType == null)
            {
                throw new ConfigurationErrorsException(String.Format("Could not find type: {0}", providerSettings.Type));
            }
            if (!providerType.IsAssignableFrom(settingsType))
            {
                throw new ConfigurationErrorsException(String.Format("Provider '{0}' must subclass from '{1}'", providerSettings.Name, providerType));
            }

            ProviderBase provider = Activator.CreateInstance(settingsType) as ProviderBase;

            provider.Initialize(providerSettings.Name, providerSettings.Parameters);

            return(provider);
        }
        /// <summary>
        /// Instantiates the provider.
        /// </summary>
        /// <param name="providerSettings">The provider settings.</param>
        /// <param name="provType">Type of the prov.</param>
        /// <returns></returns>
        public static ProviderBase InstantiateProvider(ProviderSettings providerSettings, Type provType)
        {
            if ((providerSettings.Type == null) || (providerSettings.Type.Length < 1))
            {
                throw new ConfigurationErrorsException(
                          "Provider could not be instantiated. The Type parameter cannot be null.");
            }

            Type providerType = Type.GetType(providerSettings.Type);

            if (providerType == null)
            {
                throw new ConfigurationErrorsException(
                          "Provider could not be instantiated. The Type could not be found.");
            }

            if (!provType.IsAssignableFrom(providerType))
            {
                throw new ConfigurationErrorsException("Provider must implement type \'" + provType.ToString() + "\'.");
            }

            object providerObj = Activator.CreateInstance(providerType);

            if (providerObj == null)
            {
                throw new ConfigurationErrorsException("Provider could not be instantiated.");
            }

            ProviderBase providerBase = ((ProviderBase)providerObj);

            try
            {
                providerBase.Initialize(providerSettings.Name, providerSettings.Parameters);
                return(providerBase);
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        private static ProviderBase CreateProvider(string name, string providerTypeName, NameValueCollection parameters)
        {
            Type type = Type.GetType(providerTypeName);

            if (type == null)
            {
                throw new ConfigurationErrorsException(string.Format("无法加载提供程序类型 {0}", providerTypeName));
            }


            ProviderBase provider = SecureUtil.SecureCreateInstance(type, null, true) as ProviderBase;

            if (provider == null)
            {
                throw new ConfigurationErrorsException(string.Format("未能创建提供程序类型 {0}", providerTypeName));
            }

            NameValueCollection copy = new NameValueCollection(parameters);

            provider.Initialize(name, copy);

            return(provider);
        }
Ejemplo n.º 10
0
    /// <summary>
    /// Instantiates the provider.
    /// </summary>
    /// <param name="providerSettings">The settings.</param>
    /// <param name="providerType">Type of the provider to be instantiated.</param>
    /// <returns></returns>
    public static ProviderBase InstantiateProvider(ProviderSettings providerSettings, Type providerType)
    {
        ProviderBase base2 = null;

        try
        {
            string str = (providerSettings.Type == null) ? null : providerSettings.Type.Trim();
            if (string.IsNullOrEmpty(str))
            {
                throw new ArgumentException("Provider type name is invalid");
            }
            Type c = Type.GetType(str, true, true);
            if (!providerType.IsAssignableFrom(c))
            {
                throw new ArgumentException(String.Format("Provider must implement type {0}.", providerType.ToString()));
            }
            base2 = (ProviderBase)Activator.CreateInstance(c);
            NameValueCollection parameters = providerSettings.Parameters;
            NameValueCollection config     = new NameValueCollection(parameters.Count, StringComparer.Ordinal);
            foreach (string str2 in parameters)
            {
                config[str2] = parameters[str2];
            }
            base2.Initialize(providerSettings.Name, config);
        }
        catch (Exception exception)
        {
            if (exception is ConfigurationException)
            {
                throw;
            }
            throw new ConfigurationErrorsException(exception.Message,
                                                   providerSettings.ElementInformation.Properties["type"].Source,
                                                   providerSettings.ElementInformation.Properties["type"].LineNumber);
        }
        return(base2);
    }
Ejemplo n.º 11
0
        private static ProviderBase InstantiateProvider(ProviderSettings providerSettings, Type providerType)
        {
            ProviderBase providerBase = null;

            try
            {
                string typeName = (providerSettings.Type == null) ? null : providerSettings.Type.Trim();
                if (string.IsNullOrEmpty(typeName))
                {
                    throw new ArgumentException("No provider type name found.");
                }
                Type newType = Type.GetType(typeName);
                if (!providerType.IsAssignableFrom(newType))
                {
                    throw new ArgumentException(string.Format("The provider must implement {0}", providerType.ToString()));
                }
                providerBase = (ProviderBase)Activator.CreateInstance(newType);
                //clone a name value collection
                NameValueCollection collection1 = providerSettings.Parameters;
                NameValueCollection collection2 = new NameValueCollection(collection1.Count);
                foreach (string textName in collection1)
                {
                    collection2[textName] = collection1[textName];
                }
                providerBase.Initialize(providerSettings.Name, collection2);
            }
            catch (Exception exception)
            {
                if (exception is ConfigurationException)
                {
                    throw;
                }
                throw new ConfigurationErrorsException(exception.Message, providerSettings.ElementInformation.Properties["type"].Source, providerSettings.ElementInformation.Properties["type"].LineNumber);
            }
            return(providerBase);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Instantiates the provider.
        /// </summary>
        /// <param name="providerSettings">The settings.</param>
        /// <param name="providerType">Type of the provider to be instantiated.</param>
        /// <returns></returns>
        public static ProviderBase InstantiateProvider(ProviderSettings providerSettings, Type providerType)
        {
            ProviderBase base2 = null;

            try
            {
                string str = (providerSettings.Type == null) ? null : providerSettings.Type.Trim();

                if (string.IsNullOrEmpty(str))
                {
                    throw new ArgumentException("Provider type name is invalid");
                }

                string appDir = null;

                // Unified way to get Application Path

                // if RelativeSearchPath is null or empty, check in normal way
                if (string.IsNullOrEmpty(AppDomain.CurrentDomain.RelativeSearchPath))
                {
                    appDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                }
                // otherwise check for web application
                else
                {
                    appDir = AppDomain.CurrentDomain.RelativeSearchPath;
                }

                NameValueCollection parameters = providerSettings.Parameters;
                NameValueCollection config     = new NameValueCollection(parameters.Count, StringComparer.Ordinal);

                foreach (string str2 in parameters)
                {
                    config[str2] = parameters[str2];
                }

                string dll = config["assembly"].ToString();

                Assembly assembly = Assembly.LoadFile(appDir + "\\" + dll);

                Type type = assembly.GetType(str, true, true);

                if (!providerType.IsAssignableFrom(type))
                {
                    throw new ArgumentException(String.Format("Provider must implement type {0}.", providerType.ToString()));
                }

                base2 = (ProviderBase)Activator.CreateInstance(type);

                base2.Initialize(providerSettings.Name, config);
            }

            catch (Exception exception)
            {
                if (exception is ConfigurationException)
                {
                    throw;
                }

                throw new ConfigurationErrorsException(exception.Message,
                                                       providerSettings.ElementInformation.Properties["type"].Source,
                                                       providerSettings.ElementInformation.Properties["type"].LineNumber);
            }

            return(base2);
        }