Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for Microsoft.NET.Sdk.Worker #2645

Merged
merged 9 commits into from Jan 28, 2024
Merged

Conversation

jmprieur
Copy link
Collaborator

@jmprieur jmprieur commented Jan 18, 2024

Adding support for Microsoft.NET.Sdk.Worker

This PR:

  • Fixes an issue with Downstream API. The service could be registered several times and systematically as a scoped service, independently of the token acquisition.
  • Adds a worker sample using IDownstreamApi.

Fixes:

Related to:

cc: @DavidParks8, @jennyf19


builder.Services.AddHostedService<Worker>();

builder.Services.AddTokenAcquisition(isTokenAcquisitionSingleton:true)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could introduce a:
TokenAcquirerFactory.GetDefault(hostBuilder) adds the token acquisition, the cache and sets the MicrosoftIdentityOptions.

Note that the AddTokenAcquisition in this case uses an Sdk host, which means it's not drawing ASP.NET Core services.

private readonly ILogger<Worker> _logger;
private readonly IDownstreamApi _downstreamApi;

public Worker(ILogger<Worker> logger, IDownstreamApi downstreamApi)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just inject the IDownstreamApi or GraphServiceClient etc ...

if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
var result = await _downstreamApi.CallApiAsync("MyWebApi");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and use it

},

"MyWebApi": {
"BaseUrl": "https://graph.microsoft.com/v1.0",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used Microsoft Graph as a well-known service in this sample.

return services;
}

private static void RegisterDownstreamApi(IServiceCollection services)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it not possible to unit test this? Seems like you just need to register a few mock ITokenAcquisition / IDownstream APIs with different lifetimes, get them from the service collection and assert on their lifetime.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks

@jmprieur jmprieur merged commit 59ce6ad into master Jan 28, 2024
4 checks passed
@jmprieur jmprieur deleted the jmprieur/supportWorker branch January 28, 2024 02:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants