Skip to content

Commit

Permalink
fix null ref exception and init order
Browse files Browse the repository at this point in the history
  • Loading branch information
muphblu committed Nov 13, 2023
1 parent 7492da4 commit f2d063b
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ATI.Services.Consul/ConsulServiceAddressCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ public ConsulServiceAddressCache(string serviceName, string environment, TimeSpa
_serviceName = serviceName;
_environment = environment;
_consulAdapter = new ConsulAdapter();
_updateCacheTask = _consulAdapter.GetPassingServiceInstancesAsync(_serviceName, _environment);
_cachedServices = _updateCacheTask.GetAwaiter().GetResult();
_updateCacheTimer = new Timer(_ => ReloadCache(), null, ttl, ttl);
_cachedServices = _consulAdapter.GetPassingServiceInstancesAsync(serviceName, environment).GetAwaiter().GetResult();
}

/// <summary>
Expand All @@ -38,7 +39,7 @@ public ConsulServiceAddressCache(string serviceName, string environment, TimeSpa
/// </summary>
private void ReloadCache()
{
if(_updateCacheTask.IsCompleted)
if(_updateCacheTask == null || _updateCacheTask.IsCompleted)
_updateCacheTask = _consulAdapter.GetPassingServiceInstancesAsync(_serviceName, _environment);

_cachedServices = _updateCacheTask.GetAwaiter().GetResult();
Expand Down

0 comments on commit f2d063b

Please sign in to comment.