Replies: 4 comments 11 replies
-
Hi Christopher,
When you generate a client, it only depends on Kiota abstractions to build, this abstractions library defines a set of interfaces that need to be implemented so the code can run. If we focus on the http adapter implementation for dotnet, it relies on http client, and can either instantiate one itself or accept one as a parameter.
I hope that clarifies this aspect, let us know if you have further questions. |
Beta Was this translation helpful? Give feedback.
-
it would be nice to have an example how to deal in C# with DI and a Kiota generated client |
Beta Was this translation helpful? Give feedback.
-
When I first started digging into this, I thought it would rather tricky since you get a bit blinded by all the abstractions Kiota adds with numerous builders and providers. It turned out to be a lot simpler than I thought. For our use-case, we handle authentication and other things via delegating handlers that are added to the HttpClient setup. We are only looking for a strongly typed client that wraps an HttpClient that we provide via IHttpClientFactory. This is what I created for my testing; the key is that public interface IExternalClient
{
Task<IEnumerable<Models.WeatherForecast>?> GetForecastAsync();
}
public class ExternalClient : IExternalClient
{
private readonly IAuthenticationProvider _authProvider = new AnonymousAuthenticationProvider();
private readonly TypeClient _client;
public ExternalClient(HttpClient httpClient)
{
var adapter = new HttpClientRequestAdapter(_authProvider, httpClient: httpClient);
_client = new TypeClient(adapter);
}
public async Task<IEnumerable<Models.WeatherForecast>?> GetForecastAsync()
{
return await _client.WeatherForecast.Weather.GetAsync();
}
} Our custom client can then be set up like any other that uses the IHttpClientFactory infrastructure. services
.AddHttpClient("Bob")
.AddTypedClient<IExternalClient>(httpClient => new ExternalClient(httpClient))
.ConfigureHttpClient(httpClient => httpClient.BaseAddress = new Uri("http://something")); |
Beta Was this translation helpful? Give feedback.
-
It's just a name to set up a named HttpClient. Since configuration is done based on name, we can set up things in our program.cs for normal application usage. For tests we use the WebApplicationFactory system; in that context we may want to change the primary message handler or add additional delegating handlers; using deterministic names allows us to do that. |
Beta Was this translation helpful? Give feedback.
-
For years I've been using NSwag for generating typed clients from OpenApi documents. When I discovered Kiota, it became an interesting possibility that needs a bit more investigation. The NSwag code takes in an HttpClient and is only responsible for the actual network serialization/deserialization. Kiota seems to be adding a bunch of other concepts on top such as authentication, retries, etc. There is also a KiotaClientFactory for creating HttpClients.
Microsoft made an investment in the IHttpClientFactory system as part of ASP.NET Core; and I love it. How does that fit in with Kiota? In Kiota it seems that everything is baked into the client. With IHttpClientFactory approach, things are more split. You can create one client and potentially secure it using different mechanisms depending on your environment; full Azure AD in production, something else on local dev box.
Beta Was this translation helpful? Give feedback.
All reactions