Skip to content

Commit cf0d702

Browse files
Ensure VPP is register in precompiled mode. Fix #71
1 parent db3d093 commit cf0d702

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

src/UmbracoFileSystemProviders.Azure/FileSystemVirtualPathProvider.cs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ namespace Our.Umbraco.FileSystemProviders.Azure
77
{
88
using System;
99
using System.Diagnostics.CodeAnalysis;
10+
using System.Reflection;
1011
using System.Web;
12+
using System.Web.Compilation;
1113
using System.Web.Hosting;
12-
1314
using global::Umbraco.Core.IO;
1415

1516
/// <summary>
@@ -47,13 +48,8 @@ public FileSystemVirtualPathProvider(string pathPrefix, Lazy<IFileSystem> fileSy
4748
throw new ArgumentNullException(nameof(pathPrefix));
4849
}
4950

50-
if (fileSystem == null)
51-
{
52-
throw new ArgumentNullException(nameof(fileSystem));
53-
}
54-
5551
this.pathPrefix = this.FormatVirtualPathPrefix(pathPrefix);
56-
this.fileSystem = fileSystem;
52+
this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
5753
}
5854

5955
/// <summary>
@@ -83,7 +79,36 @@ public static void Configure<TProviderTypeFilter>(string pathPrefix = Constants.
8379

8480
Lazy<IFileSystem> fileSystem = new Lazy<IFileSystem>(() => FileSystemProviderManager.Current.GetFileSystemProvider<TProviderTypeFilter>());
8581
FileSystemVirtualPathProvider provider = new FileSystemVirtualPathProvider(pathPrefix, fileSystem);
86-
HostingEnvironment.RegisterVirtualPathProvider(provider);
82+
83+
// The standard HostingEnvironment.RegisterVirtualPathProvider(virtualPathProvider) method is ignored when
84+
// BuildManager.IsPrecompiledApp is true so we have to use reflection when registering the provider.
85+
if (!BuildManager.IsPrecompiledApp)
86+
{
87+
HostingEnvironment.RegisterVirtualPathProvider(provider);
88+
}
89+
else
90+
{
91+
// Gets the private _theHostingEnvironment reference.
92+
HostingEnvironment hostingEnvironmentInstance = (HostingEnvironment)typeof(HostingEnvironment)
93+
.InvokeMember("_theHostingEnvironment", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField, null, null, null);
94+
95+
if (hostingEnvironmentInstance == null)
96+
{
97+
return;
98+
}
99+
100+
// Get the static internal MethodInfo for RegisterVirtualPathProviderInternal method.
101+
MethodInfo methodInfo = typeof(HostingEnvironment)
102+
.GetMethod("RegisterVirtualPathProviderInternal", BindingFlags.NonPublic | BindingFlags.Static);
103+
104+
if (methodInfo == null)
105+
{
106+
return;
107+
}
108+
109+
// Invoke RegisterVirtualPathProviderInternal method with one argument which is the instance of our own provider.
110+
methodInfo.Invoke(hostingEnvironmentInstance, new object[] { provider });
111+
}
87112
}
88113

89114
/// <summary>

0 commit comments

Comments
 (0)