Permalink
Browse files

Making sure that under auth scenarios, we would serialize all request…

…s to a single one at the first time.

That means that we won't have multiple auth dialogs for silverlight on startup
  • Loading branch information...
1 parent 03588f1 commit f964991d789c3fbf02c2028a844d8d2646a77667 @ayende ayende committed Nov 28, 2011
@@ -5,7 +5,9 @@
//-----------------------------------------------------------------------
using System;
using System.Net;
-
+#if SILVERLIGHT
+using Raven.Client.Silverlight.Connection;
+#endif
namespace Raven.Client.Connection
{
/// <summary>
@@ -18,5 +20,11 @@ public class WebRequestEventArgs : EventArgs
/// </summary>
/// <value>The request.</value>
public WebRequest Request{ get; set;}
+#if SILVERLIGHT
+ /// <summary>
+ /// The RavenDB json request
+ /// </summary>
+ public HttpJsonRequest JsonRequest { get; set; }
+#endif
}
}
@@ -64,6 +64,14 @@ public AsyncServerClient(string url, DocumentConvention convention, ICredentials
this.credentials = credentials;
this.jsonRequestFactory = jsonRequestFactory;
this.sessionId = sessionId;
+
+ // required to ensure just a single auth dialog
+ Task task = jsonRequestFactory.CreateHttpJsonRequest(this, url + "/docs?pageSize=0", "GET", credentials, convention)
+ .ReadResponseStringAsync();
+ jsonRequestFactory.ConfigureRequest += (sender, args) =>
+ {
+ args.JsonRequest.WaitForTask = task;
+ };
}
/// <summary>
@@ -36,7 +36,6 @@ public class HttpJsonRequest
internal HttpWebRequest webRequest;
private byte[] postedData;
private int retries;
- private readonly StackTrace usedForDebugging_CreatedBy = Debugger.IsAttached ? new StackTrace() : null;
private Task RecreateWebRequest(Action<HttpWebRequest> result)
{
@@ -70,6 +69,10 @@ internal HttpJsonRequest(string url, string method, RavenJObject metadata, Docum
this.conventions = conventions;
webRequest = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(new Uri(url));
+ var tcs = new TaskCompletionSource<object>();
+ tcs.SetResult(null);
+ WaitForTask = tcs.Task;
+
WriteMetadata(metadata);
webRequest.Method = method;
if (method != "GET")
@@ -84,15 +87,12 @@ internal HttpJsonRequest(string url, string method, RavenJObject metadata, Docum
/// <returns></returns>
public Task<string> ReadResponseStringAsync()
{
- return webRequest
- .GetResponseAsync()
- .ContinueWith(t => ReadStringInternal(() =>
- {
- var localCopy = usedForDebugging_CreatedBy;
- return t.Result;
- }))
- .ContinueWith(task => RetryIfNeedTo(task, ReadResponseStringAsync))
- .Unwrap();
+ return WaitForTask.ContinueWith(_ => webRequest
+ .GetResponseAsync()
+ .ContinueWith(t => ReadStringInternal(() => t.Result))
+ .ContinueWith(task => RetryIfNeedTo(task, ReadResponseStringAsync))
+ .Unwrap())
+ .Unwrap();
}
private Task<T> RetryIfNeedTo<T>(Task<T> task, Func<Task<T>> generator)
@@ -135,11 +135,12 @@ public Task HandleUnauthorizedResponseAsync(HttpWebResponse unauthorizedResponse
public Task<byte[]> ReadResponseBytesAsync()
{
- return webRequest
- .GetResponseAsync()
- .ContinueWith(t => ReadResponse(() => t.Result, ConvertStreamToBytes))
- .ContinueWith(task => RetryIfNeedTo(task, ReadResponseBytesAsync))
- .Unwrap(); ;
+ return WaitForTask.ContinueWith(_ => webRequest
+ .GetResponseAsync()
+ .ContinueWith(t => ReadResponse(() => t.Result, ConvertStreamToBytes))
+ .ContinueWith(task => RetryIfNeedTo(task, ReadResponseBytesAsync))
+ .Unwrap())
+ .Unwrap();
}
static byte[] ConvertStreamToBytes(Stream input)
@@ -212,6 +213,11 @@ private T ReadResponse<T>(Func<WebResponse> getResponse, Func<Stream, T> handleR
/// <value>The response status code.</value>
public HttpStatusCode ResponseStatusCode { get; set; }
+ /// <summary>
+ /// The task to wait all other actions on
+ /// </summary>
+ public Task WaitForTask { get; set; }
+
private void WriteMetadata(RavenJObject metadata)
{
foreach (var prop in metadata)
@@ -267,15 +273,15 @@ public Task WriteAsync(string data)
public Task WriteAsync(byte[] byteArray)
{
postedData = byteArray;
- return webRequest.GetRequestStreamAsync().ContinueWith(t =>
+ return WaitForTask.ContinueWith(_ => webRequest.GetRequestStreamAsync().ContinueWith(t =>
{
var dataStream = t.Result;
using (dataStream)
{
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
}
- });
+ })).Unwrap();
}
/// <summary>
@@ -1,5 +1,6 @@
using System;
using System.Net;
+using System.Threading.Tasks;
using Raven.Client.Connection;
using Raven.Client.Connection.Profiling;
using Raven.Client.Document;
@@ -69,7 +70,7 @@ public HttpJsonRequest CreateHttpJsonRequest(object self, string url, string met
public HttpJsonRequest CreateHttpJsonRequest(object self, string url, string method, RavenJObject metadata, ICredentials credentials, DocumentConvention convention)
{
var request = new HttpJsonRequest(url, method, metadata, convention);
- ConfigureRequest(self, new WebRequestEventArgs { Request = request.webRequest });
+ ConfigureRequest(self, new WebRequestEventArgs { Request = request.webRequest, JsonRequest = request });
return request;
}

0 comments on commit f964991

Please sign in to comment.