8.16Using in Non-Dependency-Injection Environments (Console/WinForms/WPF)
In applications such as ASP.NET Core or Worker Service, dependency injection support is usually built in. You only need to register the required services via services or builder.Services in the Startup.cs or Program.cs file.
However, in certain specific scenarios — for example console applications (Console), WinForms, or WPF projects — .NET does not integrate a complete dependency injection container by default. For these scenarios, you can use the Service property of the HttpRemoteClient static class to send remote HTTP requests:
var result = await HttpRemoteClient.Service.GetAsStringAsync("https://furion.net/");Custom Configuration of the HTTP Remote Request Service
If you need to customize the configuration of the HTTP remote request service, you can do so by calling the HttpRemoteClient.Configure(services => {}) method:
HttpRemoteClient.Configure(services =>{ // Example: configure the default HttpClient services.AddHttpClient(string.Empty).AddProfilerDelegatingHandler(); // To customize the HTTP remote request service, configure it by calling AddHttpRemote(); by default, no registration is required ⚠️ // services.AddHttpRemote();});After completing the custom configuration, the HttpRemoteClient.Service static property automatically uses the instance built from the latest service build information.
Sharing the Instance with the Application's Dependency Injection Container
Although HttpRemoteClient is primarily intended for non-dependency-injection scenarios, it also allows injecting the application's own root service container so that it shares the same IHttpRemoteService instance with the existing dependency injection system. This works in any application where an IServiceProvider has already been built (including Console, WinForms, WPF, Worker Service, and ASP.NET Core).
In Generic Host, Worker Service, or Console Applications
In scenarios where an IHost or IServiceProvider has already been built, simply call the UseHttpRemoteClient extension method to inject the root container:
// Using Worker Service as an examplevar builder = Host.CreateApplicationBuilder(args);builder.Services.AddHttpRemote();var host = builder.Build();// Inject the root containerhost.Services.UseHttpRemoteClient();var result = await HttpRemoteClient.Service.GetAsStringAsync("https://furion.net/");In ASP.NET Core Applications
A dedicated UseHttpRemoteClient extension method is provided, which can be injected in a single line in Program.cs:
var builder = WebApplication.CreateBuilder(args);builder.Services.AddHttpRemote();var app = builder.Build().UseHttpRemoteClient(); // Inject the root container into HttpRemoteClientapp.Run();From then on, HttpRemoteClient.Service is exactly the same instance returned by app.Services.GetRequiredService<IHttpRemoteService>(), and no separate internal container is maintained anymore.
In addition to using the Service property provided by the HttpRemoteClient static class, you can also choose to manually build a service container and resolve an IHttpRemoteService instance from it, as shown below:
// Create a new ServiceCollection instancevar services = new ServiceCollection();// Add the required HTTP remote request service to the ServiceCollectionservices.AddHttpRemote();// Build the ServiceProvider instanceusing var provider = services.BuildServiceProvider();// Get the IHttpRemoteService instance from the ServiceProvidervar httpRemoteService = provider.GetRequiredService<IHttpRemoteService>();This approach is suitable for scenarios where you need full control over the dependency injection container's lifetime. If you want HttpRemoteClient.Service to also share the service instance with the manually built container, use provider.UseHttpRemoteClient() to inject that container instead of creating them separately.