Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
116 lines (99 loc) · 3.85 KB
/
Copy pathProgram.cs
File metadata and controls
116 lines (99 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using Azure.Identity;
using Azure.Monitor.OpenTelemetry.Exporter;
using Azure.Security.KeyVault.Secrets;
using Azure.Storage.Blobs;
using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Azure.Functions.Worker.OpenTelemetry;
using Microsoft.DurableTask;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SendGrid;
using System;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
// Register Azure Functions
FunctionsApplicationBuilder tempBuilder = FunctionsApplication.CreateBuilder( args );
tempBuilder.Services.AddOpenTelemetry().UseFunctionsWorkerDefaults().UseAzureMonitorExporter();
// Azure Identity/Key Vault
DefaultAzureCredential tempAzureCredential = new();
SecretClient tempSecretClient = new( new Uri( GetRequiredConfigValue( tempBuilder.Configuration, "KEY_VAULT_URI" ) ), tempAzureCredential );
KeyVaultSecret tempSendGridKey = await tempSecretClient.GetSecretAsync( GetRequiredConfigValue( tempBuilder.Configuration, "SENDGRID_PRIVATE_KEY_NAME" ) );
KeyVaultSecret tempDocuSignPrivateKey = await tempSecretClient.GetSecretAsync( GetRequiredConfigValue( tempBuilder.Configuration, "DOCUSIGN_PRIVATE_KEY_NAME" ) );
// SendGrid
tempBuilder.Services.AddSingleton<ISendGridClient>( new SendGridClient( tempSendGridKey.Value ) );
tempBuilder.Services.AddSingleton<DataService.SendGrid.IService, DataService.SendGrid.Service>();
// DocuSign
tempBuilder.Services.AddSingleton<DataService.DocuSign.AuthenticationOptions>
(
_ => new
(
GetRequiredConfigValue( tempBuilder.Configuration, "DOCUSIGN_CLIENT_URI" ),
GetRequiredConfigValue( tempBuilder.Configuration, "DOCUSIGN_AUTH_SERVER" ),
GetRequiredConfigValue( tempBuilder.Configuration, "DOCUSIGN_INTEGRATION_KEY" ),
GetRequiredConfigValue( tempBuilder.Configuration, "DOCUSIGN_USER_ID" ),
GetRequiredConfigValue( tempBuilder.Configuration, "DOCUSIGN_ACCOUNT_ID" ),
Encoding.UTF8.GetBytes( tempDocuSignPrivateKey.Value ),
GetRequiredIntConfigValue( tempBuilder.Configuration, "DOCUSIGN_TOKEN_EXPIRATION_HOURS" ),
GetRequiredIntConfigValue( tempBuilder.Configuration, "DOCUSIGN_MAX_TOKEN_ATTEMPTS" )
)
);
tempBuilder.Services.AddSingleton<DataService.DocuSign.IService, DataService.DocuSign.Service>();
// Blob
BlobServiceClient tempBlobClient = new( new Uri( GetRequiredConfigValue( tempBuilder.Configuration, "BLOB_URI" ) ), tempAzureCredential );
tempBuilder.Services.AddSingleton( tempBlobClient.GetBlobContainerClient( GetRequiredConfigValue( tempBuilder.Configuration, "BLOB_CONTAINER" ) ) );
// MSSQL
tempBuilder.Services.AddPooledDbContextFactory<DataService.SQL.ERP.Context>
(
tOptions =>
{
tOptions.UseSqlServer( GetRequiredConfigValue( tempBuilder.Configuration, "SQL_CONNECTION" ), tOptions => tOptions.EnableRetryOnFailure() );
}
);
// JSON
tempBuilder.Services.Configure<JsonSerializerOptions>
(
tOptions =>
{
tOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
tOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
}
);
// Durable Functions Retry
tempBuilder.Services.AddSingleton
(
TaskOptions.FromRetryPolicy
(
new
(
20,
TimeSpan.FromMinutes( 1 ),
2,
TimeSpan.FromMinutes( 30 ),
TimeSpan.FromHours( 6 )
)
)
);
// Build
await tempBuilder.Build().RunAsync();
// Helpers
static string GetRequiredConfigValue( IConfiguration tConfig, string tKey )
{
string tempValue = tConfig[ tKey ];
if ( string.IsNullOrWhiteSpace( tempValue ) )
{
throw new InvalidOperationException( $"{tKey} is not set." );
}
return tempValue;
}
static int GetRequiredIntConfigValue( IConfiguration tConfig, string tKey )
{
string tempValue = GetRequiredConfigValue( tConfig, tKey );
if ( !int.TryParse( tempValue, out int tempInt ) )
{
throw new InvalidOperationException( $"{tKey} must be an integer." );
}
return tempInt;
}