Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark Timeout Property as Obsolete and Recommend IHttpClientFactory for Timeout Config #3135

Merged
merged 3 commits into from
Nov 29, 2024

Conversation

WanjohiSammy
Copy link
Contributor

@WanjohiSammy WanjohiSammy commented Nov 27, 2024

Issues

This pull request fixes #3131.

Description

Summary

This PR marks the Timeout property in the DataServiceContext class as obsolete and recommends using IHttpClientFactory to configure the timeout. This change is part of our effort to modernize the configuration of HTTP requests and provide more flexible and robust options for users.

Changes

  • Added the [Obsolete] attribute to the Timeout property with a message indicating the deprecation and suggesting the use of IHttpClientFactory.

Reason for Change

The Timeout property is being deprecated to encourage the use of IHttpClientFactory, which provides a more flexible and configurable way to manage HTTP client settings, including timeouts. This approach aligns with modern .NET practices and offers better integration with dependency injection and other configuration mechanisms.

Impact

Users will see a compiler warning when using the Timeout property, indicating that it is obsolete and suggesting the use of IHttpClientFactory instead. This change does not break existing code but encourages users to migrate to the new approach.

Next Steps

  • Update any relevant documentation to guide users on how to configure timeouts using IHttpClientFactory. Documentation PR
  • Plan for the eventual removal of the Timeout property in a future major release.

Example Usage of IHttpClientFactory

  1. Create CustomHttpClientFactory
public class CustomHttpClientFactory : IHttpClientFactory
{
    private readonly HttpClient _httpClient;

    public CustomHttpClientFactory(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }

    public HttpClient CreateClient(string name)
    {
        return _httpClient;
    }
}
  1. Create HttpClient and specify the Timeout
var httpClient = new HttpClient
{
    Timeout = TimeSpan.FromSeconds(160)
};

var httpClientFactory= new CustomHttpClientFactory(httpClient);

var context = new Container(new Uri("https://localhost:7214/odata"))
{
    HttpClientFactory = httpClientFactory
};
  1. Using DI:
var services = new ServiceCollection();
services.AddHttpClient("", client =>  // Note that the httpClient is not named
{
    client.Timeout = TimeSpan.FromSeconds(160);
});

var serviceProvider = services.BuildServiceProvider();
var httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();

var context = new Container(new Uri("https://localhost:7214/odata"))
{
    HttpClientFactory = httpClientFactory
};

Checklist (Uncheck if it is not completed)

  • Test cases added
  • Build and test with one-click build and test script passed

Additional work necessary

Using IHttpClientFactory with Microsoft.OData.Client

@WanjohiSammy WanjohiSammy requested a review from habbes November 27, 2024 11:55
@WanjohiSammy WanjohiSammy merged commit 9ce546b into main Nov 29, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Possible inconvenient implementation of timeout in OData.Client
2 participants