Exemplo n.º 1
0
        // initializes a new instance of the UmbracoContext class
        // internal for unit tests
        // otherwise it's used by EnsureContext above
        // warn: does *not* manage setting any IUmbracoContextAccessor
        internal UmbracoContext(HttpContextBase httpContext,
                                IPublishedSnapshotService publishedSnapshotService,
                                WebSecurity webSecurity,
                                IUmbracoSettingsSection umbracoSettings,
                                IEnumerable <IUrlProvider> urlProviders,
                                IEnumerable <IMediaUrlProvider> mediaUrlProviders,
                                IGlobalSettings globalSettings,
                                IVariationContextAccessor variationContextAccessor)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }
            if (publishedSnapshotService == null)
            {
                throw new ArgumentNullException(nameof(publishedSnapshotService));
            }
            if (webSecurity == null)
            {
                throw new ArgumentNullException(nameof(webSecurity));
            }
            if (umbracoSettings == null)
            {
                throw new ArgumentNullException(nameof(umbracoSettings));
            }
            if (urlProviders == null)
            {
                throw new ArgumentNullException(nameof(urlProviders));
            }
            if (mediaUrlProviders == null)
            {
                throw new ArgumentNullException(nameof(mediaUrlProviders));
            }
            VariationContextAccessor = variationContextAccessor ?? throw new ArgumentNullException(nameof(variationContextAccessor));
            _globalSettings          = globalSettings ?? throw new ArgumentNullException(nameof(globalSettings));

            // ensure that this instance is disposed when the request terminates, though we *also* ensure
            // this happens in the Umbraco module since the UmbracoCOntext is added to the HttpContext items.
            //
            // also, it *can* be returned by the container with a PerRequest lifetime, meaning that the
            // container *could* also try to dispose it.
            //
            // all in all, this context may be disposed more than once, but DisposableObject ensures that
            // it is ok and it will be actually disposed only once.
            httpContext.DisposeOnPipelineCompleted(this);

            ObjectCreated    = DateTime.Now;
            UmbracoRequestId = Guid.NewGuid();
            HttpContext      = httpContext;
            Security         = webSecurity;

            // beware - we cannot expect a current user here, so detecting preview mode must be a lazy thing
            _publishedSnapshot = new Lazy <IPublishedSnapshot>(() => publishedSnapshotService.CreatePublishedSnapshot(PreviewToken));

            // set the urls...
            // NOTE: The request will not be available during app startup so we can only set this to an absolute URL of localhost, this
            // is a work around to being able to access the UmbracoContext during application startup and this will also ensure that people
            // 'could' still generate URLs during startup BUT any domain driven URL generation will not work because it is NOT possible to get
            // the current domain during application startup.
            // see: http://issues.umbraco.org/issue/U4-1890
            //
            OriginalRequestUrl = GetRequestFromContext()?.Url ?? new Uri("http://localhost");
            CleanedUmbracoUrl  = UriUtility.UriToUmbraco(OriginalRequestUrl);
            UrlProvider        = new UrlProvider(this, umbracoSettings.WebRouting, urlProviders, mediaUrlProviders, variationContextAccessor);
        }