Timeout of 100 seconds when using SendAll in a .Net Core projet

Hi,

I have encountered a timeout issue when calling a service using JsonServiceClient from an Azure Function (a .Net Core project). AFAIK the issue is not related to the Azure environment since I was able to reproduce the issue outside of an Azure Function by creating a .Net Core test.

It looks like the Timeout property of the client is ignored and the request times out after the default value of 100 seconds. As a workaround I am using the sync Send and it is working as expected.

For testing purposes I tried using the full .Net Framework (4.8) and the issue does not occurs. It only happens in .Net Core.

You will find below some code to reproduce the issue.

// service
    public class MyServices : Service
    {
        public async Task<HelloResponse> Any(Hello request)
        {
            await Task.Delay(TimeSpan.FromMinutes(3)).ConfigureAwait(false);
            
            return new HelloResponse { Result = $"Hello, {request.Name}!" };
        }
    }

// Unit tests, must be called from a .Net Core project
        [TestMethod]
        public async Task TestMethodAsync()
        {
            var baseUrl = "http://localhost:14993";
            var client = new JsonServiceClient(baseUrl);
            client.Timeout = TimeSpan.FromMinutes(5);
            var request = new Hello { Name = "Bob" };
            var response = await client.SendAsync(request).ConfigureAwait(false);
        }

        [TestMethod]
        public void TestMethodSync()
        {
            var baseUrl = "http://localhost:14993";
            var client = new JsonServiceClient(baseUrl);
            client.Timeout = TimeSpan.FromMinutes(5);
            var request = new Hello { Name = "Bob" };
            var response = client.Send(request);
        }

The implementation of .NET’s HttpWebRequest that JsonServiceClient uses is completely different in .NET Framework vs .NET Core (which behind the scenes is retrofitted on HttpClient) where not all features are implemented.

If you’re using .NET Core I would use JsonHttpClient from ServiceStack.HttpClient instead which is the more optimal implementation for .NET Core.

You can then fetch the underlying HttpClient for customization with:

var client = new JsonHttpClient(BaseUrl);
var httpClient = client.GetHttpClient();

I tried JsonHttpClient from my .Net Core test project and it worked as expected.

Thanks!

1 Like