Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions src/AspNet/Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,19 @@
// Setup console logging
builder.Logging.AddSimpleConsole().SetMinimumLevel(LogLevel.Information);

// Set a singleton for the client _task_. Errors will not happen here, only when
// the await is performed.
builder.Services.AddSingleton(ctx =>
// TODO(cretz): It is not great practice to pass around tasks to be awaited
// on separately (VSTHRD003). We may prefer a direct DI extension, see
// https://github.com/temporalio/sdk-dotnet/issues/46.
TemporalClient.ConnectAsync(new()
{
TargetHost = "localhost:7233",
LoggerFactory = ctx.GetRequiredService<ILoggerFactory>(),
}));
var connection = await TemporalConnection.ConnectAsync(new TemporalConnectionOptions("localhost:7233"));
builder.Services.AddSingleton<ITemporalClient>(provider => new TemporalClient(connection, new()
{
LoggerFactory = provider.GetRequiredService<ILoggerFactory>(),
}));

var app = builder.Build();

app.MapGet("/", async (Task<TemporalClient> clientTask, string? name) =>
app.MapGet("/", async (ITemporalClient client, string? name) =>
{
var client = await clientTask;
return await client.ExecuteWorkflowAsync(
(MyWorkflow wf) => wf.RunAsync(name ?? "Temporal"),
new(id: $"aspnet-sample-workflow-{Guid.NewGuid()}", taskQueue: MyWorkflow.TaskQueue));
});

app.Run();
await app.RunAsync();