-
Notifications
You must be signed in to change notification settings - Fork 227
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
Conversation
|
||
builder.Services.AddHostedService<Worker>(); | ||
|
||
builder.Services.AddTokenAcquisition(isTokenAcquisitionSingleton:true) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thanks
…icrosoft-identity-web into jmprieur/supportWorker
- Adding the configuration code generator and removing the trimming warning that is no longer needed - Fixing OWIN Web.config
are using the code generators
How long before this is available? |
This went out in Feb 2024 version 2.17.0 |
Adding support for Microsoft.NET.Sdk.Worker
This PR:
Fixes:
Related to:
cc: @DavidParks8, @jennyf19