Set Network Timeouts in C#

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.

Network Timeouts

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.

Message Handler for Network Operation Timeouts

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:

  1. Define a custom TimeoutMessageHandler class derived from MessageHandler.
  2. Override Invoke() to access the current network operation.
  3. Set a timeout value on the request inside the INetworkOperationContext.
  4. Call 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.

Network Timeout to Open HTML File

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:

  1. Create a TimeoutMessageHandler instance.
  2. Create a Configuration object.
  3. Get INetworkService from the configuration.
  4. Insert the timeout handler at the top of MessageHandlers.
  5. Open the HTML file with HTMLDocument and the configured pipeline.
 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.

Network Timeout to Convert HTML

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:

  1. Create a TimeoutMessageHandler.
  2. Add it to the network service in the conversion configuration.
  3. Load the HTML document with that configuration.
  4. Run the conversion workflow.
  5. Review the output and missing remote resources if timeout interruptions occur.
 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.

FAQ

Why set network timeouts for HTML conversion?

Timeouts prevent remote resources from making an HTML opening or conversion workflow wait indefinitely.

Can a document render without timed-out resources?

Yes. If a remote resource does not load before the timeout, the document may open or render without that resource.

Where should the timeout handler be placed?

Insert the timeout handler at the top of the MessageHandlers collection so it applies before later handlers process requests.

Related Articles