88using Microsoft . Extensions . DependencyInjection ;
99using Microsoft . Extensions . Hosting ;
1010
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 ) ;
1912
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
2716
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 ( ) ;
3224
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 >
3533 {
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+ } ;
5038
51- builder . UseGrpc ( ) ; // multiple overloads available for providing gRPC information
39+ return greetings ;
5240 } ) ;
5341
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
5845
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 => { } ) ;
6552
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 ) ;
7258
59+ IHost host = builder . Build ( ) ;
7360await host . StartAsync ( ) ;
7461
7562await using DurableTaskClient client = host . Services . GetRequiredService < DurableTaskClient > ( ) ;
@@ -82,4 +69,4 @@ await context.CallActivityAsync<string>("SayHello", "Seattle"),
8269 getInputsAndOutputs : true ,
8370 cts . Token ) ;
8471
85- Console . WriteLine ( $ "Instance completed: { instance } ") ;
72+ Console . WriteLine ( $ "Instance completed: { instance } ") ;
0 commit comments