© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
31 replies
camel

Inject http client only for specified service [Answered]

I have a RemoteApiClient that I want to inject to the FooBarService and only to the FooBarService. The reason for this is that the RemoteApiClient is not really meant to be used directly. How do I achieve this? Additionally, the IMemoryCache is injected into the FooBarService, but the IMemoryCache is also used elsewhere.

builder.Services.AddMemoryCache();
builder.Services.AddHttpClient<RemoteApiClient>()
    .AddTransientHttpErrorPolicy(policyBuilder => policyBuilder.WaitAndRetryAsync(Backoff.DecorrelatedJitterBackoffV2(TimeSpan.FromSeconds(1), 5)));

builder.Services.AddScoped<FooBarService>();
builder.Services.AddMemoryCache();
builder.Services.AddHttpClient<RemoteApiClient>()
    .AddTransientHttpErrorPolicy(policyBuilder => policyBuilder.WaitAndRetryAsync(Backoff.DecorrelatedJitterBackoffV2(TimeSpan.FromSeconds(1), 5)));

builder.Services.AddScoped<FooBarService>();
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

SignalR stream data to a specified client? [Answered]
C#CC# / help
4y ago
✅ Blazor / ASP.NET - @inject keyed service
C#CC# / help
2y ago