@@ -1128,25 +1128,30 @@ private static void GenerateAutoDetectOverload(
11281128 sb . AppendLineLf ( " global::System.Collections.Generic.IEnumerable<string>? excludedPatterns = null," ) ;
11291129 sb . AppendLineLf ( " global::System.Collections.Generic.IEnumerable<global::System.Type>? excludedTypes = null)" ) ;
11301130 sb . AppendLineLf ( " {" ) ;
1131- sb . AppendLineLf ( " if (includeReferencedAssemblies)" ) ;
1132- sb . AppendLineLf ( " {" ) ;
11331131
11341132 // Build context for smart suffix calculation
11351133 var allAssemblies = new List < string > { assemblyName } ;
11361134 allAssemblies . AddRange ( referencedAssemblies . Select ( r => r . AssemblyName ) ) ;
11371135
1138- // Generate calls to all referenced assemblies (recursive)
1139- // Note: We don't pass configuration to referenced assemblies as we don't know if they need it
1140- // Each assembly manages its own conditional services and should be called directly with configuration if needed
1141- foreach ( var refAssembly in referencedAssemblies )
1136+ // Only generate the if block if there are referenced assemblies to call
1137+ if ( referencedAssemblies . Length > 0 )
11421138 {
1143- var refSmartSuffix = GetSmartMethodSuffixFromContext ( refAssembly . AssemblyName , allAssemblies ) ;
1144- var refMethodName = $ "AddDependencyRegistrationsFrom{ refSmartSuffix } ";
1145- sb . AppendLineLf ( $ " services.{ refMethodName } (includeReferencedAssemblies: true, excludedNamespaces: excludedNamespaces, excludedPatterns: excludedPatterns, excludedTypes: excludedTypes);") ;
1146- }
1139+ sb . AppendLineLf ( " if (includeReferencedAssemblies)" ) ;
1140+ sb . AppendLineLf ( " {" ) ;
11471141
1148- sb . AppendLineLf ( " }" ) ;
1149- sb . AppendLineLf ( ) ;
1142+ // Generate calls to all referenced assemblies (recursive)
1143+ // Note: We don't pass configuration to referenced assemblies as we don't know if they need it
1144+ // Each assembly manages its own conditional services and should be called directly with configuration if needed
1145+ foreach ( var refAssembly in referencedAssemblies )
1146+ {
1147+ var refSmartSuffix = GetSmartMethodSuffixFromContext ( refAssembly . AssemblyName , allAssemblies ) ;
1148+ var refMethodName = $ "AddDependencyRegistrationsFrom{ refSmartSuffix } ";
1149+ sb . AppendLineLf ( $ " services.{ refMethodName } (includeReferencedAssemblies: true, excludedNamespaces: excludedNamespaces, excludedPatterns: excludedPatterns, excludedTypes: excludedTypes);") ;
1150+ }
1151+
1152+ sb . AppendLineLf ( " }" ) ;
1153+ sb . AppendLineLf ( ) ;
1154+ }
11501155
11511156 GenerateServiceRegistrationCalls ( sb , services , includeRuntimeFiltering : true ) ;
11521157
@@ -1264,8 +1269,6 @@ private static void GenerateMultipleAssembliesOverload(
12641269 sb . AppendLineLf ( " global::System.Collections.Generic.IEnumerable<global::System.Type>? excludedTypes = null," ) ;
12651270 sb . AppendLineLf ( " params string[] referencedAssemblyNames)" ) ;
12661271 sb . AppendLineLf ( " {" ) ;
1267- sb . AppendLineLf ( " foreach (var name in referencedAssemblyNames)" ) ;
1268- sb . AppendLineLf ( " {" ) ;
12691272
12701273 // Build context for smart suffix calculation
12711274 var allAssemblies = new List < string > { assemblyName } ;
@@ -1276,24 +1279,31 @@ private static void GenerateMultipleAssembliesOverload(
12761279 . Where ( a => a . AssemblyName . StartsWith ( assemblyPrefix , StringComparison . Ordinal ) )
12771280 . ToList ( ) ;
12781281
1279- for ( var i = 0 ; i < filteredAssemblies . Count ; i ++ )
1282+ // Only generate the foreach block if there are filtered assemblies to process
1283+ if ( filteredAssemblies . Count > 0 )
12801284 {
1281- var refAssembly = filteredAssemblies [ i ] ;
1282- var refSmartSuffix = GetSmartMethodSuffixFromContext ( refAssembly . AssemblyName , allAssemblies ) ;
1283- var refMethodName = $ "AddDependencyRegistrationsFrom{ refSmartSuffix } ";
1284- var ifKeyword = i == 0 ? "if" : "else if" ;
1285+ sb . AppendLineLf ( " foreach (var name in referencedAssemblyNames)" ) ;
1286+ sb . AppendLineLf ( " {" ) ;
12851287
1286- sb . AppendLineLf ( $ " { ifKeyword } (string.Equals(name, \" { refAssembly . AssemblyName } \" , global::System.StringComparison.OrdinalIgnoreCase) ||") ;
1287- sb . AppendLineLf ( $ " string.Equals(name, \" { refAssembly . ShortName } \" , global::System.StringComparison.OrdinalIgnoreCase))") ;
1288- sb . AppendLineLf ( " {" ) ;
1288+ for ( var i = 0 ; i < filteredAssemblies . Count ; i ++ )
1289+ {
1290+ var refAssembly = filteredAssemblies [ i ] ;
1291+ var refSmartSuffix = GetSmartMethodSuffixFromContext ( refAssembly . AssemblyName , allAssemblies ) ;
1292+ var refMethodName = $ "AddDependencyRegistrationsFrom{ refSmartSuffix } ";
1293+ var ifKeyword = i == 0 ? "if" : "else if" ;
12891294
1290- sb . AppendLineLf ( $ " services.{ refMethodName } (excludedNamespaces: excludedNamespaces, excludedPatterns: excludedPatterns, excludedTypes: excludedTypes, referencedAssemblyNames: referencedAssemblyNames);") ;
1295+ sb . AppendLineLf ( $ " { ifKeyword } (string.Equals(name, \" { refAssembly . AssemblyName } \" , global::System.StringComparison.OrdinalIgnoreCase) ||") ;
1296+ sb . AppendLineLf ( $ " string.Equals(name, \" { refAssembly . ShortName } \" , global::System.StringComparison.OrdinalIgnoreCase))") ;
1297+ sb . AppendLineLf ( " {" ) ;
12911298
1292- sb . AppendLineLf ( " }" ) ;
1293- }
1299+ sb . AppendLineLf ( $ " services.{ refMethodName } (excludedNamespaces: excludedNamespaces, excludedPatterns: excludedPatterns, excludedTypes: excludedTypes, referencedAssemblyNames: referencedAssemblyNames);") ;
12941300
1295- sb . AppendLineLf ( " }" ) ;
1296- sb . AppendLineLf ( ) ;
1301+ sb . AppendLineLf ( " }" ) ;
1302+ }
1303+
1304+ sb . AppendLineLf ( " }" ) ;
1305+ sb . AppendLineLf ( ) ;
1306+ }
12971307
12981308 GenerateServiceRegistrationCalls ( sb , services , includeRuntimeFiltering : true ) ;
12991309
@@ -1313,6 +1323,9 @@ private static void GenerateServiceRegistrationCalls(
13131323 var decoratorServices = services . Where ( s => s . Decorator ) ;
13141324 var decorators = decoratorServices . ToList ( ) ;
13151325
1326+ // Determine base indentation level based on runtime filtering
1327+ var baseIndent = includeRuntimeFiltering ? " " : " " ;
1328+
13161329 // Register base services first
13171330 foreach ( var service in baseServices )
13181331 {
@@ -1349,22 +1362,25 @@ private static void GenerateServiceRegistrationCalls(
13491362 : $ "configuration.GetValue<bool>(\" { configKey } \" )";
13501363
13511364 sb . AppendLineLf ( ) ;
1352- sb . AppendLineLf ( $ " // Conditional registration for { service . ClassSymbol . Name } ") ;
1353- sb . AppendLineLf ( $ " if ({ conditionCheck } )") ;
1354- sb . AppendLineLf ( " {") ;
1365+ sb . AppendLineLf ( $ "{ baseIndent } // Conditional registration for { service . ClassSymbol . Name } ") ;
1366+ sb . AppendLineLf ( $ "{ baseIndent } if ({ conditionCheck } )") ;
1367+ sb . AppendLineLf ( $ " { baseIndent } { {") ;
13551368 }
13561369
1370+ // Determine indentation for registration calls (may be nested in conditional)
1371+ var registrationIndent = hasCondition ? baseIndent + " " : baseIndent ;
1372+
13571373 // Hosted services use AddHostedService instead of regular lifetime methods
13581374 if ( service . IsHostedService )
13591375 {
13601376 if ( isGeneric )
13611377 {
13621378 var openGenericImplementationType = GetOpenGenericTypeName ( service . ClassSymbol ) ;
1363- sb . AppendLineLf ( $ " services.AddHostedService(typeof({ openGenericImplementationType } ));") ;
1379+ sb . AppendLineLf ( $ "{ registrationIndent } services.AddHostedService(typeof({ openGenericImplementationType } ));") ;
13641380 }
13651381 else
13661382 {
1367- sb . AppendLineLf ( $ " services.AddHostedService<{ implementationType } >();") ;
1383+ sb . AppendLineLf ( $ "{ registrationIndent } services.AddHostedService<{ implementationType } >();") ;
13681384 }
13691385 }
13701386 else if ( hasInstance )
@@ -1392,19 +1408,19 @@ private static void GenerateServiceRegistrationCalls(
13921408 foreach ( var asType in service . AsTypes )
13931409 {
13941410 var serviceType = asType . ToDisplayString ( ) ;
1395- sb . AppendLineLf ( $ " services.{ lifetimeMethod } <{ serviceType } >({ instanceExpression } );") ;
1411+ sb . AppendLineLf ( $ "{ registrationIndent } services.{ lifetimeMethod } <{ serviceType } >({ instanceExpression } );") ;
13961412 }
13971413
13981414 // Also register as self if requested
13991415 if ( service . AsSelf )
14001416 {
1401- sb . AppendLineLf ( $ " services.{ lifetimeMethod } <{ implementationType } >({ instanceExpression } );") ;
1417+ sb . AppendLineLf ( $ "{ registrationIndent } services.{ lifetimeMethod } <{ implementationType } >({ instanceExpression } );") ;
14021418 }
14031419 }
14041420 else
14051421 {
14061422 // No interfaces - register as concrete type with instance
1407- sb . AppendLineLf ( $ " services.{ lifetimeMethod } <{ implementationType } >({ instanceExpression } );") ;
1423+ sb . AppendLineLf ( $ "{ registrationIndent } services.{ lifetimeMethod } <{ implementationType } >({ instanceExpression } );") ;
14081424 }
14091425 }
14101426 else if ( hasFactory )
@@ -1432,19 +1448,19 @@ private static void GenerateServiceRegistrationCalls(
14321448 foreach ( var asType in service . AsTypes )
14331449 {
14341450 var serviceType = asType . ToDisplayString ( ) ;
1435- sb . AppendLineLf ( $ " services.{ lifetimeMethod } <{ serviceType } >(sp => { implementationType } .{ service . FactoryMethodName } (sp));") ;
1451+ sb . AppendLineLf ( $ "{ registrationIndent } services.{ lifetimeMethod } <{ serviceType } >(sp => { implementationType } .{ service . FactoryMethodName } (sp));") ;
14361452 }
14371453
14381454 // Also register as self if requested
14391455 if ( service . AsSelf )
14401456 {
1441- sb . AppendLineLf ( $ " services.{ lifetimeMethod } <{ implementationType } >(sp => { implementationType } .{ service . FactoryMethodName } (sp));") ;
1457+ sb . AppendLineLf ( $ "{ registrationIndent } services.{ lifetimeMethod } <{ implementationType } >(sp => { implementationType } .{ service . FactoryMethodName } (sp));") ;
14421458 }
14431459 }
14441460 else
14451461 {
14461462 // No interfaces - register as concrete type with factory
1447- sb . AppendLineLf ( $ " services.{ lifetimeMethod } <{ implementationType } >(sp => { implementationType } .{ service . FactoryMethodName } (sp));") ;
1463+ sb . AppendLineLf ( $ "{ registrationIndent } services.{ lifetimeMethod } <{ implementationType } >(sp => { implementationType } .{ service . FactoryMethodName } (sp));") ;
14481464 }
14491465 }
14501466 else
@@ -1490,15 +1506,15 @@ private static void GenerateServiceRegistrationCalls(
14901506 var openGenericImplementationType = GetOpenGenericTypeName ( service . ClassSymbol ) ;
14911507
14921508 sb . AppendLineLf ( hasKey
1493- ? $ " services.{ lifetimeMethod } (typeof({ openGenericServiceType } ), { keyString } , typeof({ openGenericImplementationType } ));"
1494- : $ " services.{ lifetimeMethod } (typeof({ openGenericServiceType } ), typeof({ openGenericImplementationType } ));") ;
1509+ ? $ "{ registrationIndent } services.{ lifetimeMethod } (typeof({ openGenericServiceType } ), { keyString } , typeof({ openGenericImplementationType } ));"
1510+ : $ "{ registrationIndent } services.{ lifetimeMethod } (typeof({ openGenericServiceType } ), typeof({ openGenericImplementationType } ));") ;
14951511 }
14961512 else
14971513 {
14981514 // Regular non-generic registration
14991515 sb . AppendLineLf ( hasKey
1500- ? $ " services.{ lifetimeMethod } <{ serviceType } , { implementationType } >({ keyString } );"
1501- : $ " services.{ lifetimeMethod } <{ serviceType } , { implementationType } >();") ;
1516+ ? $ "{ registrationIndent } services.{ lifetimeMethod } <{ serviceType } , { implementationType } >({ keyString } );"
1517+ : $ "{ registrationIndent } services.{ lifetimeMethod } <{ serviceType } , { implementationType } >();") ;
15021518 }
15031519 }
15041520
@@ -1510,14 +1526,14 @@ private static void GenerateServiceRegistrationCalls(
15101526 var openGenericImplementationType = GetOpenGenericTypeName ( service . ClassSymbol ) ;
15111527
15121528 sb . AppendLineLf ( hasKey
1513- ? $ " services.{ lifetimeMethod } (typeof({ openGenericImplementationType } ), { keyString } );"
1514- : $ " services.{ lifetimeMethod } (typeof({ openGenericImplementationType } ));") ;
1529+ ? $ "{ registrationIndent } services.{ lifetimeMethod } (typeof({ openGenericImplementationType } ), { keyString } );"
1530+ : $ "{ registrationIndent } services.{ lifetimeMethod } (typeof({ openGenericImplementationType } ));") ;
15151531 }
15161532 else
15171533 {
15181534 sb . AppendLineLf ( hasKey
1519- ? $ " services.{ lifetimeMethod } <{ implementationType } >({ keyString } );"
1520- : $ " services.{ lifetimeMethod } <{ implementationType } >();") ;
1535+ ? $ "{ registrationIndent } services.{ lifetimeMethod } <{ implementationType } >({ keyString } );"
1536+ : $ "{ registrationIndent } services.{ lifetimeMethod } <{ implementationType } >();") ;
15211537 }
15221538 }
15231539 }
@@ -1529,22 +1545,22 @@ private static void GenerateServiceRegistrationCalls(
15291545 var openGenericImplementationType = GetOpenGenericTypeName ( service . ClassSymbol ) ;
15301546
15311547 sb . AppendLineLf ( hasKey
1532- ? $ " services.{ lifetimeMethod } (typeof({ openGenericImplementationType } ), { keyString } );"
1533- : $ " services.{ lifetimeMethod } (typeof({ openGenericImplementationType } ));") ;
1548+ ? $ "{ registrationIndent } services.{ lifetimeMethod } (typeof({ openGenericImplementationType } ), { keyString } );"
1549+ : $ "{ registrationIndent } services.{ lifetimeMethod } (typeof({ openGenericImplementationType } ));") ;
15341550 }
15351551 else
15361552 {
15371553 sb . AppendLineLf ( hasKey
1538- ? $ " services.{ lifetimeMethod } <{ implementationType } >({ keyString } );"
1539- : $ " services.{ lifetimeMethod } <{ implementationType } >();") ;
1554+ ? $ "{ registrationIndent } services.{ lifetimeMethod } <{ implementationType } >({ keyString } );"
1555+ : $ "{ registrationIndent } services.{ lifetimeMethod } <{ implementationType } >();") ;
15401556 }
15411557 }
15421558 }
15431559
15441560 // Close conditional registration check if needed
15451561 if ( hasCondition )
15461562 {
1547- sb . AppendLineLf ( " }") ;
1563+ sb . AppendLineLf ( $ " { baseIndent } } }") ;
15481564 }
15491565
15501566 // Close runtime filtering check if enabled
@@ -1580,8 +1596,8 @@ private static void GenerateServiceRegistrationCalls(
15801596 }
15811597
15821598 // Generate conditional registration check if needed
1583- var hasCondition = ! string . IsNullOrEmpty ( decorator . Condition ) ;
1584- if ( hasCondition )
1599+ var hasDecoratorCondition = ! string . IsNullOrEmpty ( decorator . Condition ) ;
1600+ if ( hasDecoratorCondition )
15851601 {
15861602 var condition = decorator . Condition ! ;
15871603 var isNegated = condition . StartsWith ( "!" , StringComparison . Ordinal ) ;
@@ -1591,11 +1607,14 @@ private static void GenerateServiceRegistrationCalls(
15911607 : $ "configuration.GetValue<bool>(\" { configKey } \" )";
15921608
15931609 sb . AppendLineLf ( ) ;
1594- sb . AppendLineLf ( $ " // Conditional registration for decorator { decorator . ClassSymbol . Name } ") ;
1595- sb . AppendLineLf ( $ " if ({ conditionCheck } )") ;
1596- sb . AppendLineLf ( " {") ;
1610+ sb . AppendLineLf ( $ "{ baseIndent } // Conditional registration for decorator { decorator . ClassSymbol . Name } ") ;
1611+ sb . AppendLineLf ( $ "{ baseIndent } if ({ conditionCheck } )") ;
1612+ sb . AppendLineLf ( $ " { baseIndent } { {") ;
15971613 }
15981614
1615+ // Determine indentation for decorator registration calls (may be nested in conditional)
1616+ var decoratorRegistrationIndent = hasDecoratorCondition ? baseIndent + " " : baseIndent ;
1617+
15991618 // Generate decorator registration for each interface
16001619 foreach ( var asType in decorator . AsTypes )
16011620 {
@@ -1613,34 +1632,34 @@ private static void GenerateServiceRegistrationCalls(
16131632#pragma warning restore S3923
16141633
16151634 sb . AppendLineLf ( ) ;
1616- sb . AppendLineLf ( $ " // Decorator: { decorator . ClassSymbol . Name } ") ;
1635+ sb . AppendLineLf ( $ "{ decoratorRegistrationIndent } // Decorator: { decorator . ClassSymbol . Name } ") ;
16171636
16181637 if ( isGeneric && isInterfaceGeneric )
16191638 {
16201639 // Open generic decorator
16211640 var openGenericServiceType = GetOpenGenericTypeName ( asType ) ;
16221641 var openGenericDecoratorType = GetOpenGenericTypeName ( decorator . ClassSymbol ) ;
16231642
1624- sb . AppendLineLf ( $ " services.{ lifetimeMethod } (typeof({ openGenericServiceType } ), (provider, inner) =>") ;
1625- sb . AppendLineLf ( " {") ;
1626- sb . AppendLineLf ( $ " var decoratorInstance = ActivatorUtilities.CreateInstance(provider, typeof({ openGenericDecoratorType } ), inner);") ;
1627- sb . AppendLineLf ( $ " return decoratorInstance;") ;
1628- sb . AppendLineLf ( " });") ;
1643+ sb . AppendLineLf ( $ "{ decoratorRegistrationIndent } services.{ lifetimeMethod } (typeof({ openGenericServiceType } ), (provider, inner) =>") ;
1644+ sb . AppendLineLf ( $ " { decoratorRegistrationIndent } { {") ;
1645+ sb . AppendLineLf ( $ "{ decoratorRegistrationIndent } var decoratorInstance = ActivatorUtilities.CreateInstance(provider, typeof({ openGenericDecoratorType } ), inner);") ;
1646+ sb . AppendLineLf ( $ "{ decoratorRegistrationIndent } return decoratorInstance;") ;
1647+ sb . AppendLineLf ( $ " { decoratorRegistrationIndent } } });") ;
16291648 }
16301649 else
16311650 {
16321651 // Regular decorator
1633- sb . AppendLineLf ( $ " services.{ lifetimeMethod } <{ serviceType } >((provider, inner) =>") ;
1634- sb . AppendLineLf ( " {") ;
1635- sb . AppendLineLf ( $ " return ActivatorUtilities.CreateInstance<{ decoratorType } >(provider, inner);") ;
1636- sb . AppendLineLf ( " });") ;
1652+ sb . AppendLineLf ( $ "{ decoratorRegistrationIndent } services.{ lifetimeMethod } <{ serviceType } >((provider, inner) =>") ;
1653+ sb . AppendLineLf ( $ " { decoratorRegistrationIndent } { {") ;
1654+ sb . AppendLineLf ( $ "{ decoratorRegistrationIndent } return ActivatorUtilities.CreateInstance<{ decoratorType } >(provider, inner);") ;
1655+ sb . AppendLineLf ( $ " { decoratorRegistrationIndent } } });") ;
16371656 }
16381657 }
16391658
16401659 // Close conditional registration check if needed
1641- if ( hasCondition )
1660+ if ( hasDecoratorCondition )
16421661 {
1643- sb . AppendLineLf ( " }") ;
1662+ sb . AppendLineLf ( $ " { baseIndent } } }") ;
16441663 }
16451664
16461665 // Close runtime filtering check if enabled
0 commit comments