|
1 | | -using Azure.Core; |
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// Copyright Microsoft Corporation |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | +// ---------------------------------------------------------------------------------- |
| 13 | + |
| 14 | +using System; |
| 15 | +using System.Threading; |
| 16 | +using System.Threading.Tasks; |
| 17 | +using Azure.Core; |
2 | 18 | using Azure.Identity; |
3 | 19 | using DurableTask.AzureStorage; |
4 | 20 | using DurableTask.Core; |
| 21 | +using Microsoft.Extensions.Azure; |
| 22 | +using Microsoft.Extensions.Logging; |
5 | 23 | using Microsoft.WindowsAzure.Storage.Auth; |
6 | 24 |
|
7 | | -internal class Program |
| 25 | +// Create a DefaultAzureCredential used to access the Azure Storage Account. |
| 26 | +// The identity will require the following roles on the resource: |
| 27 | +// - Azure Blob Data Contributor |
| 28 | +// - Azure Queue Data Contributor |
| 29 | +// - Azure Table Data Contributor |
| 30 | +DefaultAzureCredential credential = new(); |
| 31 | + |
| 32 | +// Create a diagnostic logger factory for reading telemetry |
| 33 | +ILoggerFactory loggerFactory = LoggerFactory.Create(b => b |
| 34 | + .AddConsole() |
| 35 | + .AddFilter("Azure.Core", LogLevel.Warning) |
| 36 | + .AddFilter("Azure.Identity", LogLevel.Warning)); |
| 37 | + |
| 38 | +// The Azure SDKs used by the Azure.Identity library write their telemetry via Event Sources |
| 39 | +using AzureEventSourceLogForwarder logForwarder = new(loggerFactory); |
| 40 | +logForwarder.Start(); |
| 41 | + |
| 42 | +NewTokenAndFrequency initialTokenInfo = await GetTokenInfoAsync(credential); |
| 43 | +AzureStorageOrchestrationService service = new(new AzureStorageOrchestrationServiceSettings |
8 | 44 | { |
9 | | - private static async Task Main(string[] args) |
| 45 | + StorageAccountDetails = new StorageAccountDetails |
10 | 46 | { |
11 | | - // Create credential based on the configuration |
12 | | - var credential = new DefaultAzureCredential(); |
13 | | - string[] scopes = new string[] { "https://storage.azure.com/.default" }; // Scope for Azure Storage |
14 | | - |
15 | | - static Task<NewTokenAndFrequency> RenewTokenFuncAsync(object state, CancellationToken cancellationToken) |
16 | | - { |
17 | | - var credential = new DefaultAzureCredential(); |
18 | | - var initialToken = credential.GetToken(new TokenRequestContext(new[] { "https://storage.azure.com/.default" })); |
19 | | - var expiresAfter = initialToken.ExpiresOn - DateTimeOffset.UtcNow - TimeSpan.FromMinutes(10); |
20 | | - return Task.FromResult(new NewTokenAndFrequency(initialToken.Token, expiresAfter)); |
21 | | - } |
22 | | - |
23 | | - // Get the token |
24 | | - var accessToken = await credential.GetTokenAsync(new Azure.Core.TokenRequestContext(scopes)); |
25 | | - |
26 | | - var service = new AzureStorageOrchestrationService(new AzureStorageOrchestrationServiceSettings |
27 | | - { |
28 | | - StorageAccountDetails = new StorageAccountDetails |
29 | | - { |
30 | | - AccountName = "YourStorageAccount", |
31 | | - EndpointSuffix = "core.windows.net", |
32 | | - StorageCredentials = new StorageCredentials(new Microsoft.WindowsAzure.Storage.Auth.TokenCredential( |
33 | | - accessToken.Token, |
34 | | - RenewTokenFuncAsync, |
35 | | - null, |
36 | | - TimeSpan.FromMinutes(5))) |
37 | | - } |
38 | | - }); |
39 | | - |
40 | | - var client = new TaskHubClient(service); |
41 | | - var worker = new TaskHubWorker(service); |
42 | | - |
43 | | - worker.AddTaskOrchestrations(typeof(SampleOrchestration)); |
44 | | - worker.AddTaskActivities(typeof(SampleActivity)); |
45 | | - |
46 | | - await worker.StartAsync(); |
47 | | - |
48 | | - var instance = await client.CreateOrchestrationInstanceAsync(typeof(SampleOrchestration), "World"); |
49 | | - |
50 | | - var result = await client.WaitForOrchestrationAsync(instance, TimeSpan.FromMinutes(1)); |
51 | | - |
52 | | - Console.WriteLine($"Orchestration result : {result.Output}"); |
53 | | - |
54 | | - await worker.StopAsync(); |
55 | | - } |
| 47 | + AccountName = "YourStorageAccount", |
| 48 | + EndpointSuffix = "core.windows.net", |
| 49 | + StorageCredentials = new StorageCredentials(new Microsoft.WindowsAzure.Storage.Auth.TokenCredential( |
| 50 | + initialTokenInfo.Token, |
| 51 | + GetTokenInfoAsync, |
| 52 | + credential, |
| 53 | + initialTokenInfo.Frequency.GetValueOrDefault())) |
| 54 | + }, |
| 55 | + LoggerFactory = loggerFactory, |
| 56 | +}); |
| 57 | + |
| 58 | +TaskHubClient client = new(service, loggerFactory: loggerFactory); |
| 59 | +TaskHubWorker worker = new(service, loggerFactory); |
| 60 | + |
| 61 | +worker.AddTaskOrchestrations(typeof(SampleOrchestration)); |
| 62 | +worker.AddTaskActivities(typeof(SampleActivity)); |
| 63 | + |
| 64 | +await worker.StartAsync(); |
| 65 | + |
| 66 | +OrchestrationInstance instance = await client.CreateOrchestrationInstanceAsync(typeof(SampleOrchestration), "World"); |
| 67 | +OrchestrationState state = await client.WaitForOrchestrationAsync(instance, TimeSpan.FromMinutes(1)); |
| 68 | + |
| 69 | +ILogger logger = loggerFactory.CreateLogger(nameof(Program)); |
| 70 | +logger.LogInformation("Orchestration output: {Output}", state.Output); |
| 71 | + |
| 72 | +await worker.StopAsync(); |
| 73 | + |
| 74 | +static async Task<NewTokenAndFrequency> GetTokenInfoAsync(object state, CancellationToken cancellationToken = default) |
| 75 | +{ |
| 76 | + const string AzureStorageScope = "https://storage.azure.com/.default"; |
| 77 | + |
| 78 | + if (state is not DefaultAzureCredential credential) |
| 79 | + throw new InvalidOperationException(); |
| 80 | + |
| 81 | + AccessToken accessToken = await credential.GetTokenAsync(new TokenRequestContext([AzureStorageScope]), cancellationToken); |
| 82 | + TimeSpan refreshFrequency = accessToken.ExpiresOn - DateTimeOffset.UtcNow - TimeSpan.FromMinutes(10); // 10 minutes before expiration |
| 83 | + return new NewTokenAndFrequency(accessToken.Token, refreshFrequency); |
56 | 84 | } |
57 | 85 |
|
58 | | -public class SampleOrchestration : TaskOrchestration<string, string> |
| 86 | +internal sealed class SampleOrchestration : TaskOrchestration<string, string> |
59 | 87 | { |
60 | | - public override async Task<string> RunTask(OrchestrationContext context, string input) |
61 | | - { |
62 | | - return await context.ScheduleTask<string>(typeof(SampleActivity), input); |
63 | | - } |
| 88 | + public override Task<string> RunTask(OrchestrationContext context, string input) => |
| 89 | + context.ScheduleTask<string>(typeof(SampleActivity), input); |
64 | 90 | } |
65 | 91 |
|
66 | | -public class SampleActivity : TaskActivity<string, string> |
| 92 | +internal sealed class SampleActivity : TaskActivity<string, string> |
67 | 93 | { |
68 | | - protected override string Execute(TaskContext context, string input) |
69 | | - { |
70 | | - return "Hello, " + input + "!"; |
71 | | - } |
| 94 | + protected override string Execute(TaskContext context, string input) => |
| 95 | + "Hello, " + input + "!"; |
72 | 96 | } |
0 commit comments