@@ -53,16 +53,24 @@ public ConnectionDescription InitializeConnection(IConnection connection, Cancel
53
53
54
54
AuthenticationHelper . Authenticate ( connection , description , cancellationToken ) ;
55
55
56
- try
56
+ var connectionIdServerValue = isMasterResult . ConnectionIdServerValue ;
57
+ if ( connectionIdServerValue . HasValue )
57
58
{
58
- var getLastErrorProtocol = CreateGetLastErrorProtocol ( ) ;
59
- var getLastErrorResult = getLastErrorProtocol . Execute ( connection , cancellationToken ) ;
60
-
61
- description = UpdateConnectionIdWithServerValue ( description , getLastErrorResult ) ;
59
+ description = UpdateConnectionIdWithServerValue ( description , connectionIdServerValue . Value ) ;
62
60
}
63
- catch
61
+ else
64
62
{
65
- // if we couldn't get the server's connection id, so be it.
63
+ try
64
+ {
65
+ var getLastErrorProtocol = CreateGetLastErrorProtocol ( ) ;
66
+ var getLastErrorResult = getLastErrorProtocol . Execute ( connection , cancellationToken ) ;
67
+
68
+ description = UpdateConnectionIdWithServerValue ( description , getLastErrorResult ) ;
69
+ }
70
+ catch
71
+ {
72
+ // if we couldn't get the server's connection id, so be it.
73
+ }
66
74
}
67
75
68
76
return description ;
@@ -82,16 +90,26 @@ public async Task<ConnectionDescription> InitializeConnectionAsync(IConnection c
82
90
83
91
await AuthenticationHelper . AuthenticateAsync ( connection , description , cancellationToken ) . ConfigureAwait ( false ) ;
84
92
85
- try
93
+ var connectionIdServerValue = isMasterResult . ConnectionIdServerValue ;
94
+ if ( connectionIdServerValue . HasValue )
86
95
{
87
- var getLastErrorProtocol = CreateGetLastErrorProtocol ( ) ;
88
- var getLastErrorResult = await getLastErrorProtocol . ExecuteAsync ( connection , cancellationToken ) . ConfigureAwait ( false ) ;
89
-
90
- description = UpdateConnectionIdWithServerValue ( description , getLastErrorResult ) ;
96
+ description = UpdateConnectionIdWithServerValue ( description , connectionIdServerValue . Value ) ;
91
97
}
92
- catch
98
+ else
93
99
{
94
- // if we couldn't get the server's connection id, so be it.
100
+ try
101
+ {
102
+ var getLastErrorProtocol = CreateGetLastErrorProtocol ( ) ;
103
+ var getLastErrorResult = await getLastErrorProtocol
104
+ . ExecuteAsync ( connection , cancellationToken )
105
+ . ConfigureAwait ( false ) ;
106
+
107
+ description = UpdateConnectionIdWithServerValue ( description , getLastErrorResult ) ;
108
+ }
109
+ catch
110
+ {
111
+ // if we couldn't get the server's connection id, so be it.
112
+ }
95
113
}
96
114
97
115
return description ;
@@ -132,14 +150,20 @@ private BsonDocument CreateInitialIsMasterCommand(IReadOnlyList<IAuthenticator>
132
150
133
151
private ConnectionDescription UpdateConnectionIdWithServerValue ( ConnectionDescription description , BsonDocument getLastErrorResult )
134
152
{
135
- BsonValue connectionIdBsonValue ;
136
- if ( getLastErrorResult . TryGetValue ( "connectionId" , out connectionIdBsonValue ) )
153
+ if ( getLastErrorResult . TryGetValue ( "connectionId" , out var connectionIdBsonValue ) )
137
154
{
138
- var connectionId = description . ConnectionId . WithServerValue ( connectionIdBsonValue . ToInt32 ( ) ) ;
139
- description = description . WithConnectionId ( connectionId ) ;
155
+ description = UpdateConnectionIdWithServerValue ( description , connectionIdBsonValue . ToInt32 ( ) ) ;
140
156
}
141
157
142
158
return description ;
143
159
}
160
+
161
+ private ConnectionDescription UpdateConnectionIdWithServerValue ( ConnectionDescription description , int serverValue )
162
+ {
163
+ var connectionId = description . ConnectionId . WithServerValue ( serverValue ) ;
164
+ description = description . WithConnectionId ( connectionId ) ;
165
+
166
+ return description ;
167
+ }
144
168
}
145
169
}
0 commit comments