From ff9be685c208e961da30287a8b43f0d392f5e90a Mon Sep 17 00:00:00 2001 From: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com> Date: Tue, 2 Sep 2025 14:58:30 +0100 Subject: [PATCH] Remove namespace (#585) --- .../Program.cs | 85 +++++++++---------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step09_DependencyInjection/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step09_DependencyInjection/Program.cs index 36b7f1bedc..658ba65515 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step09_DependencyInjection/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step09_DependencyInjection/Program.cs @@ -38,7 +38,7 @@ builder.Services.AddSingleton((sp) => new ChatClientAgent( options: sp.GetRequiredService())); // Add a sample service that will use the agent to respond to user input. -builder.Services.AddHostedService(); +builder.Services.AddHostedService(); // Create a cancellation token and source to pass to the sample service that can // be used to signal shutdown of the application. @@ -50,50 +50,47 @@ builder.Services.AddKeyedSingleton("AppShutdown", appShutdownCancellationTokenSo using IHost host = builder.Build(); await host.RunAsync(appShutdownCancellationToken).ConfigureAwait(false); -namespace SampleApp +/// +/// A sample service that uses an AI agent to respond to user input. +/// +internal sealed class SampleService(AIAgent agent, [FromKeyedServices("AppShutdown")] CancellationTokenSource appShutdownCancellationTokenSource) : IHostedService { - /// - /// A sample service that uses an AI agent to respond to user input. - /// - internal sealed class SampleService(AIAgent agent, [FromKeyedServices("AppShutdown")] CancellationTokenSource appShutdownCancellationTokenSource) : IHostedService + private AgentThread? _thread; + + public async Task StartAsync(CancellationToken cancellationToken) { - private AgentThread? _thread; - - public async Task StartAsync(CancellationToken cancellationToken) - { - // Create a thread that will be used for the entirety of the service lifetime so that the user can ask follow up questions. - this._thread = agent.GetNewThread(); - _ = this.RunAsync(cancellationToken); - } - - public async Task RunAsync(CancellationToken cancellationToken) - { - // Delay a little to allow the service to finish starting. - await Task.Delay(100, cancellationToken); - - while (cancellationToken.IsCancellationRequested is false) - { - Console.WriteLine("\nAgent: Ask me to tell you a joke about a specific topic. To exit just press Ctrl+C or enter without any input.\n"); - Console.Write("> "); - var input = Console.ReadLine(); - - // If the user enters no input, signal the application to shut down. - if (string.IsNullOrWhiteSpace(input)) - { - appShutdownCancellationTokenSource.Cancel(); - break; - } - - // Stream the output to the console as it is generated. - await foreach (var update in agent.RunStreamingAsync(input, this._thread!, cancellationToken: cancellationToken)) - { - Console.Write(update); - } - - Console.WriteLine(); - } - } - - public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + // Create a thread that will be used for the entirety of the service lifetime so that the user can ask follow up questions. + this._thread = agent.GetNewThread(); + _ = this.RunAsync(cancellationToken); } + + public async Task RunAsync(CancellationToken cancellationToken) + { + // Delay a little to allow the service to finish starting. + await Task.Delay(100, cancellationToken); + + while (cancellationToken.IsCancellationRequested is false) + { + Console.WriteLine("\nAgent: Ask me to tell you a joke about a specific topic. To exit just press Ctrl+C or enter without any input.\n"); + Console.Write("> "); + var input = Console.ReadLine(); + + // If the user enters no input, signal the application to shut down. + if (string.IsNullOrWhiteSpace(input)) + { + appShutdownCancellationTokenSource.Cancel(); + break; + } + + // Stream the output to the console as it is generated. + await foreach (var update in agent.RunStreamingAsync(input, this._thread!, cancellationToken: cancellationToken)) + { + Console.Write(update); + } + + Console.WriteLine(); + } + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; }