forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAspNetRequestScopeStorageProvider.cs
More file actions
111 lines (100 loc) · 4.05 KB
/
Copy pathAspNetRequestScopeStorageProvider.cs
File metadata and controls
111 lines (100 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Web.WebPages.Resources;
namespace System.Web.WebPages.Scope
{
public class AspNetRequestScopeStorageProvider : IScopeStorageProvider
{
private static readonly object _pageScopeKey = new object();
private static readonly object _requestScopeKey = new object();
private readonly HttpContextBase _httpContext;
private readonly Func<bool> _appStartExecuted;
public AspNetRequestScopeStorageProvider()
: this(httpContext: null, appStartExecuted: () => WebPageHttpModule.AppStartExecuteCompleted)
{
}
internal AspNetRequestScopeStorageProvider(HttpContextBase httpContext, Func<bool> appStartExecuted)
{
_httpContext = httpContext;
_appStartExecuted = appStartExecuted;
ApplicationScope = new ApplicationScopeStorageDictionary();
}
[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "The state storage API is designed to allow contexts to be set")]
public IDictionary<object, object> CurrentScope
{
get { return PageScope ?? RequestScopeInternal ?? ApplicationScope; }
set
{
if (!_appStartExecuted())
{
// Disallow creating new contexts before the start page is executed.
// This makes sense because our provider is scoped to a request.
throw new InvalidOperationException(WebPageResources.StateStorage_StorageScopesCannotBeCreated);
}
PageScope = value;
}
}
public IDictionary<object, object> GlobalScope
{
get { return ApplicationScope; }
}
public IDictionary<object, object> ApplicationScope { get; private set; }
public IDictionary<object, object> RequestScope
{
get
{
var requestContext = RequestScopeInternal;
if (requestContext == null)
{
throw new InvalidOperationException(WebPageResources.StateStorage_RequestScopeNotAvailable);
}
return requestContext;
}
}
private HttpContextBase HttpContext
{
get
{
// If a http context is specifically provided, use that. Else return the value from System.Web.HttpContext.Current if its available.
var currentHttpContext = Web.HttpContext.Current;
return _httpContext ?? (currentHttpContext == null ? null : new HttpContextWrapper(currentHttpContext));
}
}
private IDictionary<object, object> RequestScopeInternal
{
get
{
if (_appStartExecuted())
{
var requestContext = (IDictionary<object, object>)HttpContext.Items[_requestScopeKey];
if (requestContext == null)
{
HttpContext.Items[_requestScopeKey] = requestContext = new ScopeStorageDictionary(ApplicationScope);
}
return requestContext;
}
return null;
}
}
private IDictionary<object, object> PageScope
{
get
{
if (HttpContext == null)
{
return null;
}
return (IDictionary<object, object>)HttpContext.Items[_pageScopeKey];
}
set
{
// This call would be guarded by the CurrentContext setter.
Debug.Assert(HttpContext != null);
HttpContext.Items[_pageScopeKey] = value;
}
}
}
}