Generally, you can set the Timeout attribute value in the configuration file to customize the service Timeout time. However, the WCF Service does not have a configuration file, and the default Timeout value is 60 s. But sometimes, if our query time exceeds 60 s, can we define the time-out by ourselves.
The answer is yes. The proxy class generated by RIA Services is just partial. We can use this to set timeout.
First, create a class WcfTimeoutUtility in the Service project. Here, set the timeout method.
/// <summary> /// Utility class for changing a domain context's WCF endpoint's /// SendTimeout. /// </summary> public static class WcfTimeoutUtility { /// <summary> /// Changes the WCF endpoint SendTimeout for the specified domain /// context. /// </summary> /// <param name="context">The domain context to modify.</param> /// <param name="sendTimeout">The new timeout value.</param> public static void ChangeWcfSendTimeout(DomainContext context, TimeSpan sendTimeout) { PropertyInfo channelFactoryProperty = context.DomainClient.GetType().GetProperty("ChannelFactory"); if (channelFactoryProperty == null) { throw new InvalidOperationException( "There is no 'ChannelFactory' property on the DomainClient."); } ChannelFactory factory = (ChannelFactory) channelFactoryProperty.GetValue(context.DomainClient, null); factory.Endpoint.Binding.SendTimeout = sendTimeout; } }
The default time is changed based on the features of the partial classification.
Note that the class name: MyDomainContext must be the same as the DomainContext class name in the code automatically generated by your Service, and the namespace should also be consistent with the DomainContext class in web. g. cs.
public partial class MyDomainContext { partial void OnCreated() { TimeSpan tenMinutes = new TimeSpan(0, 10, 0); WcfTimeoutUtility.ChangeWcfSendTimeout(this, tenMinutes); } }