Network Timeouts – How to Set in C# examples

Network timeouts can occur when a network connection is not able to be established, or a server is taking too long to respond. When a network timeout occurs, it can impact the functionality of a software application and cause a range of problems. This article will discuss how to set network timeouts in C# with practical examples. You find simple C# examples of how to create a custom message handler for network operation timeouts.

Network Timeouts

The network timeout refers to the amount of time a client, such as a software application, waits for a response from a server after sending a request. A timeout value is set to avoid waiting indefinitely for a response in case the server is unavailable or unresponsive.
A network timeout is a time limit for completing a network operation, such as sending a request or waiting for a response. If the network operation exceeds the timeout, it is considered to have failed. Network timeout affects the performance and reliability of network communications by determining the maximum amount of time allowed for a request to be completed successfully. When the timeout is reached, the operation may be terminated, resulting in an error or failure. Network timeout is an essential factor to consider when designing network applications because it can affect the responsiveness, speed, and availability of network communication.

Message Handler for Network Operation Timeouts

In order to set the maximum network operation timeout, you will need to define your own network request handler and register it at the top of the queue of such handlers. Thus, all network requests will pass through it.
The following example shows how to use this functionality. First, let’s create our own network operation handler TimeoutMessageHandler that will set the maximum network timeout to 1 second and simply pass the message further down the chain.

 1using System;
 2using Aspose.Html.Net;
 3...
 4
 5	// Define the TimeoutMessageHandler class that is derived from the MessageHandler class
 6	public class TimeoutMessageHandler : MessageHandler
 7    {
 8        // Override the Invoke() method
 9		public override void Invoke(INetworkOperationContext context)
10        {
11            context.Request.Timeout = TimeSpan.FromSeconds(1);
12            Next(context);
13        }
14    }

In the C# code snippet above, we define a custom TimeoutMessageHandler class. The class inherits from the MessageHandler class and overrides its Invoke() method. In the Invoke() method, a timeout of 1 second is set for the Request property of the INetworkOperationContext object passed in as a parameter. The Next() method is then called to continue the execution of the pipeline. This custom message handler can set a specific timeout value for network operations in an application.

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.

Here is an example of how you can use the TimeoutMessageHandler class to set a timeout for opening an HTML file using the Aspose.HTML library:

 1using Aspose.Html;
 2using Aspose.Html.Net;
 3using Aspose.Html.Services;
 4using System.IO;
 5...
 6
 7    // Create an instance of the Configuration class
 8    using var configuration = new Configuration();
 9
10    // Call the INetworkService which contains the functionality for managing network operations
11    var network = configuration.GetService<INetworkService>();
12
13    // Add the TimeoutMessageHandler to the top of existing message handler chain
14    network.MessageHandlers.Insert(0, new TimeoutMessageHandler());
15
16    // Prepare path to a source document file
17    string documentPath = Path.Combine(DataDir, "document.html");
18
19    // Create an HTML document with a custom configuration
20    using var 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

Let’s look at the C# code that sets a timeout value of 1 second for requests made through the TimeoutMessageHandler class in a .NET application. The following example shows how to set a timeout for an HTML conversion operation. As a result, all network operations that will occur during the conversion and last more than one second will be interrupted.

 1using Aspose.Html;
 2using Aspose.Html.Net;
 3using Aspose.Html.Services;
 4using Aspose.Html.Converters;
 5using Aspose.Html.Saving;
 6using System.IO;
 7...
 8
 9	// Create an instance of the Configuration class
10    using var configuration = new Configuration();
11
12    // Call the INetworkService which contains the functionality for managing network operations
13    var network = configuration.GetService<INetworkService>();
14
15    // Add the TimeoutMessageHandler to the top of existing message handler chain
16    network.MessageHandlers.Insert(0, new TimeoutMessageHandler());
17
18    // Prepare path to a source document file
19    string documentPath = Path.Combine(DataDir, "document.html");
20
21    // Prepare a path for converted file saving 
22    string savePath = Path.Combine(OutputDir, "document.pdf");
23
24    // Convert HTML to PDF with customized configuration
25    Converter.ConvertHTML(documentPath, configuration, new PdfSaveOptions(), savePath);

In this example, the TimeoutMessageHandler is added to the MessageHandlers collection of the 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 properly, you can ensure a reliable and efficient experience for the user when converting HTML files.

Message Handler for Network Operation Timeouts

You can download the complete examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.