Skip to content

Commit

Permalink
Add IServiceModelProxyBuilder extension method to facilitate options …
Browse files Browse the repository at this point in the history
…configuration
  • Loading branch information
Kralizek committed Feb 25, 2021
1 parent 5901d3d commit 0584ca3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
public static class ServiceModelProxyBuilderExtensions
{
/// <summary>
/// Configures the <see cref="IServiceModelProxyBuilder" /> by customizing the <see cref="ServiceModelProxyOptions"/> used when creating the proxy.
/// </summary>
/// <param name="builder">The <see cref="IServiceModelProxyBuilder"/>.</param>
/// <param name="configuration">A delegate to be used to configure the <see cref="ServiceModelProxyOptions"/>.</param>
/// <returns>An instance of <see cref="IServiceModelProxyBuilder"/>.</returns>
public static IServiceModelProxyBuilder Configure(this IServiceModelProxyBuilder builder, Action<ServiceModelProxyOptions> configuration)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));

_ = configuration ?? throw new ArgumentNullException(nameof(configuration));

builder.Services.Configure(builder.Name, configuration);

return builder;
}

/// <summary>
/// Configures the <see cref="IServiceModelProxyBuilder" /> to customize the <see cref="ServiceEndpoint" /> when creating the proxy.
/// </summary>
Expand All @@ -29,9 +46,7 @@ public static IServiceModelProxyBuilder ConfigureServiceEndpoint(this IServiceMo

_ = configuration ?? throw new ArgumentNullException(nameof(configuration));

builder.Services.Configure<ServiceModelProxyOptions>(builder.Name, options => options.EndpointConfigurations.Add(configuration));

return builder;
return Configure(builder, options => options.EndpointConfigurations.Add(configuration));
}

/// <summary>
Expand All @@ -46,9 +61,7 @@ public static IServiceModelProxyBuilder SetBinding(this IServiceModelProxyBuilde

_ = binding ?? throw new ArgumentNullException(nameof(binding));

builder.Services.Configure<ServiceModelProxyOptions>(builder.Name, options => options.BindingFactory = _ => binding);

return builder;
return Configure(builder, options => options.BindingFactory = _ => binding);
}

/// <summary>
Expand All @@ -63,9 +76,7 @@ public static IServiceModelProxyBuilder SetEndpointAddress(this IServiceModelPro

_ = endpoint ?? throw new ArgumentNullException(nameof(endpoint));

builder.Services.Configure<ServiceModelProxyOptions>(builder.Name, options => options.EndpointAddressFactory = _ => new EndpointAddress(endpoint.ToString()));

return builder;
return Configure(builder, options => options.EndpointAddressFactory = _ => new EndpointAddress(endpoint.ToString()));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using InsightArchitectures.Utilities.ServiceModel.Behaviors;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;

namespace Tests.DependencyInjection
Expand Down Expand Up @@ -112,6 +113,21 @@ public void ConfigureServiceEndpoint_registers_customization(IServiceModelProxyB
Assert.That(options.EndpointConfigurations, Contains.Item(configuration));
}

[Test, CustomAutoData]
public void Configure_is_guarded_against_nulls(GuardClauseAssertion assertion) => assertion.Verify(typeof (ServiceModelProxyBuilderExtensions).GetMethod(nameof(ServiceModelProxyBuilderExtensions.Configure)));

[Test, CustomAutoData]
public void Configure_registers_configuration(IServiceModelProxyBuilder builder, Action<ServiceModelProxyOptions> configuration)
{
builder.Configure(configuration);

var sp = builder.Services.BuildServiceProvider();

var options = sp.GetRequiredService<IOptionsMonitor<ServiceModelProxyOptions>>().Get(builder.Name);

Mock.Get(configuration).Verify(p => p(options));
}

[Test]
[CustomInlineAutoData(3, new[] { typeof(ITestClient), typeof(ITestService), typeof(TestChannelFactoryProxyWrapper) })]
[CustomInlineAutoData(4, new[] { typeof(ITestClient), typeof(ITestService), typeof(TestClient), typeof(TestClientBaseProxyWrapper) })]
Expand Down

0 comments on commit 0584ca3

Please sign in to comment.