silentgut
silentgut

Reputation: 87

I got an error "System.Net.Http.WinHttpException: The operation timed" after i migrated to .NET Core 2.2

I am migrating my API to .NET Core 2.2 and in my application is calling another wsdl (WCF) service. Upon calling that service, I'm getting an error saying

System.Net.Http.WinHttpException: The operation timed

Is there something wrong with the way i migrated? It is perfectly working in my previous solution running at .net 4.5

Here is the full inner text message.

InnerException: System.Net.Http.HttpRequestException: An error occurred while sending the request.

System.Net.Http.WinHttpException: The operation timed out

at System.Threading.Tasks.RendezvousAwaitable``1.GetResult()
at System.Net.Http.WinHttpHandler.StartRequest(WinHttpRequestState state)
--- End of inner exception stack trace --->

at System.ServiceModel.Channels.ServiceModelHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at System.ServiceModel.Channels.HttpChannelFactory
1.HttpClientRequestChannel.HttpClientChannelAsyncRequest.SendRequestAsync(Message message, TimeoutHelper timeoutHelper)

Upvotes: 1

Views: 4601

Answers (1)

Ali Bahrami
Ali Bahrami

Reputation: 6073

Does your soap request take longer than 30 seconds? If yes, you need to know that default timeout in .NET Core for soap request is 30 seconds.

It's a little tricky to change to timeout, but someone already figured it how:

        static void Main(string[] args)
        {
            var client = new SimpleServiceClient();
            client.OpenAsync().GetAwaiter().GetResult();
            client.DelayedResponseAsync(2000).GetAwaiter().GetResult();
            var channel = client.InnerChannel;
            var httpChannelFactory = client.InnerChannel.GetProperty<IChannelFactory>();
            var cacheField = httpChannelFactory.GetType().GetField("_httpClientCache", BindingFlags.NonPublic | BindingFlags.Instance);
            var httpClientCache = cacheField.GetValue(httpChannelFactory);
            var cacheDictionaryField = httpClientCache.GetType().GetField("_items", BindingFlags.NonPublic | BindingFlags.Instance);

            IDictionary cacheDictionary = (IDictionary)cacheDictionaryField.GetValue(httpClientCache);
            foreach(var cacheKey in cacheDictionary.Keys)
            {
                var cacheEntry = cacheDictionary[cacheKey];
                var valueField = cacheEntry.GetType().GetField("value", BindingFlags.NonPublic | BindingFlags.Instance);
                HttpClient httpClient = (HttpClient)valueField.GetValue(cacheEntry);
                FixHttpClient(httpClient);
            }

            client.DelayedResponseAsync(50000).GetAwaiter().GetResult();
            Console.WriteLine("Done");
            Console.ReadLine();
        }

        private static void FixHttpClient(HttpClient httpClient)
        {
            var handlerField = typeof(HttpMessageInvoker).GetField("_handler", BindingFlags.NonPublic | BindingFlags.Instance);
            DelegatingHandler delegatingHandler = (DelegatingHandler)handlerField.GetValue(httpClient); // Should be of type ServiceModelHttpMessageHandler
            WinHttpHandler winHttpHandler = (WinHttpHandler)delegatingHandler.InnerHandler;
            WinHttpHandler newHandler = new WinHttpHandler();
            newHandler.ServerCredentials = winHttpHandler.ServerCredentials;
            newHandler.CookieUsePolicy = winHttpHandler.CookieUsePolicy;
            newHandler.ClientCertificates.AddRange(winHttpHandler.ClientCertificates);
            newHandler.ServerCertificateValidationCallback = winHttpHandler.ServerCertificateValidationCallback;
            newHandler.Proxy = winHttpHandler.Proxy;
            newHandler.AutomaticDecompression = winHttpHandler.AutomaticDecompression;
            newHandler.PreAuthenticate = winHttpHandler.PreAuthenticate;
            newHandler.CookieContainer = winHttpHandler.CookieContainer;

            // Fix the timeouts
            newHandler.ReceiveHeadersTimeout = Timeout.InfiniteTimeSpan;
            newHandler.ReceiveDataTimeout = Timeout.InfiniteTimeSpan;
            newHandler.SendTimeout = Timeout.InfiniteTimeSpan;

            var servicemodelHttpHandlerInnerHandlerField = delegatingHandler.GetType().GetField("_innerHandler", BindingFlags.NonPublic | BindingFlags.Instance);
            servicemodelHttpHandlerInnerHandlerField.SetValue(delegatingHandler, newHandler);
            var delegatingHandlerInnerHandlerField = typeof(DelegatingHandler).GetField("_innerHandler", BindingFlags.NonPublic | BindingFlags.Instance);
            delegatingHandlerInnerHandlerField.SetValue(delegatingHandler, newHandler);
        }

So eaily, pass your HttpClient to

Code directly copied from this gist.

Upvotes: 1

Related Questions