|
| 1 | +using System.Text.Json.Serialization; |
| 2 | +using Azure.Core; |
| 3 | +using Azure.Identity; |
| 4 | +using Microsoft.DurableTask; |
| 5 | +using Microsoft.DurableTask.Client; |
| 6 | +using Microsoft.DurableTask.Worker; |
| 7 | +using Microsoft.DurableTask.Worker.AzureManaged; |
| 8 | +using Microsoft.DurableTask.Client.AzureManaged; |
| 9 | + |
| 10 | +WebApplicationBuilder builder = WebApplication.CreateBuilder(args); |
| 11 | + |
| 12 | +string endpointAddress = builder.Configuration["DURABLE_TASK_SCHEDULER_ENDPOINT_ADDRESS"] |
| 13 | + ?? throw new InvalidOperationException("Missing required configuration 'DURABLE_TASK_SCHEDULER_ENDPOINT_ADDRESS'"); |
| 14 | + |
| 15 | +string taskHubName = builder.Configuration["DURABLE_TASK_SCHEDULER_TASK_HUB_NAME"] |
| 16 | + ?? throw new InvalidOperationException("Missing required configuration 'DURABLE_TASK_SCHEDULER_TASK_HUB_NAME'"); |
| 17 | + |
| 18 | +TokenCredential credential = builder.Environment.IsProduction() |
| 19 | + ? new DefaultAzureCredential(new DefaultAzureCredentialOptions { ManagedIdentityClientId = builder.Configuration["CONTAINER_APP_UMI_CLIENT_ID"] }) |
| 20 | + : new DefaultAzureCredential(); |
| 21 | + |
| 22 | +// Add all the generated orchestrations and activities automatically |
| 23 | +builder.Services.AddDurableTaskWorker(builder => |
| 24 | +{ |
| 25 | + builder.AddTasks(r => r.AddAllGeneratedTasks()); |
| 26 | + builder.UseDurableTaskScheduler(endpointAddress, taskHubName, credential); |
| 27 | +}); |
| 28 | + |
| 29 | +// Register the client, which can be used to start orchestrations |
| 30 | +builder.Services.AddDurableTaskClient(builder => |
| 31 | +{ |
| 32 | + builder.UseDurableTaskScheduler(endpointAddress, taskHubName, credential); |
| 33 | +}); |
| 34 | + |
| 35 | +// Configure console logging using the simpler, more compact format |
| 36 | +builder.Services.AddLogging(logging => |
| 37 | +{ |
| 38 | + logging.AddSimpleConsole(options => |
| 39 | + { |
| 40 | + options.SingleLine = true; |
| 41 | + options.UseUtcTimestamp = true; |
| 42 | + options.TimestampFormat = "yyyy-MM-ddTHH:mm:ss.fffZ "; |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +// Configure the HTTP request pipeline |
| 47 | +builder.Services.AddControllers().AddJsonOptions(options => |
| 48 | +{ |
| 49 | + options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); |
| 50 | + options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; |
| 51 | +}); |
| 52 | + |
| 53 | +// The actual listen URL can be configured in environment variables named "ASPNETCORE_URLS" or "ASPNETCORE_URLS_HTTPS" |
| 54 | +WebApplication app = builder.Build(); |
| 55 | +app.MapControllers(); |
| 56 | +app.Run(); |
0 commit comments