1. Use HttpWebRequest instead of WebClient and set its timeout by ReadWriteTimeout property. Of course the usage of this class is more complex than WebClisnt's one but it gives you more flexibility.
2. Make a derived class (wrapper) , which willset the timeout propery of the base class of the WebClient.
public class MyWebClient: WebClient
{
//time in milliseconds
private int timeout;
public int Timeout
{
get {
return timeout;
}
set {
timeout = value;
}
}
publicMyWebClient
(
)
{
this.timeout
=
60000
;
}public
MyWebClient
(int
timeout
)
{
this.timeout
=
timeout
;
}
protected override WebRequest GetWebRequest(Uri address)
{
var result = base.GetWebRequest(address);
result.Timeout = this.timeout;
return result;
}
}
The usage is pretty much the same as the WebClient's usage but we have to specify the new timeout in the constructor or by setting the Timeout property:
MyWebClient
wClient = new
MyWebClient
(1800000);
UTF8Encoding objUTF8 = new UTF8Encoding();
try
{
byte[] data =wClient
.DownloadData(url);
File.WriteAllBytes(
System.AppDomain.CurrentDomain.BaseDirectory + @"\files\output.xls", data);
wClient
= null;
objUTF8 = null;
}
catch (Exception ex)
{
objWebClient = null;
objUTF8 = null;
}
As you see this timeout property solves the problems by client side. The server response should also have longer timeout for generating the response. It is set by machine.config or web.config - httpRuntime section. Note that the timeout in web.config is specified in seconds.
thank you! thank you! thank you!
ReplyDeleteIn case someone is in need of the VB.NET version, here it goes :
-----------------------------------
Imports System.Net
Public Class MyWebClient
Inherits WebClient
Private _timeout As Integer
Public Property timeout As Integer
Get
timeout = _timeout
End Get
Set(ByVal value As Integer)
_timeout = value
End Set
End Property
Public Sub MyWebClient()
Me.timeout = 60000
End Sub
Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As System.Net.WebRequest
Dim result = MyBase.GetWebRequest(address)
result.Timeout = Me._timeout
Return result
End Function
End Class
-----------------------------
In order to set the timeout I used the following:
Dim objWebClient As MyWebClient = New MyWebClient()
... ... ...
objWebClient.timeout = 3600000
objWebClient.DownloadFile(ObjUri, TargetFile)
The class MyWebClient doesn't work. I've disconnected my lan cable to see whether or not a timeout occurs and nothing happend.
ReplyDeleteIf timeout beound is reached you should get the respective exception. You if you haven't done any code changes and disconnect from network you should see that exception after two minutes.
ReplyDeletePlease check your request timeout setting in web config and make sure it is longer than the webrequest timeout. Otherwise it you will rather see a general request timeout exception.
system.web/httpRuntime -> executionTimeout="90"
the setting define the page request timeout in seconds. I think the default value is 90 seconds.
Hope this helps.
Not work
ReplyDeleteYou will need to also increase the request execution timeout in web.config
DeleteEx.
The execution timout must be bigger than the WebClient request timeout.
<httpRuntime executionTimeout="600" >
Deletethe tag was not properly shown in my previous commnet
Thank you, this was useful.
ReplyDeletep.s. You don't need to define the Timeout property of MyWebClient. You are not using it. The lowercase timeout is enough.
ReplyDeleteMaxed Out settings, but still not long enough for long processing
You will need to also increase the request execution timeout in web.config
DeleteEx.
The execution timout must be bigger than the WebClient request timeout.
thanks
ReplyDelete