@@ -5,6 +5,7 @@ namespace AgDatabaseMove.SmoFacade
55 using System . Data . SqlClient ;
66 using System . Linq ;
77 using System . Net ;
8+ using System . Net . Sockets ;
89 using System . Threading . Tasks ;
910
1011
@@ -133,21 +134,20 @@ private static string AgListenerName(string dataSource)
133134 private static Server AgInstanceNameToServer ( ref SqlConnectionStringBuilder connBuilder , string agInstanceName ,
134135 string credentialName )
135136 {
136- try
137- {
137+ try {
138138 connBuilder . DataSource = ResolveDnsHostNameForInstance ( agInstanceName , connBuilder . DataSource ) ;
139139 return new Server ( connBuilder . ToString ( ) , credentialName ) ;
140140 }
141- catch ( Exception e )
142- {
141+ catch ( Exception e ) {
143142 throw new ArgumentException ( $ "agInstanceName param { agInstanceName } cannot be resolved by DNS", e ) ;
144143 }
145144 }
146145
147146 /// <summary>
148147 /// Resolves 'agReplicaInstanceName' to a FQDN
149148 /// However on Unix OS, when 'val' in 'Dns.GetHostEntry(val)' is not a FQDN, it fails intermittently
150- /// Therefore, if dns lookup on just the instance name fails, we retry after appending the domain fragments from the listener to the instance name
149+ /// Therefore, if dns lookup on just the instance name fails, we retry after appending the domain fragments from the
150+ /// listener to the instance name
151151 /// </summary>
152152 /// <param name="agReplicaInstanceName"> The name for an instance within the AG (for which we are trying to get the FQDN)</param>
153153 /// <param name="agListenerDomain"> The complete domain for the AG listener</param>
@@ -159,12 +159,10 @@ private static string ResolveDnsHostNameForInstance(string agReplicaInstanceName
159159 var ( instanceName , instancePortOrNamedInstance ) = SplitDomainAndPort ( agReplicaInstanceName ) ;
160160 var preferredPortOrNamedInstance = GetPreferredPort ( instancePortOrNamedInstance , listenerPortOrNamedInstance ) ;
161161
162- try
163- {
162+ try {
164163 return $ "{ Dns . GetHostEntry ( instanceName ) . HostName } { preferredPortOrNamedInstance } ";
165164 }
166- catch ( System . Net . Sockets . SocketException )
167- {
165+ catch ( SocketException ) {
168166 // Re-try by appending the domain fragments from listener to the instance name
169167 // However, we don't need the listener's "host name" (first fragment), so we need to strip that off
170168 // eg: if listener is "abc.def.ghi" we want to append only ".def.ghi" to the instance name
@@ -181,12 +179,10 @@ private static string ResolveDnsHostNameForInstance(string agReplicaInstanceName
181179 // If both are same type, then prioritize instance over listener (in almost all cases they should be identical)
182180 internal static string GetPreferredPort ( string instancePortOrNamedInstance , string listenerPortOrNamedInstance )
183181 {
184- if ( string . IsNullOrEmpty ( instancePortOrNamedInstance ) || string . IsNullOrEmpty ( listenerPortOrNamedInstance ) )
185- {
186- return ( instancePortOrNamedInstance ?? listenerPortOrNamedInstance ) ;
187- }
188- return instancePortOrNamedInstance . StartsWith ( "\\ " ) && listenerPortOrNamedInstance . StartsWith ( "," )
189- ? listenerPortOrNamedInstance
182+ if ( string . IsNullOrEmpty ( instancePortOrNamedInstance ) || string . IsNullOrEmpty ( listenerPortOrNamedInstance ) )
183+ return instancePortOrNamedInstance ?? listenerPortOrNamedInstance ;
184+ return instancePortOrNamedInstance . StartsWith ( "\\ " ) && listenerPortOrNamedInstance . StartsWith ( "," )
185+ ? listenerPortOrNamedInstance
190186 : instancePortOrNamedInstance ;
191187 }
192188
@@ -196,17 +192,13 @@ internal static (string domain, string port) SplitDomainAndPort(string domainAnd
196192 var domain = domainAndPort ;
197193 var splitValue = domainAndPort . Contains ( ',' ) ? "," : domainAndPort . Contains ( '\\ ' ) ? "\\ " : null ;
198194
199- if ( splitValue == null )
200- {
201- return ( domain , null ) ;
202- }
195+ if ( splitValue == null ) return ( domain , null ) ;
203196
204197 var fragments = domainAndPort . Split ( splitValue . ToCharArray ( ) , 2 ) ;
205198 domain = fragments [ 0 ] ;
206199 var port = $ "{ splitValue } { fragments [ 1 ] } ";
207200
208201 return ( domain , port ) ;
209202 }
210-
211203 }
212204}
0 commit comments