-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathProgram.cs
More file actions
55 lines (50 loc) · 1.94 KB
/
Program.cs
File metadata and controls
55 lines (50 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using Temporalio.Client;
using Temporalio.Common.EnvConfig;
using Temporalio.Extensions.Hosting;
using TemporalioSamples.DependencyInjection;
async Task RunWorkerAsync()
{
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureLogging(ctx =>
ctx.AddSimpleConsole().SetMinimumLevel(LogLevel.Information))
.ConfigureServices(ctx =>
ctx.
// Add the database client at the scoped level
AddScoped<IMyDatabaseClient, MyDatabaseClient>().
// Add the worker
AddHostedTemporalWorker(
clientTargetHost: "localhost:7233",
clientNamespace: "default",
taskQueue: "dependency-injection-sample").
// Add the activities class at the scoped level
AddScopedActivities<MyActivities>().
AddWorkflow<MyWorkflow>())
.Build();
await host.RunAsync();
}
async Task ExecuteWorkflowAsync()
{
var connectOptions = ClientEnvConfig.LoadClientConnectOptions();
connectOptions.TargetHost ??= "localhost:7233";
connectOptions.LoggerFactory = LoggerFactory.Create(builder =>
builder.
AddSimpleConsole(options => options.TimestampFormat = "[HH:mm:ss] ").
SetMinimumLevel(LogLevel.Information));
var client = await TemporalClient.ConnectAsync(connectOptions);
Console.WriteLine("Executing workflow");
var result = await client.ExecuteWorkflowAsync(
(MyWorkflow wf) => wf.RunAsync(),
new(id: "dependency-injection-workflow-id", taskQueue: "dependency-injection-sample"));
Console.WriteLine("Workflow result: {0}", result);
}
switch (args.ElementAtOrDefault(0))
{
case "worker":
await RunWorkerAsync();
break;
case "workflow":
await ExecuteWorkflowAsync();
break;
default:
throw new ArgumentException("Must pass 'worker' or 'workflow' as the single argument");
}