/// <summary> /// Instances this instance. /// </summary> /// <returns></returns> public static UrlBuilderProvider Instance() { // Use the cache because the reflection used later is expensive Cache cache = HttpRuntime.Cache; string cacheKey; // Get the names of providers ProviderConfiguration config = ProviderConfiguration.GetProviderConfiguration(providerType); // Read specific configuration information for this provider //ProviderSettings providerSettings = (ProviderSettings) config.Providers[config.DefaultProvider]; AppleseedProviderSettings providerSettings = (AppleseedProviderSettings)config.Providers[config.DefaultProvider]; // In the cache? cacheKey = "Appleseed::Web::UrlBuilder::" + config.DefaultProvider; if (cache[cacheKey] == null) { // The assembly should be in \bin or GAC, so we simply need // to get an instance of the type try { cache.Insert(cacheKey, ProviderHelper.InstantiateProvider(providerSettings, typeof(UrlBuilderProvider))); } catch (Exception e) { throw new Exception("Unable to load provider", e); } } return((UrlBuilderProvider)cache[cacheKey]); }
/// <summary> /// Instantiates the provider. /// </summary> /// <param name="providerSettings"> /// The Appleseed provider settings. /// </param> /// <param name="provType"> /// Type of the prov. /// </param> /// <returns> /// A Provider Base. /// </returns> /// <remarks> /// </remarks> public static ProviderBase InstantiateProvider(AppleseedProviderSettings providerSettings, Type provType) { if (string.IsNullOrEmpty(providerSettings.Type)) { throw new ConfigurationErrorsException( "Provider could not be instantiated. The Type parameter cannot be null."); } var 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 + "\'."); } var providerObj = Activator.CreateInstance(providerType); if (providerObj == null) { throw new ConfigurationErrorsException("Provider could not be instantiated."); } var providerBase = (ProviderBase)providerObj; providerBase.Initialize(providerSettings.Name, providerSettings.Parameters); return(providerBase); }