I am using Refit library in my Xamarin App, I want to set 10 seconds timeout for the request. Is there any way to do this in refit?
Interface:
interface IDevice
{
[Get("/app/device/{id}")]
Task<Device> GetDevice(string id, [Header("Authorization")] string authorization);
}
Invoking the API
var device = RestService.For<IDevice>("http://localhost");
var dev = await device.GetDevice("15e2a691-06df-4741-b26e-87e1eecc6bd7", "Bearer OAUTH_TOKEN");
The default value is 100,000 milliseconds (100 seconds).
Refit is a type-safe REST Client for . NET Core, Xamarin and . Net - developed by Paul Betts. It is inspired by Square's Retrofit library. Refit makes it relatively easy to make calls to REST API, without writing much of wrapping code.
What is Refit? The Refit library for C# provides us with a type-safe wrapper for interacting with HTTP-based APIs. Instead of using HttpClient , which is provided for us by ASP.NET Core, we can define an interface that represents the API we want to interact with.
The accepted answer is the correct way to enforce a timeout for a single request, but if you want to have a single consistent timeout value for all requests, you can pass a preconfigured HttpClient
with its Timeout
property set:
var api = RestService.For<IDevice>(new HttpClient
{
BaseAddress = new Uri("http://localhost"),
Timeout = TimeSpan.FromSeconds(10)
});
Here is an example project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With