Skip to content

Commit d0af139

Browse files
committed
Merge branch 'sync-context-callbacks' of https://github.com/petejohanson/RestSharp
Conflicts: RestSharp/RestClient.cs
2 parents ed93e79 + 24da780 commit d0af139

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

RestSharp/Http.Async.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,15 +317,7 @@ private void ResponseCallback(IAsyncResult result, Action<HttpResponse> callback
317317

318318
private static void ExecuteCallback(HttpResponse response, Action<HttpResponse> callback)
319319
{
320-
#if WINDOWS_PHONE
321-
var dispatcher = Deployment.Current.Dispatcher;
322-
dispatcher.BeginInvoke(() =>
323-
{
324-
#endif
325320
callback(response);
326-
#if WINDOWS_PHONE
327-
});
328-
#endif
329321
}
330322

331323
partial void AddAsyncHeaderActions()

RestSharp/IRestClient.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public interface IRestClient
4141
/// <summary>
4242
///
4343
/// </summary>
44+
bool UseSynchronizationContext { get; set; }
45+
/// <summary>
46+
///
47+
/// </summary>
4448
IAuthenticator Authenticator { get; set; }
4549
/// <summary>
4650
///

RestSharp/RestClient.Async.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System;
1818
using System.Collections.Generic;
1919
using System.Linq;
20+
using System.Threading;
2021
using System.Text;
2122
using System.Net;
2223

@@ -52,26 +53,38 @@ public virtual RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action<
5253
AddDefaultParameter("Accept", accepts, ParameterType.HttpHeader);
5354
HttpWebRequest webRequest = null;
5455
var asyncHandle = new RestRequestAsyncHandle();
56+
57+
Action<HttpResponse> response_cb = r => ProcessResponse(r, asyncHandle, callback);
58+
59+
if (UseSynchronizationContext && SynchronizationContext.Current != null) {
60+
var ctx = SynchronizationContext.Current;
61+
var cb = response_cb;
62+
63+
response_cb = resp => ctx.Post(s => cb(resp), null);
64+
}
5565

5666
switch(request.Method)
5767
{
5868
case Method.GET:
59-
webRequest = http.GetAsync(r => ProcessResponse(r, asyncHandle, callback));
69+
webRequest = http.GetAsync(response_cb);
6070
break;
6171
case Method.POST:
62-
webRequest = http.PostAsync(r => ProcessResponse(r, asyncHandle, callback));
72+
webRequest = http.PostAsync(response_cb);
6373
break;
6474
case Method.PUT:
65-
webRequest = http.PutAsync(r => ProcessResponse(r, asyncHandle, callback));
75+
webRequest = http.PutAsync(response_cb);
6676
break;
6777
case Method.DELETE:
68-
webRequest = http.DeleteAsync(r => ProcessResponse(r, asyncHandle, callback));
78+
webRequest = http.DeleteAsync(response_cb);
6979
break;
7080
case Method.HEAD:
71-
webRequest = http.HeadAsync(r => ProcessResponse(r, asyncHandle, callback));
81+
webRequest = http.HeadAsync(response_cb);
7282
break;
7383
case Method.OPTIONS:
74-
webRequest = http.OptionsAsync(r => ProcessResponse(r, asyncHandle, callback));
84+
webRequest = http.OptionsAsync(response_cb);
85+
break;
86+
case Method.PATCH:
87+
webRequest = http.PatchAsync(r => ProcessResponse(r, asyncHandle, callback));
7588
break;
7689
case Method.PATCH:
7790
webRequest = http.PatchAsync(r => ProcessResponse(r, asyncHandle, callback));

RestSharp/RestClient.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public partial class RestClient : IRestClient
4141
/// </summary>
4242
public RestClient()
4343
{
44-
CookieContainer = new CookieContainer ();
44+
#if WINDOWS_PHONE
45+
UseSynchronizationContext = true;
46+
#endif
4547
ContentHandlers = new Dictionary<string, IDeserializer>();
4648
AcceptTypes = new List<string>();
4749
DefaultParameters = new List<Parameter>();
@@ -241,6 +243,11 @@ IDeserializer GetHandler(string contentType)
241243
/// </summary>
242244
public int Timeout { get; set; }
243245

246+
/// <summary>
247+
/// Whether to invoke async callbacks using the SynchronizationContext.Current captured when invoked
248+
/// </summary>
249+
public bool UseSynchronizationContext { get; set; }
250+
244251
/// <summary>
245252
/// Authenticator to use for requests made by this client instance
246253
/// </summary>

0 commit comments

Comments
 (0)