Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public static class ServiceDiscoveryReverseProxyServiceCollectionExtensions
/// </summary>
public static IReverseProxyBuilder AddServiceDiscoveryDestinationResolver(this IReverseProxyBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);

builder.Services.AddServiceDiscoveryCore();
builder.Services.AddSingleton<IDestinationResolver, ServiceDiscoveryDestinationResolver>();
return builder;
Expand All @@ -27,6 +29,8 @@ public static IReverseProxyBuilder AddServiceDiscoveryDestinationResolver(this I
/// </summary>
public static IServiceCollection AddHttpForwarderWithServiceDiscovery(this IServiceCollection services)
{
ArgumentNullException.ThrowIfNull(services);

return services.AddHttpForwarder().AddServiceDiscoveryForwarderFactory();
}

Expand All @@ -35,6 +39,8 @@ public static IServiceCollection AddHttpForwarderWithServiceDiscovery(this IServ
/// </summary>
public static IServiceCollection AddServiceDiscoveryForwarderFactory(this IServiceCollection services)
{
ArgumentNullException.ThrowIfNull(services);

services.AddServiceDiscoveryCore();
services.AddSingleton<IForwarderHttpClientFactory, ServiceDiscoveryForwarderHttpClientFactory>();
return services;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Microsoft.Extensions.ServiceDiscovery.Yarp.Tests;

#pragma warning disable IDE0200

public class YarpServiceDiscoveryPublicApiTests
{
[Fact]
public void AddServiceDiscoveryDestinationResolverShouldThrowWhenBuilderIsNull()
{
IReverseProxyBuilder builder = null!;

var action = () => builder.AddServiceDiscoveryDestinationResolver();

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}

[Fact]
public void AddHttpForwarderWithServiceDiscoveryShouldThrowWhenServicesIsNull()
{
IServiceCollection services = null!;

var action = () => services.AddHttpForwarderWithServiceDiscovery();

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(services), exception.ParamName);
}

[Fact]
public void AddServiceDiscoveryForwarderFactoryShouldThrowWhenServicesIsNull()
{
IServiceCollection services = null!;

var action = () => services.AddServiceDiscoveryForwarderFactory();

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(services), exception.ParamName);
}
}