Skip to content

Commit 4fa073c

Browse files
authored
Update to C# 8. (#87)
1 parent 9e24a71 commit 4fa073c

40 files changed

Lines changed: 219 additions & 195 deletions

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ env:
1111
matrix:
1212
include:
1313
- os: linux
14-
dist: xenial
14+
dist: bionic
1515
sudo: required
1616
dotnet: 3.0
1717
- os: osx
1818
osx_image: xcode11.2
19-
dotnet: 3.0.100
19+
dotnet: 3.0.101
2020
before_install:
2121
- ulimit -n 4096
2222
script:

Directory.Build.props

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project>
2+
<PropertyGroup>
3+
<Authors>Winton</Authors>
4+
<Company>Winton</Company>
5+
<Copyright>Copyright 2019 Winton</Copyright>
6+
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)Rules.ruleset</CodeAnalysisRuleSet>
7+
<LangVersion>8.0</LangVersion>
8+
<Nullable>enable</Nullable>
9+
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" />
14+
</ItemGroup>
15+
</Project>

Rules.ruleset

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RuleSet Name="Rules" Description=" " ToolsVersion="16.0">
3+
<Rules AnalyzerId="Microsoft.CodeQuality.Analyzers" RuleNamespace="Microsoft.CodeQuality.Analyzers">
4+
<Rule Id="CA2007" Action="None" />
5+
</Rules>
6+
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
7+
<Rule Id="SA1101" Action="None" />
8+
<Rule Id="SA1309" Action="None" />
9+
<Rule Id="SA1413" Action="None" />
10+
</Rules>
11+
</RuleSet>

Winton.Extensions.Configuration.Consul.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,21 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1010
appveyor.yml = appveyor.yml
1111
build.sh = build.sh
1212
CONTRIBUTING.md = CONTRIBUTING.md
13+
Directory.Build.props = Directory.Build.props
1314
GitVersion.yml = GitVersion.yml
1415
LICENSE = LICENSE
1516
README.md = README.md
17+
Rules.ruleset = Rules.ruleset
1618
stylecop.json = stylecop.json
1719
EndProjectSection
1820
EndProject
1921
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{03739F78-2460-4F1E-92F2-807CBD8C8AB5}"
2022
EndProject
2123
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{972A51CA-2686-4B9B-8BBB-503B2A1BA630}"
24+
ProjectSection(SolutionItems) = preProject
25+
test\Directory.Build.props = test\Directory.Build.props
26+
test\Rules.ruleset = test\Rules.ruleset
27+
EndProjectSection
2228
EndProject
2329
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Winton.Extensions.Configuration.Consul", "src\Winton.Extensions.Configuration.Consul\Winton.Extensions.Configuration.Consul.csproj", "{B5C08CE5-B60B-485F-B78F-A119B8AA8508}"
2430
EndProject

Winton.Extensions.Configuration.Consul.sln.DotSettings

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,9 @@
414414
&lt;/TypePattern&gt;&#xD;
415415
&lt;/Patterns&gt;</s:String>
416416

417-
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForBuiltInTypes/@EntryValue">UseVarWhenEvident</s:String>
418-
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForOtherTypes/@EntryValue">UseVarWhenEvident</s:String>
419-
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForSimpleTypes/@EntryValue">UseVarWhenEvident</s:String>
417+
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForBuiltInTypes/@EntryValue">UseVar</s:String>
418+
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForOtherTypes/@EntryValue">UseVar</s:String>
419+
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForSimpleTypes/@EntryValue">UseVar</s:String>
420420
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="AaBb" /&gt;</s:String>
421421
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
422422
<s:String x:Key="/Default/CodeStyle/Naming/JavaScriptNaming/UserRules/=JS_005FBLOCK_005FSCOPE_005FCONSTANT/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>

src/Winton.Extensions.Configuration.Consul/ConfigurationBuilderExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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

44
using System;
5-
using System.Threading;
65
using Microsoft.Extensions.Configuration;
76

87
namespace Winton.Extensions.Configuration.Consul
@@ -45,4 +44,4 @@ public static IConfigurationBuilder AddConsul(
4544
return builder.Add(consulConfigSource);
4645
}
4746
}
48-
}
47+
}

src/Winton.Extensions.Configuration.Consul/ConsulClientFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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

44
using Consul;
55

src/Winton.Extensions.Configuration.Consul/ConsulConfigurationProvider.cs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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

44
using System;
55
using 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);

src/Winton.Extensions.Configuration.Consul/ConsulConfigurationSource.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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

44
using System;
55
using System.Net.Http;
@@ -11,6 +11,8 @@ namespace Winton.Extensions.Configuration.Consul
1111
{
1212
internal sealed class ConsulConfigurationSource : IConsulConfigurationSource
1313
{
14+
private string? _keyToRemove;
15+
1416
public ConsulConfigurationSource(string key)
1517
{
1618
if (string.IsNullOrWhiteSpace(key))
@@ -19,32 +21,35 @@ public ConsulConfigurationSource(string key)
1921
}
2022

2123
Key = key;
22-
KeyToRemove = Key;
2324
Parser = new JsonConfigurationParser();
2425
}
2526

26-
public Action<ConsulClientConfiguration> ConsulConfigurationOptions { get; set; }
27+
public Action<ConsulClientConfiguration>? ConsulConfigurationOptions { get; set; }
2728

28-
public Action<HttpClientHandler> ConsulHttpClientHandlerOptions { get; set; }
29+
public Action<HttpClientHandler>? ConsulHttpClientHandlerOptions { get; set; }
2930

30-
public Action<HttpClient> ConsulHttpClientOptions { get; set; }
31+
public Action<HttpClient>? ConsulHttpClientOptions { get; set; }
3132

3233
public string Key { get; }
3334

34-
public string KeyToRemove { get; set; }
35+
public string KeyToRemove
36+
{
37+
get => _keyToRemove ?? Key;
38+
set => _keyToRemove = value;
39+
}
3540

36-
public Action<ConsulLoadExceptionContext> OnLoadException { get; set; }
41+
public Action<ConsulLoadExceptionContext>? OnLoadException { get; set; }
3742

38-
public Func<ConsulWatchExceptionContext, TimeSpan> OnWatchException { get; set; }
43+
public Func<ConsulWatchExceptionContext, TimeSpan>? OnWatchException { get; set; }
3944

4045
public bool Optional { get; set; } = false;
4146

4247
public IConfigurationParser Parser { get; set; }
4348

44-
public bool ReloadOnChange { get; set; } = false;
45-
4649
public TimeSpan PollWaitTime { get; set; } = TimeSpan.FromMinutes(5);
4750

51+
public bool ReloadOnChange { get; set; } = false;
52+
4853
public IConfigurationProvider Build(IConfigurationBuilder builder)
4954
{
5055
var consulClientFactory = new ConsulClientFactory(this);

src/Winton.Extensions.Configuration.Consul/ConsulLoadExceptionContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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

44
using System;
55

0 commit comments

Comments
 (0)