8
8
using Microsoft . Extensions . DependencyInjection ;
9
9
using Microsoft . Extensions . Hosting ;
10
10
11
- IHost host = Host . CreateDefaultBuilder ( args )
12
- . ConfigureServices ( services =>
13
- {
14
- services . AddDurableTaskClient ( builder =>
15
- {
16
- // Configure options for this builder. Can be omitted if no options customization is needed.
17
- builder . Configure ( opt => { } ) ;
18
- builder . UseGrpc ( ) ; // multiple overloads available for providing gRPC information
11
+ HostApplicationBuilder builder = Host . CreateApplicationBuilder ( args ) ;
19
12
20
- // AddDurableTaskClient allows for multiple named clients by passing in a name as the first argument.
21
- // When using a non-default named client, you will need to make this call below to have the
22
- // DurableTaskClient added directly to the DI container. Otherwise IDurableTaskClientProvider must be used
23
- // to retrieve DurableTaskClients by name from the DI container. In this case, we are using the default
24
- // name, so the line below is NOT required as it was already called for us.
25
- builder . RegisterDirectly ( ) ;
26
- } ) ;
13
+ IDurableTaskClientBuilder clientBuilder = builder . Services . AddDurableTaskClient ( )
14
+ . Configure ( opt => { } ) // configure options for this builder, if desired.
15
+ . UseGrpc ( ) ; // multiple overloads available for providing gRPC information
27
16
28
- services . AddDurableTaskWorker ( builder =>
29
- {
30
- // Configure options for this builder. Can be omitted if no options customization is needed.
31
- builder . Configure ( opt => { } ) ;
17
+ // OPTIONAL STEP
18
+ // AddDurableTaskClient allows for multiple named clients by passing in a name as the first argument.
19
+ // When using a non-default named client, you will need to make this call below to have the
20
+ // DurableTaskClient added directly to the DI container. Otherwise IDurableTaskClientProvider must be used
21
+ // to retrieve DurableTaskClients by name from the DI container. In this case, we are using the default
22
+ // name, so the line below is NOT required as it was already called for us.
23
+ clientBuilder . RegisterDirectly ( ) ;
32
24
33
- // Register orchestrators and activities.
34
- builder . AddTasks ( tasks =>
25
+ builder . Services . AddDurableTaskWorker ( )
26
+ . Configure ( opt => { } ) // configure options for this builder.
27
+ . AddTasks ( tasks =>
28
+ {
29
+ // Add tasks to the worker.
30
+ tasks . AddOrchestratorFunc ( "HelloSequence" , async context =>
31
+ {
32
+ var greetings = new List < string >
35
33
{
36
- tasks . AddOrchestratorFunc ( "HelloSequence" , async context =>
37
- {
38
- var greetings = new List < string >
39
- {
40
- await context . CallActivityAsync < string > ( "SayHello" , "Tokyo" ) ,
41
- await context . CallActivityAsync < string > ( "SayHello" , "London" ) ,
42
- await context . CallActivityAsync < string > ( "SayHello" , "Seattle" ) ,
43
- } ;
44
-
45
- return greetings ;
46
- } ) ;
47
-
48
- tasks . AddActivityFunc < string , string > ( "SayHello" , ( context , city ) => $ "Hello { city } !") ;
49
- } ) ;
34
+ await context . CallActivityAsync < string > ( "SayHello" , "Tokyo" ) ,
35
+ await context . CallActivityAsync < string > ( "SayHello" , "London" ) ,
36
+ await context . CallActivityAsync < string > ( "SayHello" , "Seattle" ) ,
37
+ } ;
50
38
51
- builder . UseGrpc ( ) ; // multiple overloads available for providing gRPC information
39
+ return greetings ;
52
40
} ) ;
53
41
54
- // Can also configure worker and client options through all the existing options config methods.
55
- // These are equivalent to the 'builder.Configure' calls above.
56
- services . Configure < DurableTaskWorkerOptions > ( opt => { } ) ;
57
- services . Configure < DurableTaskClientOptions > ( opt => { } ) ;
42
+ tasks . AddActivityFunc < string , string > ( "SayHello" , ( context , city ) => $ "Hello { city } !") ;
43
+ } )
44
+ . UseGrpc ( ) ; // multiple overloads available for providing gRPC information
58
45
59
- // Registry can also be done via options pattern. This is equivalent to the 'builder.AddTasks' call above.
60
- // You can use all the tools options pattern has available. For example, if you have multiple workers you could
61
- // use ConfigureAll<DurableTaskRegistry> to add tasks to ALL workers in one go. Otherwise, you need to use
62
- // named option configuration to register to specific workers (where the name matches the name passed to
63
- // 'AddDurableTaskWorker(name?, builder)').
64
- services . Configure < DurableTaskRegistry > ( registry => { } ) ;
46
+ // OPTIONAL STEP
47
+ // Client and Worker options can also be configured through the options pattern.
48
+ // When using the options pattern, configure with the same name as the builder.
49
+ builder . Services . Configure < DurableTaskClientOptions > ( opt => { } ) ;
50
+ builder . Services . Configure < DurableTaskWorkerOptions > ( opt => { } ) ;
51
+ builder . Services . Configure < DurableTaskRegistry > ( registry => { } ) ;
65
52
66
- // You can configure custom data converter multiple ways. One is through worker/client options configuration.
67
- // Alternatively, data converter will be used from the service provider if available (as a singleton) AND no
68
- // converter was explicitly set on the options.
69
- services . AddSingleton < DataConverter > ( JsonDataConverter . Default ) ;
70
- } )
71
- . Build ( ) ;
53
+ // OPTIONAL STEP
54
+ // You can configure custom data converter multiple ways. One is through worker/client options configuration.
55
+ // Alternatively, data converter will be used from the service provider if available (as a singleton) AND no
56
+ // converter was explicitly set on the options.
57
+ builder . Services . AddSingleton < DataConverter > ( JsonDataConverter . Default ) ;
72
58
59
+ IHost host = builder . Build ( ) ;
73
60
await host . StartAsync ( ) ;
74
61
75
62
await using DurableTaskClient client = host . Services . GetRequiredService < DurableTaskClient > ( ) ;
@@ -82,4 +69,4 @@ await context.CallActivityAsync<string>("SayHello", "Seattle"),
82
69
getInputsAndOutputs : true ,
83
70
cts . Token ) ;
84
71
85
- Console . WriteLine ( $ "Instance completed: { instance } ") ;
72
+ Console . WriteLine ( $ "Instance completed: { instance } ") ;
0 commit comments