I have class HttpClientObject that create object of HttpClient
public class HttpClientObject{
private readonly HttpClient client;
public HttpClientObject(CustomParameters parameter)
{
handler=new HttpClientHandler(){}
client = new HttpClient(handler) { Timeout=parameter.timeout };
}
public HttpClient GetClient()
{
return client;
}
}
I have another class to fetch data
public class student{
HttpClientObject httpClientObject {get;set;}
HttpClient client = httpClientObject .GetClient();
//For X condition true it will timeout period 1 else by default
if(x)
{
client .Timeout = TimeSpan.FromSeconds(1);
}
var result = client .GetAsync("url").GetAwaiter().GetResult();
}
But I am getting error - "This instance has already started one or more requests. Properties can only be modified".
Getting error because of changing the Timeout and throws InvalidOperationException .
Is there any safe solution ?