dapr / dapr/dotnet-sdk

`HttpClient` quality of life

Open
#1,484 2 comments 0 reactions 1 assignee Claimed by @WhitWaldo View on GitHub
area/client P1
Dominant language
C#
Stars
1.2k
Forks
378
Avg merge
2d 1h
Merged PRs (30d)
6

Description

_As per some discussions on discord, I'm opening up this issue to track potential priorities for making life with `HttpClient` more enjoyable!_

I've been fumbling around with trying to get the [kiota](https://github.com/microsoft/kiota) library working with `HttpClient` instances provided by dapr. I've observed one pain point that likely could be alleviated with help from either dotnet itself, or the kiota library. And then one item which dapr will likely fix in `1.16`:

1) It's not clear to me how to best obtain the `HttpClient` that I end up providing to kiota
2) It looks like improvements to getting preconfigured `HttpClient` instances [didn't make it into the `1.15` version of the dapr sdk](https://discord.com/channels/778680217417809931/778693130303832135/1349393174312915015). 😅

Item 1 obviously doesn't directly impact the dapr library, although I think it's a kind of scenario that it should be prepared to offer people boilerplate guidance on. It will inevitably come with the territory of offering to produce `HttpClient` instances.

Item 2 I suspect is already in-hand (😜 @WhitWaldo)

---

For the 2nd half of this discussion, I'll just share where I'm at with this little experiment to help inspire any potential quality of life improvements dapr can offer from its end.

In its most naive form, this is what I've made in my `Program.cs`:

```csharp
builder.Services.AddSingleton(
(_) => new());

builder.Services.AddTransient((serviceProvider) =>
{
var authenticationProvider = serviceProvider.GetRequiredService();
var daprClient = serviceProvider.GetRequiredService();

// note: As of and prior to `1.15`, this _could_ cause port exhaustion.
var httpClient = daprClient.CreateInvokableHttpClient();

// note: kiota allows you to provide "adapters" to their generated clients. This is where I think there can be synergies between dapr and kiota!
return new MyGeneratedClient(new HttpClientRequestAdapter(
authenticationProvider,
httpClient: httpClient
));
});
```

There is [some documentation from the kiota project on how to configure dependency injection](https://learn.microsoft.com/en-us/openapi/kiota/tutorials/dotnet-dependency-injection), although I'm questioning whether it's applicable to my scenario. Particularly because I'll be working with an `HttpClient` already from dapr.

If I remember anything else, I'll edit or comment as appropriate.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.