Search code examples
wcfasp.net-core.net-corevisual-studio-2017

How to increase the timeout values for a WCF service in a dot net core 2.1 project


I am posting this because I was unable to find any place on Stack Overflow that addresses this issue for a .Net-Core project utilizing WCF by adding the service reference through Connected Services.

My issue was that I was facing client side timeouts because of long running operation requests.

So, how does one increase the timeout values for the wcf client objects since .Net-Core no longer uses the web config to store the configuration values for the WCF service references? (Please see my provided answer)


Solution

  • Under Connected Services in Solution Explorer, after adding a WCF service, a few files are generated for that service. You should see a folder with the name you gave the WCF service reference and under that a Getting Started, ConnectedService.json and a Reference.cs file.

    To increase any of the client service object's timeout values, open Reference.cs and locate method: GetBindingForEndpoint

    Inside this method you should see something like this:

    if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_IYourService))
                {
                    System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
                    result.MaxBufferSize = int.MaxValue;
                    result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
                    result.MaxReceivedMessageSize = int.MaxValue;
                    result.AllowCookies = true;
                    //Here's where you can set the timeout values
                    result.SendTimeout = new System.TimeSpan(0, 5, 0);
                    result.ReceiveTimeout = new System.TimeSpan(0, 5, 0);
    
                    return result;
                }
    

    Just use result. and the timeout you want to increase like SendTimeout, ReceiveTimeout, etc. and set it to a new TimeSpan with the desired timeout value.

    I hope this proves to be a useful post to someone.