Skip to content

Commit b380b77

Browse files
feat: Azure ServiceBus Client Auth and Keyed Services (#1221)
* refactor azure servicebus client * sonar qube and xml comment * changed lifetime of keyed Storage client
1 parent 156614a commit b380b77

File tree

8 files changed

+65
-23
lines changed

8 files changed

+65
-23
lines changed

application/CohortManager/src/Functions/CohortDistributionServices/DistributeParticipant/DistributeParticipant.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="2.0.0" />
1616
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.3.0" />
1717
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.0.2" />
18-
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.22.2" />
18+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.23.0" />
1919
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.4" />
2020
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="9.0.2" />
2121
<PackageReference Include="Grpc.Net.Client" Version="2.70.0" />

application/CohortManager/src/Functions/ParticipantManagementServices/ManageParticipant/ManageParticipant.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.0.0" />
1717
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.3.0" />
1818
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.0.2" />
19-
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.22.2" />
19+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.23.0" />
2020
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.4" />
2121
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="9.0.2" />
2222
</ItemGroup>

application/CohortManager/src/Functions/Shared/Common/AzureServiceBusClient.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,19 @@ namespace Common;
33
using System.Collections.Concurrent;
44
using System.Text.Json;
55
using Azure.Messaging.ServiceBus;
6+
using Microsoft.Extensions.Azure;
67
using Microsoft.Extensions.Logging;
78

89
public class AzureServiceBusClient : IQueueClient
910
{
10-
private readonly ServiceBusClient _serviceBusClient;
11+
private readonly IAzureClientFactory<ServiceBusSender> _senderFactory;
1112
private readonly ILogger<AzureServiceBusClient> _logger;
12-
1313
private readonly ConcurrentDictionary<string, ServiceBusSender> _senders = new();
1414

15-
public AzureServiceBusClient(string connectionString)
15+
public AzureServiceBusClient(ILogger<AzureServiceBusClient> logger, IAzureClientFactory<ServiceBusSender> senderFactory)
1616
{
17-
_serviceBusClient = new ServiceBusClient(connectionString);
18-
var factory = LoggerFactory.Create(builder =>
19-
{
20-
builder.AddConsole();
21-
});
22-
_logger = factory.CreateLogger<AzureServiceBusClient>();
17+
_logger = logger;
18+
_senderFactory = senderFactory;
2319
}
2420

2521

@@ -33,7 +29,7 @@ public AzureServiceBusClient(string connectionString)
3329
/// <returns></returns>
3430
public async Task<bool> AddAsync<T>(T message, string queueTopicName)
3531
{
36-
var sender = _senders.GetOrAdd(queueTopicName, _serviceBusClient.CreateSender);
32+
var sender = _senders.GetOrAdd(queueTopicName, _senderFactory.CreateClient(queueTopicName));
3733

3834
try
3935
{
@@ -51,4 +47,4 @@ public async Task<bool> AddAsync<T>(T message, string queueTopicName)
5147
return false;
5248
}
5349
}
54-
}
50+
}

application/CohortManager/src/Functions/Shared/Common/Common.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.4.0" />
12-
<PackageReference Include="Azure.Identity" Version="1.13.0" />
12+
<PackageReference Include="Azure.Identity" Version="1.14.1" />
1313
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.20.1" />
1414
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.6.0" />
1515
<PackageReference Include="Azure.Storage.Blobs" Version="12.20.0" />
@@ -20,6 +20,7 @@
2020
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="8.0.7" />
2121
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.0.0" />
2222
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="2.0.0" />
23+
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.12.0" />
2324
<PackageReference Include="Microsoft.Extensions.Options.DataAnnotations" Version="9.0.6" />
2425
<PackageReference Include="RulesEngine" Version="5.0.3" />
2526
<PackageReference Include="Polly" Version="8.5.0" />

application/CohortManager/src/Functions/Shared/Common/Extensions/AzureQueueExtension.cs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
namespace Common;
22

3+
using Azure.Identity;
4+
using Hl7.FhirPath.Expressions;
5+
using Microsoft.Extensions.Azure;
36
using Microsoft.Extensions.DependencyInjection;
47
using Microsoft.Extensions.Hosting;
58

