11// Copyright (c) Winton. All rights reserved.
2- // Licensed under the Apache License, Version 2.0. See LICENCE in the project root for license information.
2+ // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33
44using System ;
55using System . Net ;
@@ -13,7 +13,7 @@ namespace Winton.Extensions.Configuration.Consul
1313{
1414 /// <summary>
1515 /// Each instance loads configuration for the key in Consul that is specified in
16- /// the contained <see cref="IConsulConfigurationSource"/>.
16+ /// the contained <see cref="IConsulConfigurationSource" />.
1717 /// It has the ability to automatically reload the config if it changes in Consul.
1818 /// </summary>
1919 /// <remarks>
@@ -26,7 +26,7 @@ internal sealed class ConsulConfigurationProvider : ConfigurationProvider, IDisp
2626 private readonly IConsulClientFactory _consulClientFactory ;
2727 private readonly IConsulConfigurationSource _source ;
2828 private ulong _lastIndex ;
29- private Task _pollTask ;
29+ private Task ? _pollTask ;
3030
3131 public ConsulConfigurationProvider (
3232 IConsulConfigurationSource source ,
@@ -70,7 +70,7 @@ private async Task DoLoad()
7070 {
7171 try
7272 {
73- QueryResult < KVPair [ ] > result = await GetKvPairs ( false ) . ConfigureAwait ( false ) ;
73+ var result = await GetKvPairs ( false ) . ConfigureAwait ( false ) ;
7474
7575 if ( result . HasValue ( ) )
7676 {
@@ -96,30 +96,27 @@ private async Task DoLoad()
9696
9797 private async Task < QueryResult < KVPair [ ] > > GetKvPairs ( bool waitForChange )
9898 {
99- using ( IConsulClient consulClient = _consulClientFactory . Create ( ) )
99+ using var consulClient = _consulClientFactory . Create ( ) ;
100+ var queryOptions = new QueryOptions
100101 {
101- var queryOptions = new QueryOptions
102- {
103- WaitTime = _source . PollWaitTime ,
104- WaitIndex = waitForChange ? _lastIndex : 0
105- } ;
102+ WaitTime = _source . PollWaitTime ,
103+ WaitIndex = waitForChange ? _lastIndex : 0
104+ } ;
106105
107- QueryResult < KVPair [ ] > result =
108- await consulClient
109- . KV
110- . List ( _source . Key , queryOptions , _cancellationTokenSource . Token )
111- . ConfigureAwait ( false ) ;
106+ var result =
107+ await consulClient
108+ . KV
109+ . List ( _source . Key , queryOptions , _cancellationTokenSource . Token )
110+ . ConfigureAwait ( false ) ;
112111
113- switch ( result . StatusCode )
114- {
115- case HttpStatusCode . OK :
116- case HttpStatusCode . NotFound :
117- return result ;
118- default :
119- throw new Exception (
120- $ "Error loading configuration from consul. Status code: { result . StatusCode } .") ;
121- }
122- }
112+ return result . StatusCode switch
113+ {
114+ HttpStatusCode . OK => result ,
115+ HttpStatusCode . NotFound => result ,
116+ _ =>
117+ throw
118+ new Exception ( $ "Error loading configuration from consul. Status code: { result . StatusCode } .")
119+ } ;
123120 }
124121
125122 private async Task PollingLoop ( )
@@ -129,7 +126,7 @@ private async Task PollingLoop()
129126 {
130127 try
131128 {
132- QueryResult < KVPair [ ] > result = await GetKvPairs ( true ) . ConfigureAwait ( false ) ;
129+ var result = await GetKvPairs ( true ) . ConfigureAwait ( false ) ;
133130
134131 if ( result . HasValue ( ) && result . LastIndex > _lastIndex )
135132 {
@@ -142,7 +139,7 @@ private async Task PollingLoop()
142139 }
143140 catch ( Exception exception )
144141 {
145- TimeSpan wait =
142+ var wait =
146143 _source . OnWatchException ? . Invoke (
147144 new ConsulWatchExceptionContext ( exception , ++ consecutiveFailureCount , _source ) ) ??
148145 TimeSpan . FromSeconds ( 5 ) ;
0 commit comments