77using System . Net . Http ;
88using System . Net . Http . Headers ;
99using System . Text ;
10+ using System . Threading ;
1011using System . Threading . Tasks ;
1112using ServiceStack . Logging ;
1213using ServiceStack . Serialization ;
@@ -41,6 +42,8 @@ public class JsonHttpClient : IServiceClient
4142 public string Password { get ; set ; }
4243 public bool AlwaysSendBasicAuthHeader { get ; set ; }
4344
45+ public CancellationTokenSource CancelTokenSource { get ; set ; }
46+
4447 /// <summary>
4548 /// Gets the collection of headers to be added to outgoing requests.
4649 /// </summary>
@@ -115,10 +118,13 @@ public Task<TResponse> SendAsync<TResponse>(string httpMethod, string absoluteUr
115118
116119 httpReq . Headers . Add ( HttpHeaders . Accept , ContentType ) ;
117120
118- var sendAsyncTask = client . SendAsync ( httpReq ) ;
119-
120121 ApplyWebRequestFilters ( httpReq ) ;
121122
123+ if ( CancelTokenSource == null || CancelTokenSource . IsCancellationRequested )
124+ CancelTokenSource = new CancellationTokenSource ( ) ;
125+
126+ var sendAsyncTask = client . SendAsync ( httpReq , CancelTokenSource . Token ) ;
127+
122128 if ( typeof ( TResponse ) == typeof ( HttpResponseMessage ) )
123129 {
124130 return ( Task < TResponse > ) ( object ) sendAsyncTask ;
@@ -493,15 +499,14 @@ public Task CustomMethodAsync(string httpVerb, IReturnVoid requestDto)
493499
494500 public void CancelAsync ( )
495501 {
496- throw new System . NotImplementedException ( ) ;
502+ CancelTokenSource . Cancel ( ) ;
497503 }
498504
499505 public void Dispose ( )
500506 {
501507 }
502508
503509
504-
505510 public void SendOneWay ( object requestDto )
506511 {
507512 var requestUri = this . AsyncOneWayBaseUri . WithTrailingSlash ( ) + requestDto . GetType ( ) . Name ;
@@ -729,7 +734,6 @@ public List<TResponse> SendAll<TResponse>(IEnumerable<IReturn<TResponse>> reques
729734
730735 public delegate void ResultsFilterHttpResponseDelegate ( HttpResponseMessage webResponse , object response , string httpMethod , string requestUri , object request ) ;
731736
732-
733737 public static class JsonHttpClientUtils
734738 {
735739 public static Dictionary < string , string > ToDictionary ( this HttpResponseHeaders headers )
@@ -755,9 +759,9 @@ public static WebHeaderCollection ToWebHeaderCollection(this HttpResponseHeaders
755759 public static string GetContentType ( this HttpResponseMessage httpRes )
756760 {
757761 IEnumerable < string > values ;
758- if ( httpRes . Headers . TryGetValues ( HttpHeaders . ContentType , out values ) )
759- return values . FirstOrDefault ( ) ;
760- return null ;
762+ return httpRes . Headers . TryGetValues ( HttpHeaders . ContentType , out values )
763+ ? values . FirstOrDefault ( )
764+ : null ;
761765 }
762766 }
763767
0 commit comments