question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add an extension method to add Dapr HTTP Client to service collection

See original GitHub issue

Describe the proposal

Currently, we can call the CreateInvokeHttpClient from DaprClient to create a preconfigured HttpClient. It would be great to leverage the IHttpClientFactory of ASP.NET Core with the same level of integration as the CreateInvokeHttpClient.

In short, there’s how it could be used :

services.AddDaprHttpClient<CartClient, CartClient>("appId");

I’ve created a PR to see how it could be implemented.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
jkearscommented, May 19, 2022

@rynowak Actually I got it to work based upon your example…

namespace NextWare.Domain.Gateway.DependencyInjection
{
    public static class EndPointConfiguration
    {
        public static IServiceCollection AddEndPoints(this IServiceCollection services, ConfigurationManager configuration)
        {

            // Non-Dapr
            //services.AddHttpClient(WellKnownSchemaNames.OrderServices, c => c.BaseAddress = new Uri(configuration.GetConnectionString("OrderServices"))).AddHeaderPropagation();
            //services.AddHttpClient(WellKnownSchemaNames.InventoryServices, c => c.BaseAddress = new Uri(configuration.GetConnectionString("InventoryServices"))).AddHeaderPropagation();

            // With-Dapr
            services.AddHttpClient(WellKnownSchemaNames.OrderServices, c =>
            {
                c.BaseAddress = new Uri($"http://{WellKnownSchemaNames.OrderServices}");
            }).AddHttpMessageHandler(() => new InvocationHandler()).AddHeaderPropagation();

            services.AddHttpClient(WellKnownSchemaNames.InventoryServices, c =>
            {
                c.BaseAddress = new Uri($"http://{WellKnownSchemaNames.InventoryServices}");
            }).AddHttpMessageHandler(() => new InvocationHandler()).AddHeaderPropagation();
 
            return services;
        }
    }
}

Very simple so thank you!

1reaction
karoldelandcommented, Mar 15, 2021

Hi @rynowak, I have to agree with you. I realized yesterday while implementing the basic method that it will imply to have a lot of code to support all overloads.

For me, the need was mainly a concern of adopting the best-practices for ASP.NET about the IHttpClientFactory. It will be useful to have the possibility to add global policies like retries, but it seems that Dapr will solve this early soon. So I think it’s not a good idea to add code in the SDK that have high chance to be useless in the short term.

IMHO, the second sample you provide is nice. I personaly don’t like to have http:// strings in the code.

For sure, adding this to documentation would be a great addition.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How-To: Invoke services using HTTP | Dapr Docs
To invoke an application using Dapr, you can use the invoke API on any Dapr instance. The sidecar programming model encourages each application...
Read more >
DaprServiceCollectionExtensions.cs
Search code, repositories, users, issues, pull requests... · Provide feedback · Saved searches · DaprServiceCollectionExtensions.cs ...
Read more >
Getting started with the Dapr client .NET SDK
How to get up and running with the Dapr .NET SDK. ... Http.HttpClient to invoke your services. SDK; HTTP. using var client =...
Read more >
DaprClient usage
A DaprClient can be configured by invoking methods on DaprClientBuilder class before calling .Build() to create the client. The settings for each DaprClient...
Read more >
Building Microservices by Using Dapr and .NET with ...
Run("http://localhost:5000");. Here, we have included Dapr in the service collection by using the AddDaprClient extension method.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found