Skip to content

Commit 830f5b4

Browse files
authored
Dev (#11)
* - add option to change endpoint service lifetime during service registration - add overload to add endpoints from assemblies containing a given type - rename core endpoint registration methods for clarity * - update deps to latest * - bump version
1 parent 1e23c32 commit 830f5b4

File tree

7 files changed

+37
-18
lines changed

7 files changed

+37
-18
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1919
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
2020

21-
<Version>0.6.1</Version>
21+
<Version>0.6.2</Version>
2222
</PropertyGroup>
2323
</Project>

samples/ShowcaseWebApi/Extensions/WebApplicationBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static WebApplicationBuilder AddWebServices(this WebApplicationBuilder bu
8787

8888
public static WebApplicationBuilder AddFeatures(this WebApplicationBuilder builder)
8989
{
90-
builder.Services.AddModEndpointsFromAssembly(typeof(WebApplicationBuilderExtensions).Assembly);
90+
builder.Services.AddModEndpointsFromAssemblyContaining<GetBookById>();
9191
builder.Services.AddValidatorsFromAssemblyContaining<GetBookByIdRequestValidator>(includeInternalTypes: true);
9292

9393
return builder;

samples/ShowcaseWebApi/ShowcaseWebApi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.11.0" />
1111
<PackageReference Include="Asp.Versioning.Http" Version="8.1.0" />
1212
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
13-
<PackageReference Include="UUIDNext" Version="4.1.0" />
13+
<PackageReference Include="UUIDNext" Version="4.1.1" />
1414
</ItemGroup>
1515

1616
<ItemGroup>

src/ModEndpoints.Core/DependencyInjectionExtensions.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,28 @@
99
namespace ModEndpoints.Core;
1010
public static class DependencyInjectionExtensions
1111
{
12-
public static IServiceCollection AddModEndpointsFromAssemblyCore(
12+
public static IServiceCollection AddModEndpointsCoreFromAssemblyContaining<T>(
1313
this IServiceCollection services,
14-
Assembly assembly)
14+
Assembly assembly,
15+
ServiceLifetime lifetime = ServiceLifetime.Transient)
16+
{
17+
return services.AddModEndpointsCoreFromAssembly(typeof(T).Assembly, lifetime);
18+
}
19+
20+
public static IServiceCollection AddModEndpointsCoreFromAssembly(
21+
this IServiceCollection services,
22+
Assembly assembly,
23+
ServiceLifetime lifetime = ServiceLifetime.Transient)
1524
{
1625
return services
17-
.AddRouteGroupsFromAssemblyCore(assembly)
18-
.AddEndpointsFromAssemblyCore(assembly);
26+
.AddRouteGroupsCoreFromAssembly(assembly, lifetime)
27+
.AddEndpointsCoreFromAssembly(assembly, lifetime);
1928
}
2029

21-
private static IServiceCollection AddRouteGroupsFromAssemblyCore(
30+
private static IServiceCollection AddRouteGroupsCoreFromAssembly(
2231
this IServiceCollection services,
23-
Assembly assembly)
32+
Assembly assembly,
33+
ServiceLifetime lifetime)
2434
{
2535
//Don't add RootRouteGroup, it's just a marker class to define root
2636
//Normally its assembly won't be loaded with this method anyway but just in case
@@ -29,17 +39,18 @@ private static IServiceCollection AddRouteGroupsFromAssemblyCore(
2939
.Where(type => type is { IsAbstract: false, IsInterface: false } &&
3040
type.IsAssignableTo(typeof(IRouteGroupConfigurator)) &&
3141
type != typeof(RootRouteGroup))
32-
.Select(type => ServiceDescriptor.KeyedTransient(typeof(IRouteGroupConfigurator), type, type))
42+
.Select(type => ServiceDescriptor.DescribeKeyed(typeof(IRouteGroupConfigurator), type, type, lifetime))
3343
.ToArray();
3444

3545
services.TryAddEnumerable(serviceDescriptors);
3646

3747
return services;
3848
}
3949

40-
public static IServiceCollection AddEndpointsFromAssemblyCore(
50+
public static IServiceCollection AddEndpointsCoreFromAssembly(
4151
this IServiceCollection services,
42-
Assembly assembly)
52+
Assembly assembly,
53+
ServiceLifetime lifetime)
4354
{
4455
var endpointTypes = assembly
4556
.DefinedTypes
@@ -49,7 +60,7 @@ public static IServiceCollection AddEndpointsFromAssemblyCore(
4960
CheckServiceEndpointRegistrations(endpointTypes);
5061

5162
var serviceDescriptors = endpointTypes
52-
.Select(type => ServiceDescriptor.KeyedTransient(typeof(IEndpointConfigurator), type, type))
63+
.Select(type => ServiceDescriptor.DescribeKeyed(typeof(IEndpointConfigurator), type, type, lifetime))
5364
.ToArray();
5465

5566
services.TryAddEnumerable(serviceDescriptors);

src/ModEndpoints.RemoteServices/ModEndpoints.RemoteServices.csproj

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

2323
<ItemGroup>
2424
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.2" />
25-
<PackageReference Include="ModResults" Version="0.4.2" />
25+
<PackageReference Include="ModResults" Version="1.0.0" />
2626
</ItemGroup>
2727

2828
<ItemGroup>

src/ModEndpoints/DependencyInjectionExtensions.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@
88
namespace ModEndpoints;
99
public static class DependencyInjectionExtensions
1010
{
11+
public static IServiceCollection AddModEndpointsFromAssemblyContaining<T>(
12+
this IServiceCollection services,
13+
ServiceLifetime lifetime = ServiceLifetime.Transient)
14+
{
15+
return services.AddModEndpointsFromAssembly(typeof(T).Assembly, lifetime);
16+
}
17+
1118
public static IServiceCollection AddModEndpointsFromAssembly(
1219
this IServiceCollection services,
13-
Assembly assembly)
20+
Assembly assembly,
21+
ServiceLifetime lifetime = ServiceLifetime.Transient)
1422
{
1523
//WebResultEndpoint components
1624
services.TryAddKeyedSingleton<IResultToResponseMapper, DefaultResultToResponseMapper>(
@@ -24,7 +32,7 @@ public static IServiceCollection AddModEndpointsFromAssembly(
2432
services.TryAddSingleton<IUriResolverProvider, DefaultUriResolverProvider>();
2533

2634
services.AddHttpContextAccessor();
27-
return services.AddModEndpointsFromAssemblyCore(assembly);
35+
return services.AddModEndpointsCoreFromAssembly(assembly, lifetime);
2836
}
2937

3038
public static WebApplication MapModEndpoints(

src/ModEndpoints/ModEndpoints.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
</ItemGroup>
2222

2323
<ItemGroup>
24-
<PackageReference Include="ModResults.FluentValidation" Version="0.4.2" />
25-
<PackageReference Include="ModResults.MinimalApis" Version="0.4.2" />
24+
<PackageReference Include="ModResults.FluentValidation" Version="1.0.0" />
25+
<PackageReference Include="ModResults.MinimalApis" Version="1.0.0" />
2626
</ItemGroup>
2727

2828
<ItemGroup>

0 commit comments

Comments
 (0)