@@ -538,18 +538,26 @@ private void ValidateAndSetReadFrom()
538
538
switch ( strategy )
539
539
{
540
540
case ReadFromStrategy . AzAffinity :
541
- if ( string . IsNullOrWhiteSpace ( tempAz ) )
541
+ if ( tempAz == null )
542
542
{
543
543
throw new ArgumentException ( "Availability zone should be set when using AzAffinity strategy" ) ;
544
544
}
545
+ if ( string . IsNullOrWhiteSpace ( tempAz ) )
546
+ {
547
+ throw new ArgumentException ( "Availability zone cannot be empty or whitespace when using AzAffinity strategy" ) ;
548
+ }
545
549
readFrom = new ReadFrom ( strategy , tempAz ) ;
546
550
break ;
547
551
548
552
case ReadFromStrategy . AzAffinityReplicasAndPrimary :
549
- if ( string . IsNullOrWhiteSpace ( tempAz ) )
553
+ if ( tempAz == null )
550
554
{
551
555
throw new ArgumentException ( "Availability zone should be set when using AzAffinityReplicasAndPrimary strategy" ) ;
552
556
}
557
+ if ( string . IsNullOrWhiteSpace ( tempAz ) )
558
+ {
559
+ throw new ArgumentException ( "Availability zone cannot be empty or whitespace when using AzAffinityReplicasAndPrimary strategy" ) ;
560
+ }
553
561
readFrom = new ReadFrom ( strategy , tempAz ) ;
554
562
break ;
555
563
@@ -570,7 +578,7 @@ private void ValidateAndSetReadFrom()
570
578
break ;
571
579
572
580
default :
573
- throw new ArgumentOutOfRangeException ( nameof ( tempReadFromStrategy ) , $ "ReadFrom strategy '{ strategy } ' is not supported") ;
581
+ throw new ArgumentException ( $ "ReadFrom strategy '{ strategy } ' is not supported. Valid strategies are: Primary, PreferReplica, AzAffinity, AzAffinityReplicasAndPrimary ") ;
574
582
}
575
583
}
576
584
}
@@ -582,14 +590,14 @@ private static ReadFromStrategy ParseReadFromStrategy(string key, string value)
582
590
throw new ArgumentException ( $ "Keyword '{ key } ' requires a ReadFrom strategy value; the value cannot be empty", key ) ;
583
591
}
584
592
585
- return value . ToLowerInvariant ( ) switch
593
+ try
586
594
{
587
- "primary" => ReadFromStrategy . Primary ,
588
- "preferreplica" => ReadFromStrategy . PreferReplica ,
589
- "azaffinity" => ReadFromStrategy . AzAffinity ,
590
- "azaffinityreplicasandprimary" => ReadFromStrategy . AzAffinityReplicasAndPrimary ,
591
- _ => throw new ArgumentException ( $ "ReadFrom strategy '{ value } ' is not supported. Supported values are: Primary, PreferReplica, AzAffinity, AzAffinityReplicasAndPrimary", key )
592
- } ;
595
+ return Enum . Parse < ReadFromStrategy > ( value , ignoreCase : true ) ;
596
+ }
597
+ catch ( ArgumentException )
598
+ {
599
+ throw new ArgumentException ( $ "ReadFrom strategy '{ value } ' is not supported. Valid strategies are: Primary, PreferReplica, AzAffinity, AzAffinityReplicasAndPrimary", key ) ;
600
+ }
593
601
}
594
602
595
603
private static void ValidateReadFromConfiguration ( ReadFrom readFromConfig )
@@ -644,30 +652,13 @@ private static void ValidateReadFromConfiguration(ReadFrom readFromConfig)
644
652
/// <param name="readFromConfig">The ReadFrom configuration to format.</param>
645
653
private static void FormatReadFrom ( StringBuilder sb , ReadFrom readFromConfig )
646
654
{
647
- Append ( sb , OptionKeys . ReadFrom , FormatReadFromStrategy ( readFromConfig . Strategy ) ) ;
655
+ Append ( sb , OptionKeys . ReadFrom , readFromConfig . Strategy . ToString ( ) ) ;
648
656
if ( ! string . IsNullOrWhiteSpace ( readFromConfig . Az ) )
649
657
{
650
658
Append ( sb , OptionKeys . Az , readFromConfig . Az ) ;
651
659
}
652
660
}
653
661
654
- /// <summary>
655
- /// Converts a ReadFromStrategy enum value to its string representation.
656
- /// </summary>
657
- /// <param name="strategy">The ReadFromStrategy to format.</param>
658
- /// <returns>The string representation of the strategy.</returns>
659
- private static string FormatReadFromStrategy ( ReadFromStrategy strategy )
660
- {
661
- return strategy switch
662
- {
663
- ReadFromStrategy . Primary => "Primary" ,
664
- ReadFromStrategy . PreferReplica => "PreferReplica" ,
665
- ReadFromStrategy . AzAffinity => "AzAffinity" ,
666
- ReadFromStrategy . AzAffinityReplicasAndPrimary => "AzAffinityReplicasAndPrimary" ,
667
- _ => strategy . ToString ( ) ,
668
- } ;
669
- }
670
-
671
662
/// <summary>
672
663
/// Specify the connection protocol type.
673
664
/// </summary>
0 commit comments