Add dotnet activities for train interface.

This commit is contained in:
Rob Holland
2025-02-14 11:51:57 +00:00
parent 39462955eb
commit e085f02128
12 changed files with 157 additions and 23 deletions

52
enterprise/Program.cs Normal file
View File

@@ -0,0 +1,52 @@
using Microsoft.Extensions.DependencyInjection;
using Temporalio.Client;
using Temporalio.Worker;
using TrainSearchWorker.Activities;
var services = new ServiceCollection();
// Add HTTP client
services.AddHttpClient("TrainApi", client =>
{
client.BaseAddress = new Uri("http://localhost:8080/");
client.DefaultRequestHeaders.Add("Accept", "application/json");
});
// Add activities
services.AddScoped<TrainActivities>();
var serviceProvider = services.BuildServiceProvider();
// Create client
var client = await TemporalClient.ConnectAsync(new()
{
TargetHost = "localhost:7233",
});
// Create worker options
var options = new TemporalWorkerOptions("agent-task-queue-legacy");
// Register activities
var activities = serviceProvider.GetRequiredService<TrainActivities>();
options.AddActivity(activities.SearchTrains);
options.AddActivity(activities.BookTrains);
// Create and run worker
var worker = new TemporalWorker(client, options);
Console.WriteLine("Starting worker...");
using var tokenSource = new CancellationTokenSource();
Console.CancelKeyPress += (_, eventArgs) =>
{
eventArgs.Cancel = true;
tokenSource.Cancel();
};
try
{
await worker.ExecuteAsync(tokenSource.Token);
}
catch (OperationCanceledException)
{
Console.WriteLine("Worker shutting down...");
}