Skip to content

Timeout

Stanislav Molchanovskiy edited this page Feb 16, 2022 · 3 revisions

Compile-time configuration

By default, the client uses a timeout equal to 100 seconds. To change it, you can use the Timeout attribute:

[Timeout(milliseconds: 1000)]
public interface IMyClient 
{
    void Do([QueryParam] int id);   // Uses the 1000ms timeout

    [GetMethod, Timeout(milliseconds: 2000)]
    void Do([QueryParam] int id);   // Uses the 2000ms timeout
}

Note that the method attribute overrides the interface attribute.

Run-time configuration

To change the timeout at runtime, you should use the client builder. It allows you to set a common timeout for all requests. To do this, you will need the WithTimeout method:

IMyClient myClient = NClientGallery.Clients.GetRest()
    .For<IMyClient>(host: "http://localhost:8080")
    .WithTimeout(TimeSpan.FromMinutes(1))
    .Build();

The runtime configuration can override the compile-time configuration.
If you want to set a timeout for a specific request at run-time, you need to use a cancellation token (see cancellation section). Note that in this case, the waiting time using the cancellation token cannot exceed the specified or default timeout.