Ian Warburton
Ian Warburton

Reputation: 15726

Suddenly can't change timeout on HttpClient

I have the following code temporarily changing the timeout on a singleton HttpClient...

Task IncreaseTimeout(Func<Task> action)
{
    var initialTimeout = Client.HttpClient.Timeout;

    try
    {
        Client.HttpClient.Timeout = new TimeSpan(0, 5, 0);

        return action();
    }
    finally
    {
        Client.HttpClient.Timeout = initialTimeout;
    }
}

It works fine on iOS and on Android until now, when it gives the following error when the Timeout property is updated...

This instance has already started one or more requests. Properties can only be modified before sending the first request.

Why has this started happening now and on only one platform?

Upvotes: 0

Views: 1196

Answers (2)

Mukesh Modhvadiya
Mukesh Modhvadiya

Reputation: 2178

It has been designed that way, when you set timeout it checks if httpclient object is disposed or request already been made using the object?

Timeout property should be set before any request made through the client, or it will throw InvalidOperationException with the message quoted in question. Don't change timeout after the first use of HttpClient.

Upvotes: 1

Ian Warburton
Ian Warburton

Reputation: 15726

Looks like this new Android behavior I'm having is actually correct. So I have set my default to one minute instead of thirty seconds and I will hope for the best when uploading files.

Upvotes: 0

Related Questions