Skip to content

Commit f5938c8

Browse files
authored
cleanup: further code deduplication on new hosting APIs (#416)
1 parent 643f9c3 commit f5938c8

File tree

2 files changed

+27
-58
lines changed

2 files changed

+27
-58
lines changed

src/Hosting.CommandLine/HostBuilderExtensions.cs

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static async Task<int> RunCommandLineApplicationAsync<TApp>(
3535
CancellationToken cancellationToken = default)
3636
where TApp : class
3737
{
38-
return await RunCommandLineApplicationAsync<TApp>(hostBuilder, args, app => { }, cancellationToken);
38+
return await RunCommandLineApplicationAsync<TApp>(hostBuilder, args, null, cancellationToken);
3939
}
4040

4141
/// <summary>
@@ -56,18 +56,7 @@ public static async Task<int> RunCommandLineApplicationAsync<TApp>(
5656
CancellationToken cancellationToken = default)
5757
where TApp : class
5858
{
59-
var state = new CommandLineState(args);
60-
hostBuilder.Properties[typeof(CommandLineState)] = state;
61-
hostBuilder.ConfigureServices(
62-
(context, services)
63-
=>
64-
{
65-
services.AddCommonServices(state);
66-
services.AddSingleton<ICommandLineService, CommandLineService<TApp>>();
67-
services.AddSingleton(configure);
68-
});
69-
70-
using var host = hostBuilder.Build();
59+
using var host = hostBuilder.UseCommandLineApplication<TApp>(args, configure).Build();
7160
return await host.RunCommandLineApplicationAsync(cancellationToken);
7261
}
7362

@@ -88,16 +77,14 @@ public static async Task<int> RunCommandLineApplicationAsync(
8877
Action<CommandLineApplication> configure,
8978
CancellationToken cancellationToken = default)
9079
{
80+
configure ??= _ => { };
9181
var state = new CommandLineState(args);
9282
hostBuilder.Properties[typeof(CommandLineState)] = state;
93-
hostBuilder.ConfigureServices(
94-
(context, services)
95-
=>
96-
{
97-
services.AddCommonServices(state);
98-
services.AddSingleton<ICommandLineService, CommandLineService>();
99-
services.AddSingleton(configure);
100-
});
83+
hostBuilder.ConfigureServices((context, services) =>
84+
services
85+
.AddCommonServices(state)
86+
.AddSingleton<ICommandLineService, CommandLineService>()
87+
.AddSingleton(configure));
10188

10289
using var host = hostBuilder.Build();
10390
return await host.RunCommandLineApplicationAsync(cancellationToken);
@@ -131,50 +118,32 @@ public static IHostBuilder UseCommandLineApplication<TApp>(
131118
Action<CommandLineApplication<TApp>> configure)
132119
where TApp : class
133120
{
134-
configure ??= app => { };
121+
configure ??= _ => { };
135122
var state = new CommandLineState(args);
136123
hostBuilder.Properties[typeof(CommandLineState)] = state;
137-
hostBuilder.ConfigureServices(
138-
(context, services) =>
139-
{
140-
services
141-
.TryAddSingleton<StoreExceptionHandler>();
142-
services
143-
.TryAddSingleton<IUnhandledExceptionHandler>(provider => provider.GetRequiredService<StoreExceptionHandler>());
144-
services
145-
.AddSingleton<IHostLifetime, CommandLineLifetime>()
146-
.TryAddSingleton(PhysicalConsole.Singleton);
147-
services
148-
.AddSingleton(provider =>
149-
{
150-
state.SetConsole(provider.GetService<IConsole>());
151-
return state;
152-
})
153-
.AddSingleton<CommandLineContext>(state)
154-
.AddSingleton<ICommandLineService, CommandLineService<TApp>>();
155-
services
156-
.AddSingleton(configure);
157-
});
124+
hostBuilder.ConfigureServices((context, services) =>
125+
services
126+
.AddCommonServices(state)
127+
.AddSingleton<ICommandLineService, CommandLineService<TApp>>()
128+
.AddSingleton(configure));
158129

159130
return hostBuilder;
160131
}
161132

162-
private static void AddCommonServices(this IServiceCollection services, CommandLineState state)
133+
private static IServiceCollection AddCommonServices(this IServiceCollection services, CommandLineState state)
163134
{
164-
services
165-
.TryAddSingleton<StoreExceptionHandler>();
166-
services
167-
.TryAddSingleton<IUnhandledExceptionHandler>(provider => provider.GetRequiredService<StoreExceptionHandler>());
135+
services.TryAddSingleton<StoreExceptionHandler>();
136+
services.TryAddSingleton<IUnhandledExceptionHandler>(provider => provider.GetRequiredService<StoreExceptionHandler>());
137+
services.TryAddSingleton(PhysicalConsole.Singleton);
168138
services
169139
.AddSingleton<IHostLifetime, CommandLineLifetime>()
170-
.TryAddSingleton(PhysicalConsole.Singleton);
171-
services
172140
.AddSingleton(provider =>
173141
{
174142
state.SetConsole(provider.GetService<IConsole>());
175143
return state;
176144
})
177145
.AddSingleton<CommandLineContext>(state);
146+
return services;
178147
}
179148
}
180149
}

src/Hosting.CommandLine/HostExtensions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Copyright (c) Nate McMaster.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
5+
using System.Runtime.ExceptionServices;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using McMaster.Extensions.CommandLineUtils;
9+
using McMaster.Extensions.Hosting.CommandLine.Internal;
10+
using Microsoft.Extensions.DependencyInjection;
11+
412
namespace Microsoft.Extensions.Hosting
513
{
6-
using System;
7-
using System.Runtime.ExceptionServices;
8-
using System.Threading;
9-
using System.Threading.Tasks;
10-
using McMaster.Extensions.CommandLineUtils;
11-
using McMaster.Extensions.Hosting.CommandLine.Internal;
12-
using Microsoft.Extensions.DependencyInjection;
13-
1414
/// <summary>
1515
/// Extension methods for <see cref="IHost" /> support.
1616
/// </summary>

0 commit comments

Comments
 (0)