Skip to content

Commit 69bc976

Browse files
committed
Add IServiceCollection.AddOptions extension method.
1 parent eacb408 commit 69bc976

File tree

6 files changed

+23
-5
lines changed

6 files changed

+23
-5
lines changed

Source/Boxed.AspNetCore/Boxed.AspNetCore.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<PropertyGroup Label="Package">
12-
<VersionPrefix>2.1.0</VersionPrefix>
12+
<VersionPrefix>2.2.0</VersionPrefix>
1313
<Authors>Muhammad Rehan Saeed (RehanSaeed.com)</Authors>
1414
<Product>ASP.NET Core Framework Boxed</Product>
1515
<Description>Provides ASP.NET Core middleware, MVC filters, extension methods and helper code for an ASP.NET Core project.</Description>
@@ -45,6 +45,7 @@
4545
<PackageReference Include="Microsoft.AspNetCore.Rewrite" Version="2.1.0" />
4646
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="2.1.0" />
4747
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.0" />
48+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.0" />
4849
<PackageReference Condition="'$(OS)' == 'Windows_NT'" Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" Version="1.0.0-beta-62925-02" />
4950
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
5051
<PackageReference Include="StyleCop.Analyzers" PrivateAssets="All" Version="1.1.0-beta007" />

Source/Boxed.AspNetCore/ServiceCollectionExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
namespace Boxed.AspNetCore
22
{
33
using System;
4+
using Microsoft.Extensions.Configuration;
45
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Options;
57

68
/// <summary>
79
/// <see cref="IServiceCollection"/> extension methods.
@@ -57,5 +59,20 @@ public static IServiceCollection AddIfElse(
5759

5860
return services;
5961
}
62+
63+
/// <summary>
64+
/// Registers <see cref="IOptions{TOptions}"/> and <typeparamref name="TOptions"/> to the services container.
65+
/// </summary>
66+
/// <typeparam name="TOptions">The type of the options.</typeparam>
67+
/// <param name="services">The services collection.</param>
68+
/// <param name="configuration">The configuration.</param>
69+
/// <returns>The same services collection.</returns>
70+
public static IServiceCollection AddOptions<TOptions>(
71+
this IServiceCollection services,
72+
IConfiguration configuration)
73+
where TOptions : class, new() =>
74+
services
75+
.Configure<TOptions>(configuration)
76+
.AddSingleton(x => x.GetRequiredService<IOptions<TOptions>>().Value);
6077
}
6178
}

Tests/Boxed.AspNetCore.Swagger.Test/Boxed.AspNetCore.Swagger.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<ItemGroup Label="Package References">
2222
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.1.0" />
2323
<PackageReference Include="Microsoft.AspNetCore.Mvc.ApiExplorer" Version="2.1.0" />
24-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
24+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
2525
<PackageReference Include="Moq" Version="4.10.0" />
2626
<PackageReference Include="StyleCop.Analyzers" PrivateAssets="All" Version="1.1.0-beta007" />
2727
<PackageReference Include="xunit" Version="2.4.0" />

Tests/Boxed.AspNetCore.TagHelpers.Test/Boxed.AspNetCore.TagHelpers.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<ItemGroup Label="Package References">
1616
<PackageReference Include="Microsoft.AspNetCore.Razor" Version="2.1.0" />
17-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
17+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
1818
<PackageReference Include="Moq" Version="4.10.0" />
1919
<PackageReference Include="StyleCop.Analyzers" PrivateAssets="All" Version="1.1.0-beta007" />
2020
<PackageReference Include="xunit" Version="2.4.0" />

Tests/Boxed.AspNetCore.Test/Boxed.AspNetCore.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<ItemGroup Label="Package References">
1616
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.1.0" />
1717
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.0" />
18-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
18+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
1919
<PackageReference Include="Moq" Version="4.10.0" />
2020
<PackageReference Include="StyleCop.Analyzers" PrivateAssets="All" Version="1.1.0-beta007" />
2121
<PackageReference Include="xunit" Version="2.4.0" />

Tests/Boxed.Mapping.Test/Boxed.Mapping.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</ItemGroup>
1414

1515
<ItemGroup Label="Package References">
16-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
16+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
1717
<PackageReference Include="Moq" Version="4.10.0" />
1818
<PackageReference Include="StyleCop.Analyzers" PrivateAssets="All" Version="1.1.0-beta007" />
1919
<PackageReference Include="xunit" Version="2.4.0" />

0 commit comments

Comments
 (0)