microsoft / azure-devops-dotnet-samples

.NET/C# samples for integrating with Azure DevOps Services and Azure DevOps Server
https://docs.microsoft.com/azure/devops/integrate
MIT License
519 stars 511 forks source link

HttpFactory and AddHttpClient in dependency injection #296

Open yonail opened 3 years ago

yonail commented 3 years ago

Hello!

I'm trying to figure out how I can benefit from the new .netcore HttpFactory dependency injection to register the VSS clients in my startup class like so:

services.AddHttpClient<WorkItemTrackingHttpClient>(client =>
{
    // custom configuration
});

and then in a service/controller method:

public async Task Method([FromServices] WorkItemTrackingHttpClient client, int id)
{
    var workitem = await client.GetWorkItemAsync(id);
    //do something with the workitem...
}

Currently, I'm facing this exception when doing the above: InvalidOperationException: A suitable constructor for type 'Microsoft.TeamFoundation.WorkItemTracking.WebApi.WorkItemTrackingHttpClient' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

Has any one managed to do it? Would be good to have a sample for this.

Thanks, Yoni