@@ -259,15 +259,17 @@ protected override void DoInvoke(object?[] parameters)
259259 protected override void DoInvoke ( object ? [ ] parameters , AsyncLogEventInfo logEvent )
260260 {
261261 Uri ? url = null ;
262- HttpRequestMessage ? request = null ;
262+ HttpRequestMessage request ;
263+
264+ var continuation = logEvent . Continuation ;
263265
264266 try
265267 {
266268 url = BuildWebServiceUrl ( logEvent . LogEvent , parameters ) ;
267- if ( url == null )
269+ if ( url is null )
268270 {
269271 InternalLogger . Error ( "{0}: Error creating request with invalid url={1}" , this , Url ) ;
270- logEvent . Continuation ( new ArgumentException ( "Invalid Url for HttpClient" ) ) ;
272+ continuation ( new ArgumentException ( "Invalid Url for HttpClient" ) ) ;
271273 return ;
272274 }
273275
@@ -276,12 +278,13 @@ protected override void DoInvoke(object?[] parameters, AsyncLogEventInfo logEven
276278 catch ( Exception ex )
277279 {
278280 InternalLogger . Error ( ex , "{0}: Error creating request for url={1}" , this , url ) ;
279- request ? . Dispose ( ) ;
281+ continuation ( ex ) ;
280282 throw ;
281283 }
282284
283285 var httpClient = ResolveHttpClient ( logEvent . LogEvent ) ;
284- var continuation = logEvent . Continuation ;
286+
287+ var httpStatusCode = default ( HttpStatusCode ) ;
285288
286289 System . Threading . Interlocked . Increment ( ref _pendingWriteOperations ) ;
287290
@@ -300,11 +303,9 @@ protected override void DoInvoke(object?[] parameters, AsyncLogEventInfo logEven
300303 {
301304 try
302305 {
303- if ( task . IsCanceled )
304- throw new OperationCanceledException ( "HTTP Request was canceled." ) ;
305-
306306 using ( var response = task . Result )
307307 {
308+ httpStatusCode = response . StatusCode ;
308309 response . EnsureSuccessStatusCode ( ) ;
309310 }
310311
@@ -322,6 +323,12 @@ protected override void DoInvoke(object?[] parameters, AsyncLogEventInfo logEven
322323 {
323324 InternalLogger . Error ( ex , "{0}: Error starting request for url={1}" , this , url ) ;
324325 request . Dispose ( ) ;
326+ if ( httpStatusCode == 0 && ( NLog . Time . TimeSource . Current . Time - _httpClientExpiry ) > TimeSpan . FromSeconds ( 5 ) )
327+ {
328+ _httpClientExpiry = DateTime . MinValue ; // Force HttpClient renewal on next request, to clear the stale HttpClient connection pool.
329+ }
330+
331+
325332 if ( LogManager . ThrowExceptions )
326333 {
327334 throw ;
@@ -763,7 +770,7 @@ protected override void WriteContent(MemoryStream ms, object?[] parameterValues)
763770 xtw . WriteStartElement ( "Body" , SoapEnvelopeNamespace ) ;
764771 xtw . WriteStartElement ( Target . MethodName , Target . Namespace ) ;
765772
766- WriteAllParametersToCurrenElement ( xtw , parameterValues ) ;
773+ WriteAllParametersToCurrentElement ( xtw , parameterValues ) ;
767774
768775 xtw . WriteEndElement ( ) ; // method name
769776 xtw . WriteEndElement ( ) ; // Body
@@ -852,7 +859,7 @@ protected override void WriteContent(MemoryStream ms, object?[] parameterValues)
852859 {
853860 xtw . WriteStartElement ( Target . XmlRoot , Target . XmlRootNamespace ) ;
854861
855- WriteAllParametersToCurrenElement ( xtw , parameterValues ) ;
862+ WriteAllParametersToCurrentElement ( xtw , parameterValues ) ;
856863
857864 xtw . WriteEndElement ( ) ;
858865 xtw . Flush ( ) ;
@@ -866,7 +873,7 @@ protected HttpPostXmlFormatterBase(WebServiceTarget target) : base(target)
866873 {
867874 }
868875
869- protected void WriteAllParametersToCurrenElement ( XmlWriter currentXmlWriter , object ? [ ] parameterValues )
876+ protected void WriteAllParametersToCurrentElement ( XmlWriter currentXmlWriter , object ? [ ] parameterValues )
870877 {
871878 for ( int i = 0 ; i < Target . Parameters . Count ; i ++ )
872879 {
0 commit comments