• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ThreeMammals / Ocelot / 21076334793

16 Jan 2026 06:14PM UTC coverage: 93.469% (-0.03%) from 93.503%
21076334793

Pull #2125

github

web-flow
Merge df7350da3 into 9fc4e78d3
Pull Request #2125: Remove the `Newtonsoft.Json` package and migrate to `System.Text.Json`

6569 of 7028 relevant lines covered (93.47%)

4285.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.87
src/Ocelot.Provider.Consul/ConsulFileConfigurationRepository.cs
1
using Microsoft.Extensions.Options;
2
using Ocelot.Cache;
3
using Ocelot.Configuration;
4
using Ocelot.Configuration.File;
5
using Ocelot.Configuration.Repository;
6
using Ocelot.Infrastructure;
7
using Ocelot.Logging;
8
using Ocelot.Provider.Consul.Interfaces;
9
using Ocelot.Responses;
10
using System.Text;
11
using System.Text.Json;
12

13
namespace Ocelot.Provider.Consul;
14

15
public class ConsulFileConfigurationRepository : IFileConfigurationRepository
16
{
17
    private readonly IOcelotCache<FileConfiguration> _cache;
18
    private readonly string _configurationKey;
19
    private readonly IConsulClient _consul;
20
    private readonly IOcelotLogger _logger;
21

22
    public ConsulFileConfigurationRepository(
7✔
23
        IOptions<FileConfiguration> fileConfiguration,
7✔
24
        IOcelotCache<FileConfiguration> cache,
7✔
25
        IConsulClientFactory factory,
7✔
26
        IOcelotLoggerFactory loggerFactory)
7✔
27
    {
28
        _logger = loggerFactory.CreateLogger<ConsulFileConfigurationRepository>();
7✔
29
        _cache = cache;
7✔
30

31
        var provider = fileConfiguration.Value.GlobalConfiguration.ServiceDiscoveryProvider;
7✔
32
        _configurationKey = string.IsNullOrWhiteSpace(provider.ConfigurationKey)
7✔
33
            ? nameof(InternalConfiguration)
7✔
34
            : provider.ConfigurationKey;
7✔
35

36
        var config = new ConsulRegistryConfiguration(provider.Scheme, provider.Host,
7✔
37
            provider.Port, _configurationKey, provider.Token);
7✔
38
        _consul = factory.Get(config);
7✔
39
    }
7✔
40

41
    public async Task<Response<FileConfiguration>> Get()
42
    {
43
        var config = _cache.Get(_configurationKey, _configurationKey);
5✔
44
        if (config != null)
5✔
45
        {
46
            return new OkResponse<FileConfiguration>(config);
1✔
47
        }
48

49
        var queryResult = await _consul.KV.Get(_configurationKey);
4✔
50
        if (queryResult.Response == null)
4✔
51
        {
52
            return new OkResponse<FileConfiguration>(null);
1✔
53
        }
54

55
        var bytes = queryResult.Response.Value;
3✔
56
        var json = Encoding.UTF8.GetString(bytes);
3✔
57
        var consulConfig = JsonSerializer.Deserialize<FileConfiguration>(json, OcelotSerializerOptions.Web);
3✔
58

59
        return new OkResponse<FileConfiguration>(consulConfig);
3✔
60
    }
5✔
61

62
    public async Task<Response> Set(FileConfiguration ocelotConfiguration)
63
    {
64
        var json = JsonSerializer.Serialize(ocelotConfiguration, OcelotSerializerOptions.WebWriteIndented);
1✔
65
        var bytes = Encoding.UTF8.GetBytes(json);
1✔
66
        var kvPair = new KVPair(_configurationKey)
1✔
67
        {
1✔
68
            Value = bytes,
1✔
69
        };
1✔
70

71
        var result = await _consul.KV.Put(kvPair);
1✔
72
        if (result.Response)
1✔
73
        {
74
            _cache.AddOrUpdate(_configurationKey, ocelotConfiguration, _configurationKey, TimeSpan.FromSeconds(3));
1✔
75
            return new OkResponse();
1✔
76
        }
77

78
        return new ErrorResponse(new UnableToSetConfigInConsulError(
×
79
            $"Unable to set {nameof(FileConfiguration)} in {nameof(Consul)}, response status code from {nameof(Consul)} was {result.StatusCode}"));
×
80
    }
1✔
81
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc