gpt4 book ai didi

c# - 如何在 Microsoft Graph 中使用 DI

转载 作者:行者123 更新时间:2023-12-04 12:23:02 27 4
gpt4 key购买 nike

我有一个 .net web api 核心项目,我将调用 microsoft graph
所以我创建了一个配置类:

public class GraphConfiguration
{
public static void Configure(IServiceCollection services, IConfiguration configuration)
{
//Look at appsettings.Development.json | https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.1
var graphConfig = new AppSettingsSection();
configuration.GetSection("AzureAD").Bind(graphConfig);

IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(graphConfig.ClientId)
.WithTenantId(graphConfig.TenantId)
.WithClientSecret(graphConfig.ClientSecret)
.Build();

ClientCredentialProvider authenticationProvider = new ClientCredentialProvider(confidentialClientApplication);
GraphServiceClient graphServiceClient = new GraphServiceClient(authenticationProvider);

}
}
在我的 Controller 中,我有这个:
 public class UserController : ControllerBase
{
private TelemetryClient telemetry;
private readonly ICosmosStore<Partner> _partnerCosmosStore;
private readonly GraphServiceClient _graphServiceClient;


// Use constructor injection to get a TelemetryClient instance.
public UserController(TelemetryClient telemetry,ICosmosStore<Partner> partnerCosmosStore, GraphServiceClient graphServiceClient)
{
this.telemetry = telemetry;
_partnerCosmosStore = partnerCosmosStore;
_graphServiceClient = graphServiceClient;
}

/// <summary>
/// Gets all partners
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<ActionResult> GetUsers()
{
this.telemetry.TrackEvent("GetPartners");

try
{
var me = await _graphServiceClient.Me.Request().WithForceRefresh(true).GetAsync();
return Ok(me);
}
catch (Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
telemetry.TrackException(ex, dt);
return BadRequest("Error Lulo: " + guid);
}
}
}
但我想我在 Startupcs 中缺少了一步。我如何在所有 Controller 中实际注入(inject)它?

最佳答案

我建议使用和注册抽象 IGraphServiceClient使用 DI 容器

public static class GraphConfiguration {
//Called in Startup - services.ConfigureGraphComponent(configuration)
public static IServiceCollection ConfigureGraphComponent(IServiceCollection services, IConfiguration configuration) {
//Look at appsettings.Development.json | https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.1

AppSettingsSection graphConfig = configuration.GetSection("AzureAD").Get<AppSettingsSection>();

IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(graphConfig.ClientId)
.WithTenantId(graphConfig.TenantId)
.WithClientSecret(graphConfig.ClientSecret)
.Build();

ClientCredentialProvider authenticationProvider = new ClientCredentialProvider(confidentialClientApplication);

services.AddScoped<IGraphServiceClient, GraphServiceClient>(sp => {
return new GraphServiceClient(authenticationProvider);
});

return services;
}
}
并取决于您的家属。
public class UserController : ControllerBase {
private TelemetryClient telemetry;
private readonly ICosmosStore<Partner> _partnerCosmosStore;
private readonly IGraphServiceClient _graphServiceClient;


// Use constructor injection to get a TelemetryClient instance.
public UserController(TelemetryClient telemetry,ICosmosStore<Partner> partnerCosmosStore, IGraphServiceClient graphServiceClient)
{
this.telemetry = telemetry;
_partnerCosmosStore = partnerCosmosStore;
_graphServiceClient = graphServiceClient;
}

//...
这样容器将知道如何在解析 Controller 时注入(inject)所需的依赖项

关于c# - 如何在 Microsoft Graph 中使用 DI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66530370/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com