Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To set network timeouts in C#, create a custom MessageHandler, set the timeout on the request inside Invoke(), and insert the handler into INetworkService.MessageHandlers before opening or converting HTML.
A network timeout is the amount of time a client waits for a network operation to complete, such as establishing a connection, sending a request, or waiting for a response. Timeout values prevent an application from waiting indefinitely when a server is unavailable, slow, or unresponsive.
In HTML processing, timeouts matter because an HTML document can reference remote images, stylesheets, scripts, fonts, or other resources. If one of those resources takes too long to respond, opening or converting the document can become slow. Setting a timeout helps make server-side processing more predictable, although the document may be loaded or rendered without resources that did not respond in time.
To set a maximum network operation timeout, define your own network request handler and register it at the top of the handler queue. Then all network requests pass through it before continuing through the pipeline.
The following example creates a TimeoutMessageHandler that sets the maximum network timeout to 1 second and passes the message further down the chain.
To create the timeout handler:
TimeoutMessageHandler class derived from
MessageHandler.Next() to continue the message handler pipeline. 1// Set custom timeout for network requests in .NET HTML processing
2
3// Define the TimeoutMessageHandler class that is derived from the MessageHandler class
4public class TimeoutMessageHandler : MessageHandler
5{
6 public override void Invoke(INetworkOperationContext context)
7 {
8 context.Request.Timeout = TimeSpan.FromSeconds(1);
9 Next(context);
10 }
11}In the C# code snippet above, TimeoutMessageHandler inherits from
MessageHandler and overrides
Invoke(). Inside Invoke(), a timeout of 1 second is set for the request in the
INetworkOperationContext object. The Next() method then continues pipeline execution.
When making network requests, a network timeout is a crucial aspect to consider. HTML documents may include resources that are in the cloud or another server. Sometimes requests to a remote resource take a very long time or do not respond, then opening a document can take an infinitely long time. If you set an operation timeout, you will avoid long waits, but the document may open without some “problematic” resources.
To set a timeout when opening an HTML file:
TimeoutMessageHandler instance.MessageHandlers. 1// Set custom request timeout using a message handler
2
3// Create an instance of the Configuration class
4using Configuration configuration = new Configuration();
5
6// Call the INetworkService which contains the functionality for managing network operations
7INetworkService network = configuration.GetService<INetworkService>();
8
9// Add the TimeoutMessageHandler to the top of existing message handler chain
10network.MessageHandlers.Insert(0, new TimeoutMessageHandler());
11
12// Prepare path to a source document file
13string documentPath = Path.Combine(DataDir, "document.html");
14
15// Create an HTML document with a custom configuration
16using HTMLDocument document = new HTMLDocument(documentPath, configuration);In this example, we create an instance of the TimeoutMessageHandler class and insert it at the top of the list of message handlers in the network service. Finally, we create an instance of the
HTMLDocument class, passing in the path to the HTML file and the configuration object. The HTMLDocument class will use the network service from the configuration object to make the necessary network requests.
The same handler can be used during conversion. The following C# example sets a timeout value of 1 second for requests made through the TimeoutMessageHandler class. As a result, network operations that occur during conversion and last more than one second are interrupted.
To set a timeout for HTML conversion:
TimeoutMessageHandler. 1// Set request timeout when converting HTML to PDF
2
3// Create an instance of the Configuration class
4using Configuration configuration = new Configuration();
5
6// Call the INetworkService which contains the functionality for managing network operations
7INetworkService network = configuration.GetService<INetworkService>();
8
9// Add the TimeoutMessageHandler to the top of existing message handler chain
10network.MessageHandlers.Insert(0, new TimeoutMessageHandler());
11
12// Prepare path to a source document file
13string documentPath = Path.Combine(DataDir, "document.html");
14
15// Prepare a path for converted file saving
16string savePath = Path.Combine(OutputDir, "document.pdf");
17
18// Convert HTML to PDF with customized configuration
19Converter.ConvertHTML(documentPath, configuration, new PdfSaveOptions(), savePath);In this example, TimeoutMessageHandler is added to the MessageHandlers collection of INetworkService, and the timeout value is set to 1 second. The HTMLDocument class is then used to load the HTML file and perform the conversion.
By using the TimeoutMessageHandler class and handling network timeouts deliberately, you can make HTML opening and conversion workflows more predictable when remote resources are slow or unavailable.
You can download the complete examples and data files from GitHub.
Timeouts prevent remote resources from making an HTML opening or conversion workflow wait indefinitely.
Yes. If a remote resource does not load before the timeout, the document may open or render without that resource.
Insert the timeout handler at the top of the MessageHandlers collection so it applies before later handlers process requests.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.