Skip to content

Commit 16bf7a8

Browse files
committed
Created extension method to inject GraphServiceClient
1 parent de615ff commit 16bf7a8

File tree

4 files changed

+46
-22
lines changed

4 files changed

+46
-22
lines changed

Service/GroupMembershipManagement/Common.DependencyInjection/Common.DependencyInjection.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<ItemGroup>
88
<PackageReference Include="Azure.Identity" Version="1.11.0" />
99
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.3.0" />
10+
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
11+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
12+
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
13+
<PackageReference Include="Microsoft.Graph" Version="5.24.0" />
1014
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
1115
<PackageReference Include="System.Text.Encodings.Web" Version="6.0.0" />
1216
</ItemGroup>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Options;
6+
using Microsoft.Graph;
7+
using System;
8+
9+
namespace Common.DependencyInjection
10+
{
11+
public static class ServiceCollectionExtensions
12+
{
13+
public static IServiceCollection AddGraphAPIClient(this IServiceCollection services)
14+
{
15+
services.AddSingleton((services) =>
16+
{
17+
var configuration = services.GetService<IConfiguration>();
18+
var graphCredentials = services.GetService<IOptions<GraphCredentials>>().Value;
19+
var authenticationType = MapStringToAuthenticationType(configuration["GraphAPI:AuthenticationType"]);
20+
var credential = FunctionAppDI.CreateAuthenticationProvider(graphCredentials, authenticationType);
21+
return new GraphServiceClient(credential);
22+
});
23+
24+
return services;
25+
}
26+
27+
public static AuthenticationType MapStringToAuthenticationType(string input)
28+
{
29+
if (Enum.TryParse(typeof(AuthenticationType), input, true, out object result))
30+
{
31+
return (AuthenticationType)result;
32+
}
33+
else
34+
{
35+
return AuthenticationType.Unknown;
36+
}
37+
}
38+
}
39+
}
40+

Service/GroupMembershipManagement/Hosts.FunctionBase/CommonStartup.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,5 @@ public string GetValueOrDefault(string key, [CallerFilePath] string callerFile =
233233
{
234234
return Environment.GetEnvironmentVariable(key, EnvironmentVariableTarget.Process) ?? string.Empty;
235235
}
236-
237-
public AuthenticationType MapStringToAuthenticationType(string input)
238-
{
239-
if (Enum.TryParse(typeof(AuthenticationType), input, true, out object result))
240-
{
241-
return (AuthenticationType)result;
242-
}
243-
else
244-
{
245-
return AuthenticationType.Unknown;
246-
}
247-
}
248-
249236
}
250237
}

Service/GroupMembershipManagement/Hosts/JobTrigger/Function/Startup.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,8 @@ public override void Configure(IFunctionsHostBuilder builder)
5252
return new KeyVaultSecret<IJobTriggerService, Guid>(Guid.Parse(configuration["teamsChannelServiceAccountObjectId"]));
5353
});
5454

55-
builder.Services.AddSingleton((services) =>
56-
{
57-
var configuration = services.GetService<IConfiguration>();
58-
var graphCredentials = services.GetService<IOptions<GraphCredentials>>().Value;
59-
var authenticationType = MapStringToAuthenticationType(configuration["GraphAPI:AuthenticationType"]);
60-
var credential = FunctionAppDI.CreateAuthenticationProvider(graphCredentials, authenticationType);
61-
return new GraphServiceClient(credential);
62-
})
63-
.AddScoped<IGraphGroupRepository, GraphGroupRepository>();
55+
builder.Services.AddGraphAPIClient();
56+
builder.Services.AddScoped<IGraphGroupRepository, GraphGroupRepository>();
6457

6558
builder.Services.AddTransient<ITeamsChannelRepository, TeamsChannelRepository>((services) =>
6659
{

0 commit comments

Comments
 (0)