How to make timeout in WCF client shorter. I want to call some WCF service periodically and check if it is available or not. I want that call to the service method be no longer than one second. I am trying to manipulate different timeouts but in each case it is executing for more than 20 seconds.
Below you'll find my code:
Service:
[ServiceContract]
public interface ICustomerManager
{
[OperationContract]
void StoreCustomer(string customerName);
[OperationContract]
void Ping();
}
[ServiceBehavior(IncludeExceptionDetailInFaults=true)]
public class CustomerManager : MarshalByRefObject, ICustomerManager
{
public void StoreCustomer(string customerName)
{
if(customerName == null) throw new ArgumentNullException("customerName");
Console.WriteLine(customerName + " stored.");
}
public void Ping()
{
}
}
Client:
var binding = new NetTcpBinding(SecurityMode.None);
binding.OpenTimeout = TimeSpan.FromSeconds(1);
binding.SendTimeout = TimeSpan.FromSeconds(1);
binding.ReceiveTimeout = TimeSpan.FromSeconds(1);
binding.CloseTimeout = TimeSpan.FromSeconds(1);
binding.ReliableSession.Enabled = true;
var bindingElements = binding.CreateBindingElements();
var tcpBindingElement = bindingElements.Find<TcpTransportBindingElement>();
tcpBindingElement.ChannelInitializationTimeout = TimeSpan.FromSeconds(1);
var factory = new ChannelFactory<ICustomerManager>(binding, "net.tcp://10.68.117.19:9998/CustomerManager");
var customerManager = factory.CreateChannel();
var contextChannel = customerManager as IClientChannel;
contextChannel.OperationTimeout = TimeSpan.FromMilliseconds(1000);
var start = DateTime.Now;
try
{
customerManager.Ping();
}
catch (CommunicationException)
{
var elapsed = DateTime.Now - start;
Console.WriteLine("elapsed: {0}", elapsed);
}
The output is: elapsed: 00:00:20.8662525
How to make it to execute no longer than a second.
I am not attaching code for my server because I am testing a case when it is not available.
I have found very similar problem on msdn forum link unfortunately answer there also does not work.