-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dependency injection sample (#30)
Fixes #9
- Loading branch information
Showing
9 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace TemporalioSamples.DependencyInjection; | ||
|
||
public interface IMyDatabaseClient | ||
{ | ||
Task<string> SelectValueAsync(string table); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace TemporalioSamples.DependencyInjection; | ||
|
||
using Temporalio.Activities; | ||
|
||
public class MyActivities | ||
{ | ||
private readonly IMyDatabaseClient databaseClient; | ||
|
||
public MyActivities(IMyDatabaseClient databaseClient) => this.databaseClient = databaseClient; | ||
|
||
[Activity] | ||
public Task<string> SelectFromDatabaseAsync(string table) => | ||
databaseClient.SelectValueAsync(table); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace TemporalioSamples.DependencyInjection; | ||
|
||
public class MyDatabaseClient : IMyDatabaseClient | ||
{ | ||
public Task<string> SelectValueAsync(string table) => | ||
Task.FromResult($"some-db-value from table {table}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace TemporalioSamples.DependencyInjection; | ||
|
||
using Temporalio.Workflows; | ||
|
||
[Workflow] | ||
public class MyWorkflow | ||
{ | ||
[WorkflowRun] | ||
public async Task<string> RunAsync() | ||
{ | ||
return await Workflow.ExecuteActivityAsync( | ||
(MyActivities act) => act.SelectFromDatabaseAsync("some-db-table"), | ||
new() | ||
{ | ||
StartToCloseTimeout = TimeSpan.FromMinutes(5), | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Temporalio.Client; | ||
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 client = await TemporalClient.ConnectAsync(new("localhost:7233") | ||
{ | ||
LoggerFactory = LoggerFactory.Create(builder => | ||
builder. | ||
AddSimpleConsole(options => options.TimestampFormat = "[HH:mm:ss] "). | ||
SetMinimumLevel(LogLevel.Information)), | ||
}); | ||
|
||
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Dependency Injection | ||
|
||
This sample shows an activity having a dependency injected and a worker running as a generic host. | ||
|
||
To run, first see [README.md](../../README.md) for prerequisites. Then, run the following from this directory | ||
in a separate terminal to start the worker: | ||
|
||
dotnet run worker | ||
|
||
Then in another terminal, run the workflow from this directory: | ||
|
||
dotnet run workflow | ||
|
||
This will show logs in the worker window of the workflow running. |
5 changes: 5 additions & 0 deletions
5
src/DependencyInjection/TemporalioSamples.DependencyInjection.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Worker"> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" /> | ||
</ItemGroup> | ||
</Project> |