1

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)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
silentgut
  • 87
  • 2
  • 9
  • We can't tell "how you migrated". Most likely there is a configuration error, you will need to dig for more info. Turn on all logging etc. – H H Jun 30 '19 at 05:55
  • Possible duplicate of [How to set timeout of soapclient in .net core project](https://stackoverflow.com/questions/48079244/how-to-set-timeout-of-soapclient-in-net-core-project) – Rahul Sharma Jun 30 '19 at 06:07

1 Answers1

1

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.

Ali Bahrami
  • 5,935
  • 3
  • 34
  • 53