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

ThreeMammals / Ocelot / 25553538207

08 May 2026 11:39AM UTC coverage: 94.48% (+0.9%) from 93.559%
25553538207

Pull #2388

github

web-flow
Merge 6158d8eee into fe672ec02
Pull Request #2388: #2378 Deprecate `Ocelot.Provider.Consul` project

6401 of 6775 relevant lines covered (94.48%)

2478.96 hits per line

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

96.55
src/Ocelot/Configuration/Repository/DiskFileConfigurationRepository.cs
1
using Microsoft.AspNetCore.Hosting;
2
using Newtonsoft.Json;
3
using Ocelot.Configuration.ChangeTracking;
4
using Ocelot.Configuration.File;
5
using Ocelot.DependencyInjection;
6
using FileIO = System.IO.File;
7

8
namespace Ocelot.Configuration.Repository;
9
public class DiskFileConfigurationRepository : IFileConfigurationRepository, IDisposable
10
{
11
    private FileInfo _ocelotFile;
12
    private FileInfo _environmentFile;
13
    private readonly IWebHostEnvironment _hostingEnvironment;
14
    private readonly IOcelotConfigurationChangeTokenSource _changeTokenSource;
15
    private readonly SemaphoreSlim _semaphore = new(1, 1); // 1 concurrent access
9✔
16

17
    public DiskFileConfigurationRepository(IWebHostEnvironment hostingEnvironment, IOcelotConfigurationChangeTokenSource changeTokenSource)
1✔
18
    {
19
        _hostingEnvironment = hostingEnvironment;
1✔
20
        _changeTokenSource = changeTokenSource;
1✔
21
        Initialize(AppContext.BaseDirectory);
1✔
22
    }
1✔
23

24
    public DiskFileConfigurationRepository(IWebHostEnvironment hostingEnvironment, IOcelotConfigurationChangeTokenSource changeTokenSource, string folder)
8✔
25
    {
26
        _hostingEnvironment = hostingEnvironment;
8✔
27
        _changeTokenSource = changeTokenSource;
8✔
28
        Initialize(folder);
8✔
29
    }
8✔
30

31
    private void Initialize(string folder)
32
    {
33
        folder ??= AppContext.BaseDirectory;
9✔
34
        _ocelotFile = new FileInfo(Path.Combine(folder, ConfigurationBuilderExtensions.PrimaryConfigFile));
9✔
35

36
        var envFile = !string.IsNullOrEmpty(_hostingEnvironment.EnvironmentName)
9✔
37
            ? string.Format(ConfigurationBuilderExtensions.EnvironmentConfigFile, _hostingEnvironment.EnvironmentName)
9✔
38
            : ConfigurationBuilderExtensions.PrimaryConfigFile;
9✔
39

40
        _environmentFile = new FileInfo(Path.Combine(folder, envFile));
9✔
41
    }
9✔
42

43
    public FileConfiguration Get()
44
    {
45
        string json;
46
        _semaphore.Wait();
1✔
47
        try
48
        {
49
            json = FileIO.ReadAllText(_environmentFile.FullName);
1✔
50
        }
1✔
51
        finally
52
        {
53
            _semaphore.Release();
1✔
54
        }
1✔
55

56
        return JsonConvert.DeserializeObject<FileConfiguration>(json);
1✔
57
    }
58

59
    public async Task<FileConfiguration> GetAsync(CancellationToken cancellationToken = default)
60
    {
61
        string json;
62
        await _semaphore.WaitAsync(cancellationToken);
6✔
63
        try
64
        {
65
            json = await FileIO.ReadAllTextAsync(_environmentFile.FullName, cancellationToken);
6✔
66
        }
6✔
67
        finally
68
        {
69
            _semaphore.Release();
6✔
70
        }
71

72
        return JsonConvert.DeserializeObject<FileConfiguration>(json);
6✔
73
    }
6✔
74

75
    public void Set(FileConfiguration configuration)
76
    {
77
        var json = JsonConvert.SerializeObject(configuration, Formatting.Indented);
1✔
78
        _semaphore.Wait();
1✔
79
        try
80
        {
81
            if (_environmentFile.Exists)
1✔
82
                _environmentFile.Delete();
×
83

84
            FileIO.WriteAllText(_environmentFile.FullName, json);
1✔
85

86
            if (_ocelotFile.Exists)
1✔
87
                _ocelotFile.Delete();
×
88

89
            FileIO.WriteAllText(_ocelotFile.FullName, json);
1✔
90
        }
1✔
91
        finally
92
        {
93
            _semaphore.Release();
1✔
94
        }
1✔
95

96
        _changeTokenSource.Activate();
1✔
97
    }
1✔
98

99
    public async Task SetAsync(FileConfiguration configuration, CancellationToken cancellationToken = default)
100
    {
101
        var json = JsonConvert.SerializeObject(configuration, Formatting.Indented);
3✔
102
        await _semaphore.WaitAsync(cancellationToken);
3✔
103
        try
104
        {
105
            if (_environmentFile.Exists)
3✔
106
                _environmentFile.Delete();
1✔
107

108
            await FileIO.WriteAllTextAsync(_environmentFile.FullName, json, cancellationToken);
3✔
109

110
            if (_ocelotFile.Exists)
3✔
111
                _ocelotFile.Delete();
1✔
112

113
            await FileIO.WriteAllTextAsync(_ocelotFile.FullName, json, cancellationToken);
3✔
114
        }
3✔
115
        finally
116
        {
117
            _semaphore.Release();
3✔
118
        }
119

120
        _changeTokenSource.Activate();
3✔
121
    }
3✔
122

123
    public void Dispose()
124
    {
125
        _semaphore.Dispose();
1✔
126
        GC.SuppressFinalize(this);
1✔
127
    }
1✔
128
}
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