Skip to content

Commit 3553df5

Browse files
authored
Allow null broker in lazy authorization service client (#363)
1 parent 88ad777 commit 3553df5

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/Microsoft.ServiceHub.Framework/ServiceHub/LazyAuthorizationServiceProxy.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ internal class LazyAuthorizationServiceProxy : IAuthorizationService, IDisposabl
1919
/// </summary>
2020
/// <param name="serviceBroker">A service broker used to acquire the activation service.</param>
2121
/// <param name="joinableTaskFactory">An optional <see cref="JoinableTaskFactory"/> to use when scheduling async work, to avoid deadlocks in an application with a main thread.</param>
22-
public LazyAuthorizationServiceProxy(IServiceBroker serviceBroker, JoinableTaskFactory? joinableTaskFactory)
22+
public LazyAuthorizationServiceProxy(IServiceBroker? serviceBroker, JoinableTaskFactory? joinableTaskFactory)
2323
{
24-
Requires.NotNull(serviceBroker);
2524
this.authorizationService = new(() => this.ActivateAsync(serviceBroker), joinableTaskFactory);
2625
}
2726

@@ -58,10 +57,14 @@ public async ValueTask<IReadOnlyDictionary<string, string>> GetCredentialsAsync(
5857
return await service.GetCredentialsAsync(cancellationToken).ConfigureAwait(false);
5958
}
6059

61-
private async Task<IAuthorizationService> ActivateAsync(IServiceBroker serviceBroker)
60+
private async Task<IAuthorizationService> ActivateAsync(IServiceBroker? serviceBroker)
6261
{
6362
this.DisposeToken.ThrowIfCancellationRequested();
64-
IAuthorizationService? authService = await serviceBroker.GetProxyAsync<IAuthorizationService>(FrameworkServices.Authorization, this.DisposeToken).ConfigureAwait(false);
63+
64+
IAuthorizationService? authService =
65+
serviceBroker is not null
66+
? await serviceBroker.GetProxyAsync<IAuthorizationService>(FrameworkServices.Authorization, this.DisposeToken).ConfigureAwait(false)
67+
: null;
6568

6669
authService ??= new DefaultAuthorizationService();
6770
authService.CredentialsChanged += this.AuthService_CredentialsChanged;

test/Microsoft.ServiceHub.Framework.Tests/LazyAuthorizationServiceProxyTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ public LazyAuthorizationServiceProxyTests(ITestOutputHelper logger)
1212
{
1313
}
1414

15+
[Fact]
16+
public async Task CanActivateWithNullBroker()
17+
{
18+
using LazyAuthorizationServiceProxy proxy = new(serviceBroker: null, joinableTaskFactory: null);
19+
Assert.False(await proxy.CheckAuthorizationAsync(new ProtectedOperation("operation"), this.TimeoutToken));
20+
}
21+
1522
[Fact]
1623
public async Task ActivatedAuthorizationServiceProxyIsDisposed()
1724
{

0 commit comments

Comments
 (0)