forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildManagerViewEngine.cs
More file actions
163 lines (145 loc) · 6.18 KB
/
Copy pathBuildManagerViewEngine.cs
File metadata and controls
163 lines (145 loc) · 6.18 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// 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.Diagnostics.Contracts;
using System.Threading;
using System.Web.Hosting;
using System.Web.WebPages;
using Microsoft.Internal.Web.Utils;
namespace System.Web.Mvc
{
public abstract class BuildManagerViewEngine : VirtualPathProviderViewEngine
{
private static object _isPrecompiledNonUpdateableSiteInitializedLock = new object();
private static bool _isPrecompiledNonUpdateableSite;
private static bool _isPrecompiledNonUpdateableSiteInitialized;
private static FileExistenceCache _sharedFileExistsCache;
private IBuildManager _buildManager;
private IViewPageActivator _viewPageActivator;
private IResolver<IViewPageActivator> _activatorResolver;
private FileExistenceCache _fileExistsCache;
protected BuildManagerViewEngine()
: this(null, null, null, null)
{
}
protected BuildManagerViewEngine(IViewPageActivator viewPageActivator)
: this(viewPageActivator, null, null, null)
{
}
internal BuildManagerViewEngine(IViewPageActivator viewPageActivator, IResolver<IViewPageActivator> activatorResolver,
IDependencyResolver dependencyResolver, VirtualPathProvider pathProvider)
{
if (viewPageActivator != null)
{
_viewPageActivator = viewPageActivator;
}
else
{
_activatorResolver = activatorResolver ?? new SingleServiceResolver<IViewPageActivator>(
() => null,
new DefaultViewPageActivator(dependencyResolver),
"BuildManagerViewEngine constructor");
}
if (pathProvider != null)
{
Func<VirtualPathProvider> providerFunc = () => pathProvider;
_fileExistsCache = new FileExistenceCache(providerFunc);
VirtualPathProviderFunc = providerFunc;
}
else
{
if (_sharedFileExistsCache == null)
{
// Startup initialization race is OK providing service remains read-only
_sharedFileExistsCache = new FileExistenceCache(() => HostingEnvironment.VirtualPathProvider);
}
_fileExistsCache = _sharedFileExistsCache;
}
}
internal IBuildManager BuildManager
{
get
{
if (_buildManager == null)
{
_buildManager = new BuildManagerWrapper();
}
return _buildManager;
}
set { _buildManager = value; }
}
protected IViewPageActivator ViewPageActivator
{
get
{
if (_viewPageActivator != null)
{
return _viewPageActivator;
}
_viewPageActivator = _activatorResolver.Current;
return _viewPageActivator;
}
}
protected virtual bool IsPrecompiledNonUpdateableSite
{
get
{
return LazyInitializer.EnsureInitialized(ref _isPrecompiledNonUpdateableSite,
ref _isPrecompiledNonUpdateableSiteInitialized,
ref _isPrecompiledNonUpdateableSiteInitializedLock,
GetPrecompiledNonUpdateable);
}
}
protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
{
// When dealing with non-updateable precompiled views, the view files may not exist on disk. The correct
// way to check for existence of a file in this case is by querying the BuildManager.
// For all other scenarios, checking for files on disk is faster and should suffice.
Contract.Assert(_fileExistsCache != null);
return _fileExistsCache.FileExists(virtualPath) ||
(IsPrecompiledNonUpdateableSite && BuildManager.FileExists(virtualPath));
}
private static bool GetPrecompiledNonUpdateable()
{
IVirtualPathUtility virtualPathUtility = new VirtualPathUtilityWrapper();
return WebPages.BuildManagerWrapper.IsNonUpdateablePrecompiledApp(HostingEnvironment.VirtualPathProvider,
virtualPathUtility);
}
internal class DefaultViewPageActivator : IViewPageActivator
{
private Func<IDependencyResolver> _resolverThunk;
public DefaultViewPageActivator()
: this(null)
{
}
public DefaultViewPageActivator(IDependencyResolver resolver)
{
if (resolver == null)
{
_resolverThunk = () => DependencyResolver.Current;
}
else
{
_resolverThunk = () => resolver;
}
}
public object Create(ControllerContext controllerContext, Type type)
{
try
{
return _resolverThunk().GetService(type) ?? Activator.CreateInstance(type);
}
catch (MissingMethodException exception)
{
// Ensure thrown exception contains the type name. Might be down a few levels.
MissingMethodException replacementException =
TypeHelpers.EnsureDebuggableException(exception, type.FullName);
if (replacementException != null)
{
throw replacementException;
}
throw;
}
}
}
}
}