Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

623
Visualizações
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");
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

I finally found a way of setting the timeout for a request in Refit. I used CancelationToken. Here is the modified code after adding CancelationToken

Interface:

interface IDevice
{
  [Get("/app/device/{id}")]
  Task<Device> GetDevice(string id, [Header("Authorization")] string authorization, CancellationToken cancellationToken);
}

Invoking the API:

var device = RestService.For<IDevice>("http://localhost");    
CancellationTokenSource tokenSource = new CancellationTokenSource();
tokenSource.CancelAfter(10000); // 10000 ms
CancellationToken token = tokenSource.Token;          
var dev = await device.GetDevice("15e2a691-06df-4741-b26e-87e1eecc6bd7", "Bearer OAUTH_TOKEN", token);

It works properly for me. I don't know whether it is the proper way or not. If it is a wrong, kindly suggest the correct way.

over 4 years ago · Santiago Trujillo Relatório

0

Another solution: One of the tests in Refit uses this method. Add System.Reactive.Linq in nuget. Then in the interface specification:

interface IDevice
{
    [Get("/app/device/{id}")]
    IObservable<Device> GetDevice(string id, [Header("Authorization")] string authorization);
}

And in the API:

try
{
  await device.GetDevice("your_parameters_here").Timeout(TimeSpan.FromSeconds(10));
}
catch(System.TimeoutException e)
{
  Console.WriteLine("Timeout: " + e.Message);
}

+1 solution from here:

Create an extension method for your tasks:

public static class TaskExtensions
{
    public static async Task<TResult> TimeoutAfter<TResult>(this Task<TResult> task, TimeSpan timeout)
    {

        using (var timeoutCancellationTokenSource = new CancellationTokenSource())
        {

            var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token));
            if (completedTask == task)
            {
                timeoutCancellationTokenSource.Cancel();
                return await task;  // Very important in order to propagate exceptions
            }
            else
            {
                throw new TimeoutException("The operation has timed out.");
            }
        }
    }
}

Interface can be left using Task<Device> return value. In the API:

try
{
  await _server.ListGasLines().TimeoutAfter(TimeSpan.FromSeconds(10));
}
catch(System.TimeoutException e)
{
  Console.WriteLine("Timeout: " + e.Message);
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda