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

Viewed 6873

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)

3 Answers

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.

Answer by Ryan Wilson will work but only until you will try to update service. Reference.cs will be overwritten. In .NET Core 3.1 you can grammatically modify binding timeouts:

 public MemoqTMServiceClass(string api_key)
    {
        
        client = new TMServiceClient();
        
        var eab = new EndpointAddressBuilder(client.Endpoint.Address);

        eab.Headers.Add(
              AddressHeader.CreateAddressHeader("ApiKey",  // Header Name
                                                 string.Empty,           // Namespace
                                                 api_key));  // Header Value

        client.Endpoint.Address = eab.ToEndpointAddress();
        client.Endpoint.Binding.CloseTimeout = new TimeSpan(2, 0, 0);
        client.Endpoint.Binding.OpenTimeout = new TimeSpan(2, 0, 0);
        client.Endpoint.Binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
        client.Endpoint.Binding.SendTimeout = new TimeSpan(0, 10, 0);
    }

Just implement the following partial method in the generated proxy class to configure the service endpoint. Place the partial method in your own file to make sure it will not be overwritten.

static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);
Related