How to: Set The Command Timeout
This article will give you details about how you can set the timeout for the execution of all Telerik Data Access operations:
- On Global Level
- On Query Level
- And the exception thrown by the backend driver in case a query exceedes the timeout value.
On Global Level
On global level, the timeout for all Telerik Data Access operations can be set either through The Backend Configuration Of The Model or in the app.config/web.config file of the project that holds the model.
Through The Backend Configuration Of The Model
Consider the SofiaCarRental model. The timeout setting can be applied with the help of the following steps:
- Open the FluentModel file that holds the context.
- Locate the GetBackendConfiguration method, which provides the backend object consumed by the context.
-
In it, adjust the value of the command timeout through the Runtime.CommandTimeout property.
public static BackendConfiguration GetBackendConfiguration() { BackendConfiguration backend = new BackendConfiguration(); backend.Backend = "MsSql"; backend.ProviderName = "System.Data.SqlClient"; backend.Runtime.CommandTimeout = 100; CustomizeBackendConfiguration(ref backend); return backend; }
Public Shared Function GetBackendConfiguration() As BackendConfiguration Dim backend As BackendConfiguration = New BackendConfiguration() backend.Backend = "MsSql" backend.ProviderName = "System.Data.SqlClient" backend.Runtime.CommandTimeout = 100 CustomizeBackendConfiguration(backend) Return backend End Function
Save the FluentModel file.
In The Config File
Setting the timeout value in the app.config/web.config file can be achieved with the following process:
- Open the app.config/web.config file of the application.
-
In the
<configuration>
section, create a new section called openAccessConfiguration:<configSections> <section name="openAccessConfiguration" type="Telerik.OpenAccess.Config.OpenAccessConfigSectionHandler, Telerik.OpenAccess" requirePermission="false" /> </configSections>
-
Set the timeout value in the new section as shown below:
<openAccessConfiguration> <backendConfiguration name="MyBackendConfiguration" backend="MsSql"> <runtime commandTimeout="100"></runtime> </backendConfiguration> </openAccessConfiguration>
Save the app.config/web.config file.
- The setting can be consumed like this:
BackendConfiguration fromCodeConfiguration =
FluentModel.GetBackendConfiguration();
BackendConfiguration.MergeBackendConfigurationFromConfigFile(fromCodeConfiguration,
ConfigurationMergeMode.ConfigFileDefinitionWins,
"MyBackendConfiguration");
using (FluentModel dbContext = new FluentModel(fromCodeConfiguration))
{
//a Telerik Data Access Operation
}
Dim fromCodeConfiguration As BackendConfiguration = _
FluentModel.GetBackendConfiguration()
BackendConfiguration.MergeBackendConfigurationFromConfigFile(fromCodeConfiguration, _
ConfigurationMergeMode.ConfigFileDefinitionWins,
"MyBackendConfiguration")
Using dbContext As New FluentModel(fromCodeConfiguration)
'a Telerik Data Access Operation
End Using
On Query Level
By design, Telerik Data Access will respect the timeout value on query level, if it is provided. In every other case, it will take into account the global one.
The following example demonstrates how to specify a timeout value on query level:
using Telerik.OpenAccess;
using (FluentModel dbContext = new FluentModel())
{
List<Car> allCars = dbContext.Cars
.WithOption(new QueryOptions ()
{
CommandTimeout = 100 //in seconds
}).ToList();
}
Imports Telerik.OpenAccess
Using dbContext As New FluentModel()
Dim allCars As List(Of Car) = dbContext.Cars.
WithOption(New QueryOptions() With
{
.CommandTimeout = 100 'in seconds
}).ToList()
End Using
The WithOption() extension method resides in the Telerik.OpenAccess namespace
If a given Telerik Data Access operation exceeds the timeout value, a backend specific exception with the following text will be thrown:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.