Need help setting timeout for http request - c#

I want to set the timeout value to 5 mins for the http client request call. So I am getting the value for timeout in my app.config as follows and using it to set the client request for 5 mins timeout session. But I am getting error as mentioned below. How do I fix this?
<add key="ClientSrvcTimeout" value="300000" />
I am using the above to set the http client timeout.
client.Timeout = Convert.ToInt32(ConfigurationManager.AppSettings["ClientSrvcTimeout"]);
But I am getting error as "Cannot implicitly convert type string to System.Timespan".
How do I fix this?

The client.Timeout is a TimeSpan and your app setting is an integer serialized to a string. And the integer represents milliseconds.
int milliseconds = int.Parse(ConfigurationManager.AppSettings["ClientSrvcTimeout"]);
client.Timeout = TimeSpan.FromMilliseconds(milliseconds);
I would suggest renaming the app settting to reflect that it is milliseconds, e.g. "ClientSrvcTimeoutMilliseconds".

Related

Timeout exception - c# ( wcf )

While I'm executing the server method asynchronously, getting this Timeout exception continuously.
"Additional information:
This request operation sent to http://schemas.microsoft.com/2005/12/ServiceModel/Addressing/Anonymous
did not receive a reply within the configured timeout (00:01:00).
The time allotted to this operation may have been a portion of a longer timeout.
This may be because the service is still processing the operation or
because the service was unable to send a reply message.
Please consider increasing the operation timeout
(by casting the channel/proxy to IContextChannel and setting the OperationTimeout property)
and ensure that the service is able to connect to the client."
Could someone mention how to increasing the operation timeout
by casting the channel/proxy to IContextChannel and setting the OperationTimeout property ?
This is my existing binding (with client) code.
DuplexChannelFactory<IPortal> datafactory;
NetTcpBinding tcpBinding = new NetTcpBinding();
String sURL = "net.tcp://localhost:8002/MyPortal";
tcpBinding.MaxReceivedMessageSize = System.Int32.MaxValue;
tcpBinding.ReaderQuotas.MaxArrayLength = System.Int32.MaxValue;
datafactory = new DuplexChannelFactory<IPortal>(this,tcpBinding, sURL);
Portal = datafactory.CreateChannel();
If you follow the link in the error (schemas.microsoft.com etc etc) it serves up:
Theresource you are looking for has been removed, had its name
changed, or is temporarily unavailable.
Why are you looking up MS? It sounds like you've got some config data wrong somewhere. If you search your source for that url, what do you find? Does it look sensible?

Issues with C# HttpClient Timeout

I'm making a simple GetAsync() request with an HttpClient object. When I retrieve a small amount of data this way, the request works fine. When I try to retrieve a large amount of data the request times out.
I tried to set the HttpClient's Timeout property to 5 minutes like so:
var client = new HttpClient();
client.Timeout = new TimeSpan(0, 5, 0);
before making the call, but it still times out after ~60 seconds.
I've read in several answers to use HttpWebRequest to access more granular timeout options, but I'm using .net 4.5.2 and references to HttpWebRequest won't compile.
How can I increase the readwrite timeout of the HttpClient?

CloudTableClient client side timeout

When using a CloudTableClient, is there a way to specify a client side timeout?
The TableRequestOptions RetryPolicy and ServerTimeout control the number of retry attempts, the delay between attempts, and the storage service side timeout, but don't seem to cover a client side per-attempt timeout (like the HttpClient.Timeout property).
My concern with relying on the ServerSideTimeout is with delays connecting to the actual server.
When using a CloudTableClient, is there a way to specify a client side timeout?
The MaximumExecutionTime property of TableRequestOptions could help us specific the maximum execution time for all potential retries for the request include the time used by client side and server side.
tableClient.DefaultRequestOptions.MaximumExecutionTime = new TimeSpan(0, 0, 0, 0, 100);
A exception will throw if the request can't be handled in the specific time.
Microsoft.WindowsAzure.Storage.StorageException: 'The client could not finish the operation within specified timeout.'
like the HttpClient.Timeout property
The Timeout property of HttpClient also specific all the execution time before the response came back.
I'm hoping to find a per-retry timeout.
I suggest you use the MaximumExecutionTime. If you need a client side DNS resolve timeout, you could get or set the DnsRefreshTimeout property of ServicePointManager.
ServicePointManager.DnsRefreshTimeout = 4*60*1000; // 4 minutes

ClientBase EndPoint Binding SendTimeout ReceiveTimeout: how to change while debugging

I'm developing a solution with a WCF service and a client that uses the service. Sometimes I'm debugging the service, sometime the client, and sometimes both.
During debugging I get a TimeoutException with additional information
Additional information: The request channel timed out while waiting for a reply after 00:00:59.9950000. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout.
The reason if of course that my server is waiting at a breakpoint instead of answering the question.
During debugging I want longer timeouts, preferably without creating a new configuration for my service client, because if other values of this configuration would change, the changer would have to remember that a special configuration for debugging was created.
I think it is something like:
private IMyServiceInterface CreateServiceChannel()
{
var myServiceClient = new MyServiceClient(); // reads from configuration file
if (Debugger.IsAttached)
{
// Increase timeouts to enable slow debugging
...
}
return (IMyServiceInterface)myServiceClient;
}
According to MSDN Binding.SendTimeout Property is used for something else:
SendTimeout gets or sets the interval of time provided for a write operation to complete before the transport raises an exception.
Therefore I'd rather not change this value if not needed.
Is SendTimeout really the best timeout to increase, or is there something like a TransactionTimeout, the timeout between my question and the receipt of the answer?
How to change the timeout programmatically
The article All WCF timouts explained states that indeed there is something like a transaction timeout: IContextChannel.OperationTimeout
The operation timeout covers the whole service call (sending the request, processing it and receiving a reply). In other words, it defines the maximum time a service call is active from a client’s point of view. If not set, WCF initializes the operation timeout with the configured send timeout.
This explains why the TimeoutException that is thrown advises to change the send timeout.
However, it is possible to change the operation timeout without changing the send timeout:
var myServiceClient = new MyServiceClient(); // reads from configuration file
if (Debugger.IsAttached)
{ // Increase timeouts to enable slow debugging:
IContextChannel contextChannel = (IContextChannel)myServiceClient.InnerChannel;
// InnerChannel is of type IClientChannel, which implements IContextChannel
// set the operation timeout to a long value, for example 3 minutes:
contextChannel.OperationTimeout = TimeSpan.FromMinutes(3);
}
return (IMyInterface)myService;

How do I specify a global HttpWebRequest timeout?

I'm using a twitter library that uses HttpWebRequest internally to make requests to the twitter API. For some odd reason, the requests sometimes take a lot of time to complete (~10 minutes).
The HttpWebRequest object is not exposed by the library.
Is there any way to specify a global timeout and readwritetimeout for the requests, perhaps via app.config?
Unfortunately not currently possible. The constructor of HttpWebRequest has this value hardcoded - reference source.
That timeout is in milliseconds - so 2000ms = only 2 seconds.
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("URL");
req.Timeout = Convert.ToInt32(ConfigurationManager.AppSettings["timeOut"]);
Req.ReadWriteTimeout = Convert.ToInt32(ConfigurationManager.AppSettings["readWriteTimeout "]);
App.config
<appSettings>
<add key="timeOut" value="200" />
<add key="readWriteTimeout " value="10000" />
</appSettings>
Timeout = time spent trying to establish a connection (not including lookup time)
ReadWriteTimeout = time spent trying to read or write data after connection established

Categories