-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dependency injection sample #30
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat. Ez.