@@ -538,18 +538,26 @@ private void ValidateAndSetReadFrom()
538538 switch ( strategy )
539539 {
540540 case ReadFromStrategy . AzAffinity :
541- if ( string . IsNullOrWhiteSpace ( tempAz ) )
541+ if ( tempAz == null )
542542 {
543543 throw new ArgumentException ( "Availability zone should be set when using AzAffinity strategy" ) ;
544544 }
545+ if ( string . IsNullOrWhiteSpace ( tempAz ) )
546+ {
547+ throw new ArgumentException ( "Availability zone cannot be empty or whitespace when using AzAffinity strategy" ) ;
548+ }
545549 readFrom = new ReadFrom ( strategy , tempAz ) ;
546550 break ;
547551
548552 case ReadFromStrategy . AzAffinityReplicasAndPrimary :
549- if ( string . IsNullOrWhiteSpace ( tempAz ) )
553+ if ( tempAz == null )
550554 {
551555 throw new ArgumentException ( "Availability zone should be set when using AzAffinityReplicasAndPrimary strategy" ) ;
552556 }
557+ if ( string . IsNullOrWhiteSpace ( tempAz ) )
558+ {
559+ throw new ArgumentException ( "Availability zone cannot be empty or whitespace when using AzAffinityReplicasAndPrimary strategy" ) ;
560+ }
553561 readFrom = new ReadFrom ( strategy , tempAz ) ;
554562 break ;
555563
@@ -570,7 +578,7 @@ private void ValidateAndSetReadFrom()
570578 break ;
571579
572580 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 ") ;
574582 }
575583 }
576584 }
@@ -582,14 +590,14 @@ private static ReadFromStrategy ParseReadFromStrategy(string key, string value)
582590 throw new ArgumentException ( $ "Keyword '{ key } ' requires a ReadFrom strategy value; the value cannot be empty", key ) ;
583591 }
584592
585- return value . ToLowerInvariant ( ) switch
593+ try
586594 {
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+ }
593601 }
594602
595603 private static void ValidateReadFromConfiguration ( ReadFrom readFromConfig )
@@ -644,30 +652,13 @@ private static void ValidateReadFromConfiguration(ReadFrom readFromConfig)
644652 /// <param name="readFromConfig">The ReadFrom configuration to format.</param>
645653 private static void FormatReadFrom ( StringBuilder sb , ReadFrom readFromConfig )
646654 {
647- Append ( sb , OptionKeys . ReadFrom , FormatReadFromStrategy ( readFromConfig . Strategy ) ) ;
655+ Append ( sb , OptionKeys . ReadFrom , readFromConfig . Strategy . ToString ( ) ) ;
648656 if ( ! string . IsNullOrWhiteSpace ( readFromConfig . Az ) )
649657 {
650658 Append ( sb , OptionKeys . Az , readFromConfig . Az ) ;
651659 }
652660 }
653661
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-
671662 /// <summary>
672663 /// Specify the connection protocol type.
673664 /// </summary>
0 commit comments