From ef754e101d3334b448a1f257305c7fc45171aa08 Mon Sep 17 00:00:00 2001 From: raman-m Date: Mon, 2 Oct 2023 11:08:44 +0300 Subject: [PATCH] Throw NullRef exception if no client --- src/Ocelot.Provider.Eureka/Eureka.cs | 6 +++--- src/Ocelot.Provider.Eureka/EurekaProviderFactory.cs | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Ocelot.Provider.Eureka/Eureka.cs b/src/Ocelot.Provider.Eureka/Eureka.cs index 5fd6b4e73..9b1372ddc 100644 --- a/src/Ocelot.Provider.Eureka/Eureka.cs +++ b/src/Ocelot.Provider.Eureka/Eureka.cs @@ -5,13 +5,13 @@ namespace Ocelot.Provider.Eureka { public class Eureka : IServiceDiscoveryProvider { - private readonly IDiscoveryClient _client; private readonly string _serviceName; + private readonly IDiscoveryClient _client; public Eureka(string serviceName, IDiscoveryClient client) { - _client = client; - _serviceName = serviceName; + _serviceName = serviceName ?? throw new ArgumentNullException(nameof(serviceName)); + _client = client ?? throw new ArgumentNullException(nameof(client)); } public Task> GetAsync() diff --git a/src/Ocelot.Provider.Eureka/EurekaProviderFactory.cs b/src/Ocelot.Provider.Eureka/EurekaProviderFactory.cs index da3c602ee..c1e545a28 100644 --- a/src/Ocelot.Provider.Eureka/EurekaProviderFactory.cs +++ b/src/Ocelot.Provider.Eureka/EurekaProviderFactory.cs @@ -16,8 +16,12 @@ public static class EurekaProviderFactory private static IServiceDiscoveryProvider CreateProvider(IServiceProvider provider, ServiceProviderConfiguration config, DownstreamRoute route) { var client = provider.GetService(); + if (client == null) + { + throw new NullReferenceException($"Cannot get an {nameof(IDiscoveryClient)} service during {nameof(CreateProvider)} operation to instanciate the {nameof(Eureka)} provider!"); + } - return Eureka.Equals(config.Type, StringComparison.OrdinalIgnoreCase) && client != null + return Eureka.Equals(config.Type, StringComparison.OrdinalIgnoreCase) ? new Eureka(route.ServiceName, client) : null; }