@@ -7,9 +7,10 @@ namespace Our.Umbraco.FileSystemProviders.Azure
7
7
{
8
8
using System ;
9
9
using System . Diagnostics . CodeAnalysis ;
10
+ using System . Reflection ;
10
11
using System . Web ;
12
+ using System . Web . Compilation ;
11
13
using System . Web . Hosting ;
12
-
13
14
using global ::Umbraco . Core . IO ;
14
15
15
16
/// <summary>
@@ -47,13 +48,8 @@ public FileSystemVirtualPathProvider(string pathPrefix, Lazy<IFileSystem> fileSy
47
48
throw new ArgumentNullException ( nameof ( pathPrefix ) ) ;
48
49
}
49
50
50
- if ( fileSystem == null )
51
- {
52
- throw new ArgumentNullException ( nameof ( fileSystem ) ) ;
53
- }
54
-
55
51
this . pathPrefix = this . FormatVirtualPathPrefix ( pathPrefix ) ;
56
- this . fileSystem = fileSystem ;
52
+ this . fileSystem = fileSystem ?? throw new ArgumentNullException ( nameof ( fileSystem ) ) ;
57
53
}
58
54
59
55
/// <summary>
@@ -83,7 +79,36 @@ public static void Configure<TProviderTypeFilter>(string pathPrefix = Constants.
83
79
84
80
Lazy < IFileSystem > fileSystem = new Lazy < IFileSystem > ( ( ) => FileSystemProviderManager . Current . GetFileSystemProvider < TProviderTypeFilter > ( ) ) ;
85
81
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
+ }
87
112
}
88
113
89
114
/// <summary>
0 commit comments