Skip to content

Commit 7666bc9

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 01ce8d0 + 4f3ec9c commit 7666bc9

4 files changed

Lines changed: 35 additions & 17 deletions

File tree

ReadMe.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -768,16 +768,35 @@ The hooking behavior using `PosixSignalRegistration` is determined by the presen
768768
In the case of `Run/RunAsync` from `ConsoleAppBuilder`, you can also pass a CancellationToken. This is combined with PosixSignalRegistration and passed to each method. This makes it possible to cancel at any arbitrary timing.
769769

770770
```csharp
771-
var cancellationTokenSource = new CancellationTokenSource();
771+
// Create a CancellationTokenSource that will be cancelled when 'Q' is pressed.
772+
var cts = new CancellationTokenSource();
773+
_ = Task.Run(() =>
774+
{
775+
while (Console.ReadKey().Key != ConsoleKey.Q) ;
776+
Console.WriteLine();
777+
cts.Cancel();
778+
});
772779

773780
var app = ConsoleApp.Create();
774781

775-
app.Add("", (CancellationToken cancellationToken) =>
782+
app.Add("", async (CancellationToken cancellationToken) =>
776783
{
777-
// do anything...
784+
// CancellationToken will be triggered when 'Q' is pressed or Ctrl+C(SIGINT/SIGTERM/SIGKILL) is sent.
785+
try
786+
{
787+
for (int i = 0; i < 10; i++)
788+
{
789+
Console.WriteLine($"Running main task iteration {i + 1}/10. Press 'Q' to quit.");
790+
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
791+
}
792+
}
793+
catch (OperationCanceledException)
794+
{
795+
Console.WriteLine("Main task was cancelled.");
796+
}
778797
});
779798

780-
await app.RunAsync(args, cancellationTokenSource.Token); // pass external CancellationToken
799+
await app.RunAsync(args, cts.Token); // pass external CancellationToken
781800
```
782801

783802
Exit Code
@@ -1239,8 +1258,10 @@ If you want to use other DI container(like [DryIoc](https://github.com/dadhi/Dry
12391258
// dotnet add package DryIoc.Microsoft.DependencyInjection
12401259
var app = ConsoleApp.Create()
12411260
// setup DryIoc as the DI container
1242-
.ConfigureContainer(new DryIocServiceProviderFactory())
1243-
.ConfigureServices(services => services.AddSingleton<MyService>());
1261+
.ConfigureContainer(new DryIocServiceProviderFactory(), container =>
1262+
{
1263+
container.Register<MyService>();
1264+
});
12441265

12451266
app.Add("", ([FromServices] MyService service) => { });
12461267

sandbox/GeneratorSandbox/GeneratorSandbox.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.6" />
2121
<PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="6.2.0" />
2222

23-
<!--<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.6" />-->
23+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.6" />
2424
<!--
2525
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.6" />
2626
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.6" />

sandbox/GeneratorSandbox/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ConsoleAppFramework;
1+
using ConsoleAppFramework;
22

33
args = "9999 --x 1000".Split(' ');
44

src/ConsoleAppFramework/Emitter.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
855855
sb.AppendLine();
856856
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureContainer<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> factory, Action<TContainerBuilder>? configure = null) where TContainerBuilder : notnull"))
857857
{
858+
sb.AppendLine("this.isRequireCallBuildAndSetServiceProvider = true;");
858859
using (sb.BeginBlock("createServiceProvider = services =>"))
859860
{
860861
sb.AppendLine("var containerBuilder = factory.CreateBuilder(services);");
@@ -936,17 +937,13 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
936937
// Build
937938
using (sb.BeginBlock("partial void BuildAndSetServiceProvider(ConsoleAppContext context)"))
938939
{
939-
if (dllReference.HasDependencyInjection && dllReference.HasLogging)
940-
{
941-
sb.AppendLine("if (configureServices == null && configureLogging == null) return;");
942-
}
943-
else if (dllReference.HasDependencyInjection)
944-
{
945-
sb.AppendLine("if (configureServices == null) return;");
946-
}
947-
948940
if (dllReference.HasDependencyInjection)
949941
{
942+
using(sb.BeginBlock("if (!isRequireCallBuildAndSetServiceProvider)"))
943+
{
944+
sb.AppendLine("return;");
945+
}
946+
950947
if (dllReference.HasConfiguration)
951948
{
952949
sb.AppendLine("var config = configuration;");

0 commit comments

Comments
 (0)