@@ -11,20 +14,29 @@ public static class AzureQueueExtension
1114
/// </summary>
1215
public static IHostBuilder AddAzureQueues(this IHostBuilder hostBuilder, bool UseNewFunctions, string serviceBusConnectionString)
1316
{
14-
return hostBuilder.ConfigureServices(_ =>
15-
{
17+
18+
19+
hostBuilder.ConfigureServices(_ =>
20+
{
1621
if (UseNewFunctions)
1722
{
18-
_.AddSingleton<IQueueClient>(_ => new AzureServiceBusClient(serviceBusConnectionString));
23+
_.AddAzureClients(builder =>
24+
{
25+
builder.AddServiceBusClient(serviceBusConnectionString);
26+
builder.UseCredential(new DefaultAzureCredential());
27+
28+
});
29+
_.AddSingleton<IQueueClient, AzureStorageQueueClient>();
1930
}
2031
else
2132
{
2233
_.AddTransient<IQueueClient, AzureStorageQueueClient>();
2334
_.AddTransient<IQueueClientFactory, QueueClientFactory>();
2435
}
2536
});
26-
}
2737

38+
return hostBuilder;
39+
}
2840
/// <summary>
2941
/// Overload that creates storage queue clients for instances where only storage queues
3042
/// will be used and we do not need control what is injected via config
@@ -37,4 +49,36 @@ public static IHostBuilder AddAzureQueues(this IHostBuilder hostBuilder)
3749
_.AddTransient<IQueueClientFactory, QueueClientFactory>();
3850
});
3951
}
40-
}
52+
/// <summary>
53+
/// Extension method for adding azure queue clients, if UseNewFunctions is set to true, it will inject a service bus queue client,
54+
/// otherwise, it will inject a azure storage queue client
55+
/// This will implement the queue client as a keyed service allowing it to be used in parallel with other queue types
56+
/// </summary>
57+
public static IHostBuilder AddKeyedAzureQueues(this IHostBuilder hostBuilder, bool UseNewFunctions, string serviceBusConnectionString, string keyName)
58+
{
59+
60+
61+
hostBuilder.ConfigureServices(_ =>
62+
{
63+
if (UseNewFunctions)
64+
{
65+
_.AddAzureClients(builder =>
66+
{
67+
builder.AddServiceBusClient(serviceBusConnectionString);
68+
builder.UseCredential(new DefaultAzureCredential());
69+
70+
});
71+
_.AddKeyedSingleton<IQueueClient, AzureStorageQueueClient>(keyName);
72+
}
73+
else
74+
{
75+
_.AddKeyedTransient<IQueueClient, AzureStorageQueueClient>(keyName);
76+
_.AddTransient<IQueueClientFactory, QueueClientFactory>();
77+
}
78+
});
79+
80+
return hostBuilder;
81+
}
82+
83+
84+
}

application/CohortManager/src/Functions/Shared/Common/Extensions/ExceptionHandlerServiceExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public static IHostBuilder AddExceptionHandler(this IHostBuilder hostBuilder)
2121
public static IHostBuilder AddExceptionHandlerWithServiceBus(this IHostBuilder hostBuilder)
2222
{
2323
hostBuilder.AddConfiguration<ServiceBusValidationConfig>(out ServiceBusValidationConfig config);
24+
hostBuilder.AddKeyedAzureQueues(true, config.serviceBusConnectionString, "Exception");
2425
return hostBuilder.ConfigureServices(_ =>
2526
{
2627
_.AddSingleton<IExceptionHandler, ExceptionHandler>();
2728
_.AddTransient<IExceptionSender, SendExceptionToServiceBus>();
28-
_.AddTransient<IQueueClient>(_ => new AzureServiceBusClient(config.serviceBusConnectionString));
2929
});
3030
}
3131

application/CohortManager/src/Functions/Shared/Common/SendExceptionToServicBus.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Common.Interfaces;
33

44
using Common;
5+
using Microsoft.Extensions.DependencyInjection;
56
using Microsoft.Extensions.Logging;
67
using Microsoft.Extensions.Options;
78
using Model;
@@ -14,7 +15,7 @@ public class SendExceptionToServiceBus : IExceptionSender
1415
private readonly ServiceBusValidationConfig _serviceBusValidationConfig;
1516

1617

17-
public SendExceptionToServiceBus(IQueueClient serviceBusHandler, ILogger<SendExceptionToServiceBus> logger, IOptions<ServiceBusValidationConfig> serviceBusValidationConfig)
18+
public SendExceptionToServiceBus([FromKeyedServices("Exception")] IQueueClient serviceBusHandler, ILogger<SendExceptionToServiceBus> logger, IOptions<ServiceBusValidationConfig> serviceBusValidationConfig)
1819
{
1920
_serviceBusHandler = serviceBusHandler;
2021
_logger = logger;
@@ -30,4 +31,4 @@ public async Task<bool> sendToCreateException(ValidationException validationExce
3031
}
3132
return await _serviceBusHandler.AddAsync<ValidationException>(validationException, serviceBusTopicName);
3233
}
33-
}
34+
}

application/CohortManager/src/Functions/Shared/DataServices.Migrations/DataServices.Migrations.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</ItemGroup>
1919

2020
<ItemGroup>
21-
<PackageReference Include="Azure.Identity" Version="1.13.0" />
21+
<PackageReference Include="Azure.Identity" Version="1.14.1" />
2222
</ItemGroup>
2323

2424
<ItemGroup>

0 commit comments

Comments
 (0)