@@ -252,45 +252,67 @@ private async Task<string> GetLatestMatchingBundleVersionAsync(HttpClient httpCl
252
252
253
253
internal string FindBestVersionMatch ( VersionRange versionRange , IEnumerable < string > versions , string bundleId , FunctionsHostingConfigOptions configOption )
254
254
{
255
+ int dotnetVersion = typeof ( string ) . Assembly . GetName ( ) . Version . Major ;
256
+
257
+ // Limit the version range for v4.x bundles on .NET 6 to be between 4.2.0 and 4.22.0
258
+ // Return original version range if not .NET 6 or not v4.x bundle
259
+ var effectiveVersionRange = GetAdjustedVersion ( dotnetVersion , versionRange , bundleId ) ;
260
+
261
+ // Check if there is a max version configured in hosting config for the current dotnet version and bundle major version
262
+ if ( TryGetMaxBundleVersionFromHostingConfig ( bundleId , versionRange . MinVersion . Major , dotnetVersion , configOption , out NuGetVersion maxBundleVersion ) )
263
+ {
264
+ effectiveVersionRange = new VersionRange ( effectiveVersionRange . MinVersion , effectiveVersionRange . IsMinInclusive , maxBundleVersion , true ) ;
265
+ }
266
+
255
267
var bundleVersions = versions . Select ( p =>
256
268
{
257
269
var dirName = Path . GetFileName ( p ) ;
258
270
NuGetVersion . TryParse ( dirName , out NuGetVersion version ) ;
259
271
if ( version != null )
260
272
{
261
- version = versionRange . Satisfies ( version ) ? version : null ;
273
+ version = effectiveVersionRange . Satisfies ( version ) ? version : null ;
262
274
}
263
275
return version ;
264
276
} ) . Where ( v => v != null ) . OrderByDescending ( version => version . Version ) . ToList ( ) ;
265
277
266
278
var matchingVersion = ResolvePlatformReleaseChannelVersion ( bundleVersions ) ;
267
279
268
- if ( bundleId != ScriptConstants . DefaultExtensionBundleId )
269
- {
270
- return matchingVersion ? . ToString ( ) ;
271
- }
280
+ return matchingVersion ? . ToString ( ) ;
281
+ }
272
282
273
- // Check to see if there is a max bundle version set via hosting configuration, if yes then use that instead of the one
274
- // available on VM or local machine. Only use MaximumBundleV3Version or MaximumBundleV4Version if the version configured
275
- // by the customer resolved to version higher than the version set via hosting config.
276
- if ( ! string . IsNullOrEmpty ( configOption . MaximumBundleV3Version )
277
- && matchingVersion ? . Major == ScriptConstants . ExtensionBundleV3MajorVersion )
283
+ // Applies bundle version limits for .NET 6, using the default bundle if referencing a v4 major bundle version.
284
+ private VersionRange GetAdjustedVersion ( int dotnetVersion , VersionRange versionRange , string bundleId )
285
+ {
286
+ if ( dotnetVersion != 6
287
+ || ! string . Equals ( bundleId , ScriptConstants . DefaultExtensionBundleId , StringComparison . OrdinalIgnoreCase )
288
+ || versionRange . MinVersion . Major != ScriptConstants . ExtensionBundleV4MajorVersion )
278
289
{
279
- var maximumBundleV3Version = NuGetVersion . Parse ( configOption . MaximumBundleV3Version ) ;
280
- matchingVersion = matchingVersion > maximumBundleV3Version ? maximumBundleV3Version : matchingVersion ;
281
- return matchingVersion ? . ToString ( ) ;
290
+ return versionRange ;
282
291
}
283
292
284
- if ( ! string . IsNullOrEmpty ( configOption . MaximumBundleV4Version )
285
- && matchingVersion ? . Major == ScriptConstants . ExtensionBundleV4MajorVersion )
286
- {
287
- var maximumBundleV4Version = NuGetVersion . Parse ( configOption . MaximumBundleV4Version ) ;
288
- matchingVersion = matchingVersion > maximumBundleV4Version
289
- ? maximumBundleV4Version
290
- : matchingVersion ;
291
- }
293
+ var effectiveMinVersion = new NuGetVersion ( ScriptConstants . Net6MinimumV4BundleVersion ) ;
294
+ var effectiveMaxVersion = new NuGetVersion ( ScriptConstants . Net6MaximumV4BundleVersion ) ;
292
295
293
- return matchingVersion ? . ToString ( ) ;
296
+ var minVersion = versionRange . MinVersion < effectiveMinVersion ? effectiveMinVersion : versionRange . MinVersion ;
297
+ var maxVersion = versionRange . MaxVersion > effectiveMaxVersion ? effectiveMaxVersion : versionRange . MaxVersion ;
298
+
299
+ // Use the original inclusion values if it is within the min or max range.
300
+ bool isMinInclusive = versionRange . MinVersion > effectiveMinVersion && versionRange . IsMinInclusive ;
301
+ bool isMaxInclusive = versionRange . MaxVersion < effectiveMaxVersion && versionRange . IsMaxInclusive ;
302
+
303
+ versionRange = new VersionRange ( minVersion , isMinInclusive , maxVersion , isMaxInclusive ) ;
304
+ return versionRange ;
305
+ }
306
+
307
+ private bool TryGetMaxBundleVersionFromHostingConfig ( string bundleId , int bundleVersion , int dotnetVersion , FunctionsHostingConfigOptions configOption , out NuGetVersion maxVersion )
308
+ {
309
+ maxVersion = null ;
310
+ string hostingConfig = $ "Net{ dotnetVersion } MaximumBundleV{ bundleVersion } Version";
311
+ string hostingConfigValue = configOption . GetFeature ( hostingConfig ) ;
312
+
313
+ return string . Equals ( bundleId , ScriptConstants . DefaultExtensionBundleId , StringComparison . OrdinalIgnoreCase )
314
+ && ! string . IsNullOrEmpty ( hostingConfigValue )
315
+ && NuGetVersion . TryParse ( hostingConfigValue , out maxVersion ) ;
294
316
}
295
317
296
318
private NuGetVersion ResolvePlatformReleaseChannelVersion ( IList < NuGetVersion > orderedByDescBundles ) => _platformReleaseChannel . ToUpper ( ) switch
0 commit comments