Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set timeout in Refit library

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");
like image 357
Siddharth Eswaramoorthy Avatar asked Apr 10 '17 06:04

Siddharth Eswaramoorthy


People also ask

What is the default timeout for HttpClient C#?

The default value is 100,000 milliseconds (100 seconds).

What is refit library?

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 C#?

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.


1 Answers

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.

like image 111
Bennor McCarthy Avatar answered Sep 22 '22 18:09

Bennor McCarthy