dapr / dapr/dotnet-sdk

Injecting or passing in HttpClient

Open
#756 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
1.2k
Forks
378
Avg merge
1d 22h
Merged PRs (30d)
5

Description

It would help testing if I could pass in HttpClient or IHttpClientFactory into DaprClientBuilder.

I can see an internal HttpClient Factory used for internal testing:

https://github.com/dapr/dotnet-sdk/blob/master/src/Dapr.Client/DaprClientBuilder.cs#L70
```
internal DaprClientBuilder UseHttpClientFactory(Func factory)
{
this.HttpClientFactory = factory;
return this;
}
```
Which then is used here:
https://github.com/dapr/dotnet-sdk/blob/master/src/Dapr.Client/DaprClientBuilder.cs#L158

And that there is an extension in the ASP.NET Core project:
https://github.com/dapr/dotnet-sdk/blob/master/src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs#L39

```
services.AddSingleton(_ =>
{
var builder = new DaprClientBuilder();
if (configure != null)
{
configure.Invoke(builder);
}

return builder.Build();
});
```

Can there be constructor that takes in an HttpClient? Or an IHttpClientFactory?

With that I can wire up my test cases to use test handlers to ensure the responses I want out.

I know that I can wrap Dapr in an interface and inject that.

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.