/// <summary>
        /// Initializes the providers (Content Provider, Link Resolver, Media Helper, etc.) using dependency injection, i.e. obtained from configuration.
        /// </summary>
        /// <param name="dependencyResolver">A delegate that provide an implementation instance for a given interface type.</param>
        /// <remarks>
        /// This method took a parameter of type <see cref="System.Web.Mvc.IDependencyResolver"/> in DXA 1.1 and 1.2.
        /// That created an undesirable dependency on System.Web.Mvc and therefore this has been changed to a delegate in DXA 1.3.
        /// We couldn't keep the old signature (as deprecated) because that would still result in a dependency on System.Web.Mvc.
        /// This means that the call in Global.asax.cs must be changed from
        /// <c>SiteConfiguration.InitializeProviders(DependencyResolver.Current);</c> to
        /// <c>SiteConfiguration.InitializeProviders(DependencyResolver.Current.GetService);</c>
        /// </remarks>
        public static void InitializeProviders(Func <Type, object> dependencyResolver)
        {
            // Initialize the Logger before logging anything:
            Logger = (ILogger)dependencyResolver(typeof(ILogger));

            using (new Tracer())
            {
                string assemblyFileVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion;
                Log.Info("-------- Initializing DXA Framework v{0} --------", assemblyFileVersion);

                if (Logger != null)
                {
                    Log.Info("Using implementation type '{0}' for interface ILogger.", Logger.GetType().FullName);
                }

                CacheProvider              = GetProvider <ICacheProvider>(dependencyResolver);
                BinaryProvider             = GetProvider <IBinaryProvider>(dependencyResolver, isOptional: true);
                ModelServiceProvider       = GetProvider <IModelServiceProvider>(dependencyResolver);
                ContentProvider            = GetProvider <IContentProvider>(dependencyResolver);
                NavigationProvider         = GetProvider <INavigationProvider>(dependencyResolver);
                ContextClaimsProvider      = GetProvider <IContextClaimsProvider>(dependencyResolver, isOptional: true);
                LinkResolver               = GetProvider <ILinkResolver>(dependencyResolver);
                ConditionalEntityEvaluator = GetProvider <IConditionalEntityEvaluator>(dependencyResolver, isOptional: true);
                MediaHelper                = GetProvider <IMediaHelper>(dependencyResolver);
                LocalizationResolver       = GetProvider <ILocalizationResolver>(dependencyResolver);
                UnknownLocalizationHandler = GetProvider <IUnknownLocalizationHandler>(dependencyResolver, isOptional: true);
            }
        }
Ejemplo n.º 2
0
        private static void BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext     context     = application.Context;
            HttpRequest     request     = context.Request;
            HttpResponse    response    = context.Response;
            string          url         = request.Url.AbsolutePath;

            using (new Tracer(sender, e, url))
            {
                // Attempt to determine Localization
                Localization localization = null;
                try
                {
                    localization = WebRequestContext.Localization;
                }
                catch (DxaUnknownLocalizationException ex)
                {
                    IUnknownLocalizationHandler unknownLocalizationHandler = SiteConfiguration.UnknownLocalizationHandler;
                    if (unknownLocalizationHandler != null)
                    {
                        localization = unknownLocalizationHandler.HandleUnknownLocalization(ex, request, response);
                    }

                    if (localization == null)
                    {
                        // There was no Unknown Localization Handler or it didn't terminate the request processing using response.End()
                        // and it also didn't resolve a Localization.
                        SendNotFoundResponse(ex.Message, response);
                    }
                }
                catch (DxaItemNotFoundException ex)
                {
                    // Localization has been resolved, but could not be initialized (e.g. Version.json not found)
                    Log.Error(ex);
                    SendNotFoundResponse(ex.Message, response);
                }
                catch (Exception ex)
                {
                    // Other exceptions: log and let ASP.NET handle them
                    Log.Error(ex);
                    throw;
                }
                Log.Debug("Request URL '{0}' maps to Localization [{1}]", request.Url, localization);

                // Prevent direct access to BinaryData folder
                if (url.StartsWith("/" + SiteConfiguration.StaticsFolder + "/"))
                {
                    SendNotFoundResponse(string.Format("Attempt to directly access the static content cache through URL '{0}'", url), response);
                }

                // Rewrite versioned URLs
                string versionLessUrl = SiteConfiguration.RemoveVersionFromPath(url);
                if (url != versionLessUrl)
                {
                    Log.Debug("Rewriting versioned static content URL '{0}' to '{1}'", url, versionLessUrl);
                    context.RewritePath(versionLessUrl);
                    context.Items[IsVersionedUrlContextItem] = true;
                }
            }
        }