10
10
using System . Net . Http ;
11
11
using System . Resources ;
12
12
using System . Threading . Tasks ;
13
+ using Microsoft . Azure . WebJobs . Script . Config ;
13
14
using Microsoft . Azure . WebJobs . Script . Configuration ;
14
15
using Microsoft . Azure . WebJobs . Script . Diagnostics . Extensions ;
15
16
using Microsoft . Azure . WebJobs . Script . Models ;
16
17
using Microsoft . Extensions . Logging ;
18
+ using Microsoft . WindowsAzure . Storage . Shared . Protocol ;
17
19
using Newtonsoft . Json ;
18
20
using NuGet . Versioning ;
19
21
@@ -23,16 +25,18 @@ public class ExtensionBundleManager : IExtensionBundleManager
23
25
{
24
26
private readonly IEnvironment _environment ;
25
27
private readonly ExtensionBundleOptions _options ;
28
+ private readonly FunctionsHostingConfigOptions _configOption ;
26
29
private readonly ILogger _logger ;
27
30
private readonly string _cdnUri ;
28
31
private string _extensionBundleVersion ;
29
32
30
- public ExtensionBundleManager ( ExtensionBundleOptions options , IEnvironment environment , ILoggerFactory loggerFactory )
33
+ public ExtensionBundleManager ( ExtensionBundleOptions options , IEnvironment environment , ILoggerFactory loggerFactory , FunctionsHostingConfigOptions configOption )
31
34
{
32
35
_environment = environment ?? throw new ArgumentNullException ( nameof ( environment ) ) ;
33
36
_logger = loggerFactory . CreateLogger < ExtensionBundleManager > ( ) ?? throw new ArgumentNullException ( nameof ( loggerFactory ) ) ;
34
37
_cdnUri = _environment . GetEnvironmentVariable ( EnvironmentSettingNames . ExtensionBundleSourceUri ) ?? ScriptConstants . ExtensionBundleDefaultSourceUri ;
35
38
_options = options ?? throw new ArgumentNullException ( nameof ( options ) ) ;
39
+ _configOption = configOption ?? throw new ArgumentNullException ( nameof ( configOption ) ) ;
36
40
}
37
41
38
42
public async Task < ExtensionBundleDetails > GetExtensionBundleDetails ( )
@@ -128,7 +132,7 @@ internal bool TryLocateExtensionBundle(out string bundlePath)
128
132
if ( FileUtility . DirectoryExists ( path ) )
129
133
{
130
134
var bundleDirectories = FileUtility . EnumerateDirectories ( path ) ;
131
- string version = FindBestVersionMatch ( _options . Version , bundleDirectories ) ;
135
+ string version = FindBestVersionMatch ( _options . Version , bundleDirectories , _options . Id , _configOption ) ;
132
136
133
137
if ( ! string . IsNullOrEmpty ( version ) )
134
138
{
@@ -236,7 +240,8 @@ private async Task<string> GetLatestMatchingBundleVersionAsync(HttpClient httpCl
236
240
237
241
var content = await response . Content . ReadAsStringAsync ( ) ;
238
242
var bundleVersions = JsonConvert . DeserializeObject < IEnumerable < string > > ( content ) ;
239
- var matchingBundleVersion = FindBestVersionMatch ( _options . Version , bundleVersions ) ;
243
+
244
+ var matchingBundleVersion = FindBestVersionMatch ( _options . Version , bundleVersions , _options . Id , _configOption ) ;
240
245
241
246
if ( string . IsNullOrEmpty ( matchingBundleVersion ) )
242
247
{
@@ -246,7 +251,7 @@ private async Task<string> GetLatestMatchingBundleVersionAsync(HttpClient httpCl
246
251
return matchingBundleVersion ;
247
252
}
248
253
249
- private static string FindBestVersionMatch ( VersionRange versionRange , IEnumerable < string > versions )
254
+ internal static string FindBestVersionMatch ( VersionRange versionRange , IEnumerable < string > versions , string bundleId , FunctionsHostingConfigOptions configOption )
250
255
{
251
256
var bundleVersions = versions . Select ( p =>
252
257
{
@@ -259,7 +264,29 @@ private static string FindBestVersionMatch(VersionRange versionRange, IEnumerabl
259
264
return version ;
260
265
} ) . Where ( v => v != null ) ;
261
266
262
- return bundleVersions . OrderByDescending ( version => version . Version ) . FirstOrDefault ( ) ? . ToString ( ) ;
267
+ var matchingVersion = bundleVersions . OrderByDescending ( version => version . Version ) . FirstOrDefault ( ) ;
268
+
269
+ if ( bundleId != ScriptConstants . DefaultExtensionBundleId )
270
+ {
271
+ return matchingVersion ? . ToString ( ) ;
272
+ }
273
+
274
+ var maximumBundleV3Version = NuGetVersion . Parse ( configOption . MaximumSupportedBundleV3Version ) ;
275
+ matchingVersion = matchingVersion ? . Major == 3 && matchingVersion > maximumBundleV3Version
276
+ ? maximumBundleV3Version
277
+ : matchingVersion ;
278
+
279
+ var maximumBundleV4Version = NuGetVersion . Parse ( configOption . MaximumSupportedBundleV4Version ) ;
280
+ matchingVersion = matchingVersion ? . Major == 4 && matchingVersion > maximumBundleV4Version
281
+ ? maximumBundleV4Version
282
+ : matchingVersion ;
283
+
284
+ if ( matchingVersion ? . Major > 4 )
285
+ {
286
+ throw new HostInitializationException ( $ "Referenced bundle { bundleId } of version { matchingVersion } does not meet the requirements for maximum supported version for extension bundle. Update your extension bundle reference in host.json to reference bundle version { maximumBundleV4Version } or lower.") ;
287
+ }
288
+
289
+ return matchingVersion ? . ToString ( ) ;
263
290
}
264
291
265
292
public async Task < string > GetExtensionBundleBinPathAsync ( )
0 commit comments